pub trait Formula {
const MAX_STACK_SIZE: Option<usize>;
const EXACT_SIZE: bool;
const HEAPLESS: bool;
}Expand description
Trait for data formulas.
Types that implement this trait are used as markers
to guide serialization and deserialization process.
Many types that implement BareFormula
implement Serialize and/or Deserialize traits
with Self as formula type.
The typical exceptions are lazily serialized and deserialize types.
For example [T] can be used as formula for which iterators
implement Serialize trait.
And SliceIter and FromIterator containers implement Deserialize trait.
Similarly structures that contain [T] may be serialized
from structures with identical layout but iterator for that field.
Users may derive(BareFormula) for their types, structures and enums.
Then derive(Serialize) and derive(Deserialize)
will use formula structure to implement serialization and deserialization.
Fields of formula structure must be visible in scope of type where
derive(Serialize) and derive(Deserialize) is used.
Additionally for each field of the serialization/deserialization structure there must be field in formula. And all field of formula structure must be used. Otherwise derive macro will generate compile error.
Structures can be used to serialize into enum formula. In this case specific variant is used and layout of that variant must match layout of serialization structure.
Users are also free to implement BareFormula and other traits manually.
In this case they are encouraged to pay attention to BareFormula documentation.
And provide implementations for Serialize and Deserialize traits
with this formula.
For use-cases outside defining new primitives users are encouraged to use derive macros.
Implementing traits incorrectly may result in wrong content of serialized data and deserialized values. It can’t result in undefined behavior.
Examples
struct MyFormula;
impl Formula for MyFormula {
const MAX_STACK_SIZE: Option<usize> = Some(0);
const EXACT_SIZE: bool = true;
const HEAPLESS: bool = true;
}Required Associated Constants§
sourceconst MAX_STACK_SIZE: Option<usize>
const MAX_STACK_SIZE: Option<usize>
Maximum size of stack this formula occupies.
sourceconst EXACT_SIZE: bool
const EXACT_SIZE: bool
Signals that MAX_STACK_SIZE is accurate.