1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
 * SPDX-License-Identifier: MIT
 */
use std::fmt::{Debug, Display, Formatter};
use std::io::{Seek, Write};
use std::mem::size_of;
use std::ops::{Deref, Rem};
use std::os::fd::RawFd;
use std::slice;

use spa_sys::spa_pod;

use array::PodArrayRef;
use bitmap::PodBitmapRef;
use bytes::PodBytesRef;
use choice::PodChoiceRef;
use id::PodIdRef;
use pipewire_wrapper_proc_macro::RawWrapper;
use restricted::PodRawValue;
use sequence::PodSequenceRef;
use string::PodStringRef;
use struct_::PodStructRef;

use crate::spa::pod::choice::{ChoiceType, ChoiceValueType};
use crate::spa::pod::object::PodObjectRef;
use crate::spa::pod::pod_buf::{AllocPod, PodBuf};
use crate::spa::pod::pointer::PodPointerRef;
use crate::spa::pod::restricted::{
    BasicTypePod, CloneTo, PodHeader, PrimitiveValue, SizedPod, WritePod, WriteValue,
};
use crate::spa::type_::{FractionRef, RectangleRef, Type};
use crate::wrapper::RawWrapper;

use self::restricted::write_header;

pub mod array;
pub mod bitmap;
pub mod bytes;
pub mod choice;
pub mod control;
pub mod id;
pub mod iterator;
pub mod object;
pub mod pointer;
pub mod sequence;
pub mod string;
pub mod struct_;

pub mod pod_buf;
mod restricted;

macro_rules! primitive_type_pod_impl {
    ($pod_ref_type:ty, $pod_type:expr, $value_raw_type:ty) => {
        primitive_type_pod_impl!(
            $pod_ref_type,
            $pod_type,
            $value_raw_type,
            $value_raw_type,
            v,
            v
        );
    };

    ($pod_ref_type:ty, $pod_type:expr, $value_raw_type:ty, $value_type:ty) => {
        primitive_type_pod_impl!($pod_ref_type, $pod_type, $value_raw_type, $value_type, v, v);
    };

    ($pod_ref_type:ty, $pod_type:expr, $value_raw_type:ty, $value_type:ty, $value_ident:ident, $convert_value_expr:expr) => {
        impl restricted::PrimitiveValue for $pod_ref_type {}

        impl PodRawValue for $pod_ref_type {
            type RawValue = $value_raw_type;

            fn parse_raw_value(ptr: *const Self::RawValue, size: usize) -> PodResult<Self::Value> {
                if size < size_of::<$value_raw_type>() {
                    Err(PodError::DataIsTooShort(size_of::<$value_raw_type>(), size))
                } else {
                    let $value_ident = unsafe { *ptr };
                    Ok($convert_value_expr)
                }
            }

            fn raw_value_ptr(&self) -> *const Self::RawValue {
                &self.raw.value
            }
        }

        impl PodValue for $pod_ref_type {
            type Value = $value_type;

            fn value(&self) -> PodResult<Self::Value> {
                Self::parse_raw_value(&self.raw_value(), self.raw.pod.size as usize)
            }
        }

        impl WritePod for $pod_ref_type {
            fn write_pod<W>(buffer: &mut W, value: &<Self as PodValue>::Value) -> PodResult<()>
            where
                W: Write + Seek,
            {
                restricted::write_header(
                    buffer,
                    size_of::<$value_raw_type>() as u32,
                    <$pod_ref_type>::static_type(),
                )?;
                Self::write_raw_value(buffer, value)?;
                restricted::write_align_padding(buffer)?;
                Ok(())
            }
        }

        impl WriteValue for $pod_ref_type {
            fn write_raw_value<W>(
                buffer: &mut W,
                value: &<Self as PodValue>::Value,
            ) -> PodResult<()>
            where
                W: Write + Seek,
            {
                let value: $value_raw_type = (*value).into();
                restricted::write_value(buffer, &value)?;
                Ok(())
            }
        }

        impl PodHeader for $pod_ref_type {
            fn pod_header(&self) -> &spa_sys::spa_pod {
                &self.raw.pod
            }

            fn static_type() -> Type {
                $pod_type
            }
        }

        impl $pod_ref_type {
            pub fn raw_value(&self) -> $value_raw_type {
                self.raw.value
            }
        }

        impl Debug for $pod_ref_type {
            fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
                unsafe {
                    f.debug_struct(stringify!($pod_ref_type))
                        .field("pod.type", &self.pod_type())
                        .field("pod.size", &self.pod_size())
                        .field("value", &self.value())
                        .finish()
                }
            }
        }
    };
}

