pub trait Serialize: Sized {
// Required method
fn serialize<S: Serializer>(
&self,
ser: &mut S,
pos: &Position,
) -> SuccessResult;
}Expand description
Allows for the serialization of the implemented type
Implementors must provide the functionality to write self into any Serializer.
§Example
Best to not implemented by hand but rather derived via the Serialize derive macro of the proc_contra crate. See: Contra
use crate::{lib_contra::{serialize::Serialize, serialize::Serializer, position::Position, error::SuccessResult}};
struct Point {
x: f32,
y: f32,
z: f32
}
impl Serialize for Point {
fn serialize<S: Serializer>(&self, ser: &mut S, _pos: &Position) -> SuccessResult {
ser.begin_struct("Point", 3)?;
ser.serialize_field("x", &self.x, &Position::Trailing)?;
ser.serialize_field("y", &self.y, &Position::Trailing)?;
ser.serialize_field("z", &self.z, &Position::Closing)?;
ser.end_struct("Point")?;
Ok(())
}
}Required Methods§
fn serialize<S: Serializer>(&self, ser: &mut S, pos: &Position) -> SuccessResult
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.