pub trait Serialize {
// Required method
unsafe fn ser_on_field_write(
&self,
backend: &mut impl WriteWithNames,
) -> Result<()>;
// Provided methods
unsafe fn serialize(&self, backend: &mut impl WriteNoStd) -> Result<usize> { ... }
unsafe fn serialize_with_schema(
&self,
backend: &mut impl WriteNoStd,
) -> Result<Schema> { ... }
unsafe fn store(&self, path: impl AsRef<Path>) -> Result<()> { ... }
}Expand description
Main serialization trait. It is separated from SerInner to avoid
that the user modify its behavior, and hide internal serialization methods.
It provides a convenience method Serialize::store that serializes the
type to a file.
§Safety
All serialization methods are unsafe as they write padding bytes. Serializing to such a vector and accessing such bytes will lead to undefined behavior as padding bytes are uninitialized.
If you are concerned about this issue, you must organize your structures so
that they do not contain any padding (e.g., by creating explicit padding
bytes). Traits like
FromByte
can provide this guarantee.
For example, this code reads a portion of the stack:
use epserde::{ser::Serialize, Epserde};
#[repr(C)]
#[repr(align(1024))]
#[epserde(zero_copy)]
struct Example(u8);
let value = [Example(0), Example(1)];
let mut bytes = vec![];
unsafe { value.serialize(&mut bytes).unwrap(); }
for chunk in bytes.chunks(8) {
println!("{:016x}", u64::from_ne_bytes(chunk.try_into().unwrap()));
}Required Methods§
Sourceunsafe fn ser_on_field_write(
&self,
backend: &mut impl WriteWithNames,
) -> Result<()>
unsafe fn ser_on_field_write( &self, backend: &mut impl WriteWithNames, ) -> Result<()>
Provided Methods§
Sourceunsafe fn serialize(&self, backend: &mut impl WriteNoStd) -> Result<usize>
unsafe fn serialize(&self, backend: &mut impl WriteNoStd) -> Result<usize>
Sourceunsafe fn serialize_with_schema(
&self,
backend: &mut impl WriteNoStd,
) -> Result<Schema>
unsafe fn serialize_with_schema( &self, backend: &mut impl WriteNoStd, ) -> Result<Schema>
Serializes the type using the given backend and return a schema describing the data that has been written.
This method is mainly useful for debugging and to check cross-language interoperability.
§Safety
See the trait documentation.
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.
Implementors§
impl<T: SerInner<SerType: TypeHash + AlignHash>> Serialize for T
Blanket implementation that prevents the user from overwriting the
methods in Serialize.
This implementation writes a header containing a magic cookie, some hashes and debug information and then delegates to WriteWithNames::write.
§Implementation Notes
Note the bound on the serialization type or T: we need to be able to
compute type and alignment hashes for it. We could bound the serialization
type itself in the definition of SerInner, but having the bound here
instead gives us more flexibility and makes the implementation of
Owned easier.