Trait libspa::pod::deserialize::PodDeserialize
source · 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§
sourcefn deserialize(
deserializer: PodDeserializer<'de>
) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>where
Self: Sized,
fn deserialize(
deserializer: PodDeserializer<'de>
) -> Result<(Self, DeserializeSuccess<'de>), DeserializeError<&'de [u8]>>where
Self: Sized,
Deserialize the type by using the provided PodDeserializer