pub enum PodError {
    DataIsTooShort(usize, usize),
    UnknownPodTypeToDowncast,
    WrongPodTypeToCast(Type, Type),
    StringIsNotNullTerminated,
    IndexIsOutOfRange,
    ChoiceElementMissing,
    UnexpectedChoiceElement,
    UnexpectedChoiceElementSize(usize, usize),
    UnsupportedChoiceElementType,
    UnexpectedControlType(u32),
    UnexpectedObjectType(u32),
    UnexpectedChoiceType(ChoiceType, ChoiceType),
    IOError(std::io::Error),
    PodIsNotAligned,
}

impl From<PodError> for crate::Error {
    fn from(value: PodError) -> Self {
        Self::PodParseError(value)
    }
}

impl From<std::io::Error> for PodError {
    fn from(value: std::io::Error) -> Self {
        Self::IOError(value)
    }
}

impl Display for PodError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            PodError::DataIsTooShort(expected, actual) => write!(
                f,
                "POD data size is too small for that type, expected {} > actual {}",
                expected, actual
            ),
            PodError::UnknownPodTypeToDowncast => {
                write!(f, "Cannot downcast Pod, child type is unknown")
            }
            PodError::WrongPodTypeToCast(cast_type, actual_type) => write!(
                f,
                "Cannot cast Pod with type {:?} to type {:?}",
                actual_type, cast_type
            ),
            PodError::StringIsNotNullTerminated => write!(f, "String is not null terminated"),
            PodError::IndexIsOutOfRange => write!(f, "Index is out of range"),
            PodError::ChoiceElementMissing => {
                write!(f, "Cannot find required element for choice type")
            }
            PodError::UnexpectedChoiceElement => write!(f, "Unexpected element in the choice type"),
            PodError::UnexpectedChoiceElementSize(expected, actual) => write!(
                f,
                "Unexpected choice element size, expected {} != actual {}",
                expected, actual
            ),
            PodError::UnsupportedChoiceElementType => write!(f, "Unsupported choice element type"),
            PodError::UnexpectedControlType(type_) => {
                write!(f, "Unexpected control type {}", type_)
            }
            PodError::UnexpectedObjectType(type_) => {
                write!(f, "Unexpected object type {}", type_)
            }
            PodError::UnexpectedChoiceType(expected, actual) => {
                write!(
                    f,
                    "Unexpected choice type, expected {:?} actual {:?}",
                    expected, actual
                )
            }
            PodError::IOError(err) => {
                write!(f, "IOError {:?}", err)
            }
            PodError::PodIsNotAligned => {
                write!(f, "Pod is not aligned!")
            }
        }
    }
}

impl Debug for PodError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

impl std::error::Error for PodError {}

type PodResult<T> = Result<T, PodError>;

const POD_ALIGN: usize = 8;

pub trait PodValue {
    type Value: Debug;
    fn value(&self) -> PodResult<Self::Value>;
}

