pub trait PodDeserialize<'de> {
    // Required method
    fn deserialize(
        deserializer: PodDeserializer<'de>
    ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
       where Self: Sized;
}
Expand description

Implementors of this trait can be deserialized from the raw SPA Pod format using a PodDeserializer-

Their deserialize method should invoke exactly one of the deserialize_*() methods of the provided PodDeserializer that fits the type that should be deserialized.

If you want to deserialize from a pod that always has the same size, implement super::FixedSizedPod instead and this trait will be implemented for you automatically.

§Examples

Deserialize a String pod without copying:

use std::io;
use libspa::pod::deserialize::{PodDeserialize, PodDeserializer, DeserializeError, DeserializeSuccess, StringVisitor};

struct ContainsStr<'s>(&'s str);

impl<'de> PodDeserialize<'de> for ContainsStr<'de> {
    fn deserialize(
        deserializer: PodDeserializer<'de>,
    ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
    where
        Self: Sized,
    {
        deserializer.deserialize_str(StringVisitor).map(|(s, success)| (ContainsStr(s), success))
    }
}

Bytes pods are created in the same way, but with the serialize_bytes method.

Deserialize an Array pod with Int elements:

use std::io;
use std::io::Cursor;
use libspa::pod::deserialize::{PodDeserialize, PodDeserializer, DeserializeError, DeserializeSuccess, Visitor};
use libspa::pod::serialize::PodSerializer;

struct Numbers(Vec<i32>);

impl<'de> PodDeserialize<'de> for Numbers {
    fn deserialize(
        deserializer: PodDeserializer<'de>,
    ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
    where
        Self: Sized,
    {
        struct NumbersVisitor;

        impl<'de> Visitor<'de> for NumbersVisitor {
            type Value = Numbers;
            type ArrayElem = i32;

            fn visit_array(
                &self,
                elements: Vec<Self::ArrayElem>,
            ) -> Result<Self::Value, DeserializeError<&'de [u8]>> {
                Ok(Numbers(elements))
            }
        }

        deserializer.deserialize_array(NumbersVisitor)
    }
}

Make a struct deserialize from a Struct pod:

use std::{convert::TryInto, io};
use libspa::pod::deserialize::{PodDeserialize, PodDeserializer, DeserializeError, DeserializeSuccess, Visitor, StructPodDeserializer};

struct Animal {
    name: String,
    feet: u8,
    can_fly: bool,
}

impl<'de> PodDeserialize<'de> for Animal {
    fn deserialize(
        deserializer: PodDeserializer<'de>,
    ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
    where
        Self: Sized,
    {
    struct AnimalVisitor;

    impl<'de> Visitor<'de> for AnimalVisitor {
        type Value = Animal;
        type ArrayElem = std::convert::Infallible;

        fn visit_struct(
            &self,
            struct_deserializer: &mut StructPodDeserializer<'de>,
        ) -> Result<Self::Value, DeserializeError<&'de [u8]>> {
            Ok(Animal {
                name: struct_deserializer
                    .deserialize_field()?
                    .expect("Input has too few fields"),
                feet: struct_deserializer
                    .deserialize_field::<i32>()?
                    .expect("Input has too few fields")
                    .try_into()
                    .expect("Animal is a millipede, has too many feet for a u8."),
                can_fly: struct_deserializer
                    .deserialize_field()?
                    .expect("Input has too few fields"),
            })
        }
    }

    deserializer.deserialize_struct(AnimalVisitor)
   }
}

Required Methods§

source

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

Deserialize the type by using the provided PodDeserializer

Implementations on Foreign Types§

source§

impl<'de> PodDeserialize<'de> for &'de str

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de> PodDeserialize<'de> for &'de [u8]

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de> PodDeserialize<'de> for bool

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de> PodDeserialize<'de> for f32

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de> PodDeserialize<'de> for f64

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de> PodDeserialize<'de> for i32

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de> PodDeserialize<'de> for i64

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de> PodDeserialize<'de> for ()

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de> PodDeserialize<'de> for String

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de> PodDeserialize<'de> for Vec<u8>

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de, P: FixedSizedPod + CanonicalFixedSizedPod + Copy> PodDeserialize<'de> for Vec<P>

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

source§

impl<'de, T> PodDeserialize<'de> for (u32, *const T)

source§

fn deserialize( deserializer: PodDeserializer<'de> ) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>
where Self: Sized,

Implementors§

source§

impl<'de> PodDeserialize<'de> for Value

source§

impl<'de> PodDeserialize<'de> for Choice<bool>

source§

impl<'de> PodDeserialize<'de> for Choice<f32>

source§

impl<'de> PodDeserialize<'de> for Choice<f64>

source§

impl<'de> PodDeserialize<'de> for Choice<i32>

source§

impl<'de> PodDeserialize<'de> for Choice<i64>

source§

impl<'de> PodDeserialize<'de> for Choice<Fd>

source§

impl<'de> PodDeserialize<'de> for Choice<Fraction>

source§

impl<'de> PodDeserialize<'de> for Choice<Id>

source§

impl<'de> PodDeserialize<'de> for Choice<Rectangle>

source§

impl<'de> PodDeserialize<'de> for Fd

source§

impl<'de> PodDeserialize<'de> for Fraction

source§

impl<'de> PodDeserialize<'de> for Id

source§

impl<'de> PodDeserialize<'de> for Rectangle