pub trait FixedSizedPod {
type CanonicalType: CanonicalFixedSizedPod;
// Required methods
fn as_canonical_type(&self) -> Self::CanonicalType;
fn from_canonical_type(_: &Self::CanonicalType) -> Self;
}Expand description
Implementors of this trait can be serialized into pods that always have the same size.
This lets them be used as elements in Array type SPA Pods.
Implementors of this automatically implement PodSerialize.
Serialization is accomplished by having the type convert itself into/from the canonical representation of this pod,
e.g. i32 for a Int type pod.
That type then takes care of the actual serialization.
See the CanonicalFixedSizedPod trait for a list of possible target types.
Which type to convert in is specified with the traits FixedSizedPod::CanonicalType type,
while the traits as_canonical_type
and from_canonical_type methods are responsible for the actual conversion.
§Examples
Implementing the trait on a i32 newtype wrapper:
use libspa::pod::FixedSizedPod;
struct Newtype(i32);
impl FixedSizedPod for Newtype {
// The pod we want to serialize into is a `Int` type pod, which has `i32` as it's canonical representation.
type CanonicalType = i32;
fn as_canonical_type(&self) -> Self::CanonicalType {
// Convert self to the canonical type.
self.0
}
fn from_canonical_type(canonical: &Self::CanonicalType) -> Self {
// Create a new Self instance from the canonical type.
Newtype(*canonical)
}
}Required Associated Types§
Sourcetype CanonicalType: CanonicalFixedSizedPod
type CanonicalType: CanonicalFixedSizedPod
The canonical representation of the type of pod that should be serialized to/deserialized from.
Required Methods§
Sourcefn as_canonical_type(&self) -> Self::CanonicalType
fn as_canonical_type(&self) -> Self::CanonicalType
Convert self to the canonical type.
Sourcefn from_canonical_type(_: &Self::CanonicalType) -> Self
fn from_canonical_type(_: &Self::CanonicalType) -> Self
Convert the canonical type to Self.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.