pub trait FromValue<'a>
where
    Self: 'a,
    Self: Sized,
    &'a Self: WritePod,
{
    fn from_value(value: &<&'a Self as PodValue>::Value) -> PodResult<AllocPod<Self>>;
}

pub trait FromPrimitiveValue
where
    Self: Sized,
    Self: WritePod,
{
    fn from_primitive(value: <Self as PodValue>::Value) -> PodResult<AllocPod<Self>>;
}

/// Allow to convert borrowed pod to allocated.
pub trait ToOwnedPod {
    /// Type of the pod
    type PodType: Sized;

    /// Allocate pod and clone content from the given borrowed
    fn to_owned_pod(&self) -> PodResult<AllocPod<Self::PodType>>;
}

impl<'a, T> ToOwnedPod for &'a T
where
    &'a T: CloneTo,
{
    type PodType = T;

    fn to_owned_pod(&self) -> PodResult<AllocPod<Self::PodType>> {
        AllocPod::from_pod(self)
    }
}

impl<'a, T> FromValue<'a> for T
where
    T: Sized,
    T: 'a,
    &'a T: WritePod,
{
    fn from_value(value: &<&'a Self as PodValue>::Value) -> PodResult<AllocPod<Self>> {
        Ok(PodBuf::<Self>::from_value(value)?.into_pod())
    }
}

impl<T> FromPrimitiveValue for T
where
    T: Sized,
    T: WritePod,
{
    fn from_primitive(value: <Self as PodValue>::Value) -> PodResult<AllocPod<Self>> {
        Ok(PodBuf::<Self>::from_primitive_value(value)?.into_pod())
    }
}

impl<'a, T> PodValue for &'a T
where
    T: PodRawValue,
    T: PrimitiveValue,
{
    type Value = T::Value;
    fn value(&self) -> PodResult<Self::Value> {
        (*self).value()
    }
}

impl<'a, T> WritePod for &'a T
where
    T: WritePod,
    T: PrimitiveValue,
{
    fn write_pod<W>(buffer: &mut W, value: &<Self as PodValue>::Value) -> PodResult<()>
    where
        W: Write + Seek,
    {
        T::write_pod(buffer, value)
    }
}

impl<'a, T> WriteValue for &'a T
where
    T: WriteValue,
    T: PrimitiveValue,
{
    fn write_raw_value<W>(buffer: &mut W, value: &<Self as PodValue>::Value) -> PodResult<()>
    where
        W: Write + Seek,
    {
        T::write_raw_value(buffer, value)
    }
}

impl<T: PodHeader> SizedPod for T {
    fn pod_size(&self) -> usize {
        self.pod_header().size as usize + size_of::<spa_sys::spa_pod>()
    }
}

impl<T: SizedPod> CloneTo for &T {
    fn clone_to(&self, buffer: &mut impl Write) -> PodResult<()> {
        let size = self.pod_size();
        let slice = unsafe { slice::from_raw_parts(*self as *const T as *const u8, size) };
        buffer.write_all(slice)?;
        let rem = size.rem(POD_ALIGN);
        if rem > 0 {
            let padding_len = POD_ALIGN - rem;
            let padding = vec![0u8; padding_len];
            buffer.write_all(padding.as_slice())?;
        }
        Ok(())
    }
}

impl<T> BasicTypePod for T
where
    T: PodHeader,
    T: RawWrapper,
    T: Debug,
{
}

pub trait Upcast {
    fn upcast(&self) -> &PodRef;
}

impl<'a, T: BasicTypePod> Upcast for &'a T {
    fn upcast(&self) -> &PodRef {
        unsafe { PodRef::from_raw_ptr(self.pod_header()) }
    }
}

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct PodRef {
    #[raw]
    raw: spa_sys::spa_pod,
}

impl PodRef {
    pub fn size(&self) -> u32 {
        self.raw.size
    }

    pub fn type_(&self) -> Type {
        Type::from_raw(self.raw.type_)
    }

    pub fn downcast(&self) -> PodResult<BasicType> {
        unsafe {
            match self.pod_type() {
                Type::NONE => Ok(BasicType::NONE),
                Type::BOOL => self.cast().map(BasicType::BOOL),
                Type::ID => self.cast().map(BasicType::ID),
                Type::INT => self.cast().map(BasicType::INT),
                Type::LONG => self.cast().map(BasicType::LONG),
                Type::FLOAT => self.cast().map(BasicType::FLOAT),
                Type::DOUBLE => self.cast().map(BasicType::DOUBLE),
                Type::STRING => self.cast().map(BasicType::STRING),
                Type::BYTES => self.cast().map(BasicType::BYTES),
                Type::RECTANGLE => self.cast().map(BasicType::RECTANGLE),
                Type::FRACTION => self.cast().map(BasicType::FRACTION),
                Type::BITMAP => self.cast().map(BasicType::BITMAP),
                Type::ARRAY => self.cast().map(BasicType::ARRAY),
                Type::STRUCT => self.cast().map(BasicType::STRUCT),
                Type::OBJECT => self.cast().map(BasicType::OBJECT),
                Type::SEQUENCE => self.cast().map(BasicType::SEQUENCE),
                Type::POINTER => self.cast().map(BasicType::POINTER),
                Type::FD => self.cast().map(BasicType::FD),
                Type::CHOICE => self.cast().map(BasicType::CHOICE),
                Type::POD => self.cast().map(BasicType::POD),
                _ => Err(PodError::UnknownPodTypeToDowncast),
            }
        }
    }
}

impl PodHeader for PodRef {
    fn pod_header(&self) -> &spa_pod {
        &self.raw
    }

    fn static_type() -> Type {
        Type::POD
    }
}

impl PodValue for PodRef {
    type Value = ();
    fn value(&self) -> PodResult<Self::Value> {
        Ok(())
    }
}

impl<'a> PodValue for &'a PodRef {
    type Value = BasicTypeValue<'a>;
    fn value(&self) -> PodResult<Self::Value> {
        Self::parse_raw_value(self.as_raw_ptr(), self.size() as usize)
    }
}

impl<'a> WritePod for &'a PodRef {
    fn write_pod<W>(buffer: &mut W, value: &<Self as PodValue>::Value) -> PodResult<()>
    where
        W: Write + Seek,
    {
        match value {
            BasicTypeValue::NONE => write_header(buffer, 0, Type::NONE),
            BasicTypeValue::BOOL(v) => PodBoolRef::write_pod(buffer, v),
            BasicTypeValue::ID(v) => PodIdRef::write_pod(buffer, v),
            BasicTypeValue::INT(v) => PodIntRef::write_pod(buffer, v),
            BasicTypeValue::LONG(v) => PodLongRef::write_pod(buffer, v),
            BasicTypeValue::FLOAT(v) => PodFloatRef::write_pod(buffer, v),
            BasicTypeValue::DOUBLE(v) => PodDoubleRef::write_pod(buffer, v),
            BasicTypeValue::STRING(v) => <&PodStringRef>::write_pod(buffer, v),
            BasicTypeValue::BYTES(v) => <&PodBytesRef>::write_pod(buffer, v),
            BasicTypeValue::RECTANGLE(v) => PodRectangleRef::write_pod(buffer, v),
            BasicTypeValue::FRACTION(v) => PodFractionRef::write_pod(buffer, v),
            BasicTypeValue::BITMAP(v) => <&PodBitmapRef>::write_pod(buffer, v),
            BasicTypeValue::ARRAY(v) => <&PodArrayRef>::write_pod(buffer, v),
            BasicTypeValue::STRUCT(v) => <&PodStructRef>::write_pod(buffer, v),
            BasicTypeValue::OBJECT(v) => <&PodObjectRef>::write_pod(buffer, v),
            BasicTypeValue::SEQUENCE(v) => <&PodSequenceRef>::write_pod(buffer, v),
            BasicTypeValue::POINTER(v) => <&PodPointerRef>::write_pod(buffer, v),
            BasicTypeValue::FD(v) => PodFdRef::write_pod(buffer, v),
            BasicTypeValue::CHOICE(v) => PodChoiceRef::<PodRef>::write_choice_value(buffer, v),
            BasicTypeValue::POD(v) => Err(PodError::UnknownPodTypeToDowncast),
        }
    }
}

impl<'a, T> PodRawValue for &'a T
where
    T: PodRawValue,
    T: PrimitiveValue,
{
    type RawValue = T::RawValue;

    fn raw_value_ptr(&self) -> *const Self::RawValue {
        (*self).raw_value_ptr()
    }

    fn parse_raw_value(ptr: *const Self::RawValue, size: usize) -> PodResult<Self::Value> {
        T::parse_raw_value(ptr, size)
    }
}

impl PodRawValue for PodRef {
    type RawValue = spa_sys::spa_pod;

    fn raw_value_ptr(&self) -> *const Self::RawValue {
        &self.raw
    }

    fn parse_raw_value(ptr: *const Self::RawValue, size: usize) -> PodResult<Self::Value> {
        Ok(())
    }
}

impl<'a> PodRawValue for &'a PodRef {
    type RawValue = spa_sys::spa_pod;

    fn raw_value_ptr(&self) -> *const Self::RawValue {
        &self.raw
    }

    fn parse_raw_value(ptr: *const Self::RawValue, _size: usize) -> PodResult<Self::Value> {
        Ok(match unsafe { PodRef::from_raw_ptr(ptr).downcast()? } {
            BasicType::NONE => BasicTypeValue::NONE,
            BasicType::BOOL(pod) => BasicTypeValue::BOOL(pod.value()?),
            BasicType::ID(pod) => BasicTypeValue::ID(pod.value()?),
            BasicType::INT(pod) => BasicTypeValue::INT(pod.value()?),
            BasicType::LONG(pod) => BasicTypeValue::LONG(pod.value()?),
            BasicType::FLOAT(pod) => BasicTypeValue::FLOAT(pod.value()?),
            BasicType::DOUBLE(pod) => BasicTypeValue::DOUBLE(pod.value()?),
            BasicType::STRING(pod) => BasicTypeValue::STRING(pod.value()?),
            BasicType::BYTES(pod) => BasicTypeValue::BYTES(pod.value()?),
            BasicType::RECTANGLE(pod) => BasicTypeValue::RECTANGLE(pod.value()?),
            BasicType::FRACTION(pod) => BasicTypeValue::FRACTION(pod.value()?),
            BasicType::BITMAP(pod) => BasicTypeValue::BITMAP(pod.value()?),
            BasicType::ARRAY(pod) => BasicTypeValue::ARRAY(pod.value()?),
            BasicType::STRUCT(pod) => BasicTypeValue::STRUCT(pod.value()?),
            BasicType::OBJECT(pod) => BasicTypeValue::OBJECT(pod.value()?),
            BasicType::SEQUENCE(pod) => BasicTypeValue::SEQUENCE(pod.value()?),
            BasicType::POINTER(pod) => BasicTypeValue::POINTER(pod.value()?),
            BasicType::FD(pod) => BasicTypeValue::FD(pod.value()?),
            BasicType::CHOICE(pod) => BasicTypeValue::CHOICE(pod.choice_value()?),
            _ => return Err(PodError::UnknownPodTypeToDowncast),
        })
    }
}

impl Debug for PodRef {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PodRef")
            .field("size", &self.size())
            .field("type", &self.type_())
            .field("value", &self.value())
            .finish()
    }
}

