pub trait PodSerialize {
    // Required method
    fn serialize<O: Write + Seek>(
        &self,
        serializer: PodSerializer<O>
    ) -> Result<SerializeSuccess<O>, GenError>;
}
Expand description

Implementors of this trait are able to serialize themselves into a SPA pod by using a PodSerializer.

Their serialize method should invoke exactly one of the serialize_*() methods of the provided PodSerializer that fits the type that will be serialized.

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

§Examples

Make a type serialize into a String pod.

use std::io;
use libspa::pod::serialize::{GenError, PodSerialize, PodSerializer, SerializeSuccess};

struct StringNewtype(String);

impl PodSerialize for StringNewtype {
    fn serialize<O: io::Write + io::Seek>(
        &self,
        serializer: PodSerializer<O>,
    ) -> Result<SerializeSuccess<O>, GenError> {
        serializer.serialize_string(self.0.as_str())
    }
}

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

Make a type serialize into a Array pod with Int pod elements:

use std::io;
use libspa::pod::serialize::{GenError, PodSerialize, PodSerializer, SerializeSuccess};

struct Numbers(Vec<i32>);

impl PodSerialize for Numbers {
    fn serialize<O: io::Write + io::Seek>(
        &self,
        serializer: PodSerializer<O>,
    ) -> Result<SerializeSuccess<O>, GenError> {
        let mut array_serializer = serializer.serialize_array(self.0.len() as u32)?;
        for element in self.0.iter() {
            array_serializer.serialize_element(element)?;
        }
        array_serializer.end()
    }
}

Make a struct serialize into a Struct pod:

use std::io;
use libspa::pod::serialize::{GenError, PodSerialize, PodSerializer, SerializeSuccess};

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

impl PodSerialize for Animal {
    fn serialize<O: io::Write + io::Seek>(
        &self,
        serializer: PodSerializer<O>,
    ) -> Result<SerializeSuccess<O>, GenError> {
        let mut struct_serializer = serializer.serialize_struct()?;
        struct_serializer.serialize_field(self.name.as_str())?;
        // No pod exists for u8, we need to use an `Int` type pod by casting to `i32`.
        struct_serializer.serialize_field(&(self.feet as i32))?;
        struct_serializer.serialize_field(&self.can_fly)?;
        struct_serializer.end()
    }
}

Required Methods§

source

fn serialize<O: Write + Seek>( &self, serializer: PodSerializer<O> ) -> Result<SerializeSuccess<O>, GenError>

Serialize the type by using the provided PodSerializer

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl PodSerialize for str

source§

fn serialize<O: Write + Seek>( &self, serializer: PodSerializer<O> ) -> Result<SerializeSuccess<O>, GenError>

source§

impl PodSerialize for [u8]

source§

fn serialize<O: Write + Seek>( &self, serializer: PodSerializer<O> ) -> Result<SerializeSuccess<O>, GenError>

source§

impl<P: FixedSizedPod> PodSerialize for [P]

source§

fn serialize<O: Write + Seek>( &self, serializer: PodSerializer<O> ) -> Result<SerializeSuccess<O>, GenError>

source§

impl<T> PodSerialize for (u32, *const T)

source§

fn serialize<O: Write + Seek>( &self, serializer: PodSerializer<O> ) -> Result<SerializeSuccess<O>, GenError>

Implementors§