#[repr(u32)]
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub enum BasicType<'a> {
    NONE = Type::NONE.raw,
    BOOL(&'a PodBoolRef) = Type::BOOL.raw,
    ID(&'a PodIdRef) = Type::ID.raw,
    INT(&'a PodIntRef) = Type::INT.raw,
    LONG(&'a PodLongRef) = Type::LONG.raw,
    FLOAT(&'a PodFloatRef) = Type::FLOAT.raw,
    DOUBLE(&'a PodDoubleRef) = Type::DOUBLE.raw,
    STRING(&'a PodStringRef) = Type::STRING.raw,
    BYTES(&'a PodBytesRef) = Type::BYTES.raw,
    RECTANGLE(&'a PodRectangleRef) = Type::RECTANGLE.raw,
    FRACTION(&'a PodFractionRef) = Type::FRACTION.raw,
    BITMAP(&'a PodBitmapRef) = Type::BITMAP.raw,
    ARRAY(&'a PodArrayRef) = Type::ARRAY.raw,
    STRUCT(&'a PodStructRef) = Type::STRUCT.raw,
    OBJECT(&'a PodObjectRef) = Type::OBJECT.raw,
    SEQUENCE(&'a PodSequenceRef) = Type::SEQUENCE.raw,
    POINTER(&'a PodPointerRef) = Type::POINTER.raw,
    FD(&'a PodFdRef) = Type::FD.raw,
    CHOICE(&'a PodChoiceRef) = Type::CHOICE.raw,
    POD(&'a PodRef) = Type::POD.raw,
}

#[repr(u32)]
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub enum BasicTypeValue<'a> {
    NONE = Type::NONE.raw,
    BOOL(<PodBoolRef as PodValue>::Value) = Type::BOOL.raw,
    ID(<PodIdRef as PodValue>::Value) = Type::ID.raw,
    INT(<PodIntRef as PodValue>::Value) = Type::INT.raw,
    LONG(<PodLongRef as PodValue>::Value) = Type::LONG.raw,
    FLOAT(<PodFloatRef as PodValue>::Value) = Type::FLOAT.raw,
    DOUBLE(<PodDoubleRef as PodValue>::Value) = Type::DOUBLE.raw,
    STRING(<&'a PodStringRef as PodValue>::Value) = Type::STRING.raw,
    BYTES(<&'a PodBytesRef as PodValue>::Value) = Type::BYTES.raw,
    RECTANGLE(<PodRectangleRef as PodValue>::Value) = Type::RECTANGLE.raw,
    FRACTION(<PodFractionRef as PodValue>::Value) = Type::FRACTION.raw,
    BITMAP(<&'a PodBitmapRef as PodValue>::Value) = Type::BITMAP.raw,
    ARRAY(<&'a PodArrayRef as PodValue>::Value) = Type::ARRAY.raw,
    STRUCT(<&'a PodStructRef as PodValue>::Value) = Type::STRUCT.raw,
    OBJECT(<&'a PodObjectRef as PodValue>::Value) = Type::OBJECT.raw,
    SEQUENCE(<&'a PodSequenceRef as PodValue>::Value) = Type::SEQUENCE.raw,
    POINTER(<&'a PodPointerRef as PodValue>::Value) = Type::POINTER.raw,
    FD(<PodFdRef as PodValue>::Value) = Type::FD.raw,
    CHOICE(ChoiceValueType) = Type::CHOICE.raw,
    POD(&'a PodRef) = Type::POD.raw,
}

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct PodBoolRef {
    #[raw]
    raw: spa_sys::spa_pod_bool,
}

primitive_type_pod_impl!(PodBoolRef, Type::BOOL, i32, bool, v, v != 0);

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct PodIntRef {
    #[raw]
    raw: spa_sys::spa_pod_int,
}

primitive_type_pod_impl!(PodIntRef, Type::INT, i32);

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct PodLongRef {
    #[raw]
    raw: spa_sys::spa_pod_long,
}

primitive_type_pod_impl!(PodLongRef, Type::LONG, i64);

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct PodFloatRef {
    #[raw]
    raw: spa_sys::spa_pod_float,
}

primitive_type_pod_impl!(PodFloatRef, Type::FLOAT, f32);

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct PodDoubleRef {
    #[raw]
    raw: spa_sys::spa_pod_double,
}

primitive_type_pod_impl!(PodDoubleRef, Type::DOUBLE, f64);

#[derive(RawWrapper, Copy, Clone)]
#[repr(transparent)]
pub struct PodRectangleRef {
    #[raw]
    raw: spa_sys::spa_pod_rectangle,
}

primitive_type_pod_impl!(
    PodRectangleRef,
    Type::RECTANGLE,
    spa_sys::spa_rectangle,
    RectangleRef,
    v,
    RectangleRef::from_raw(v)
);

#[derive(RawWrapper, Copy, Clone)]
#[repr(transparent)]
pub struct PodFractionRef {
    #[raw]
    raw: spa_sys::spa_pod_fraction,
}

primitive_type_pod_impl!(
    PodFractionRef,
    Type::FRACTION,
    spa_sys::spa_fraction,
    FractionRef,
    v,
    FractionRef::from_raw(v)
);

#[derive(RawWrapper)]
#[repr(transparent)]
pub struct PodFdRef {
    #[raw]
    raw: spa_sys::spa_pod_fd,
}

primitive_type_pod_impl!(PodFdRef, Type::FD, i64, RawFd, v, v as RawFd);