pub trait Encode {
// Required method
fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>;
}
Expand description
Any type that can be serialized into a series of Felts. This trait corresponds to the
serialize
function of the Cairo Serde
trait.
This trait can be derived as long as all the fields in type implement Encode
.
§Example
This example demonstrates deriving the trait and then using it to serialize an instance into
Vec<Felt>
.
use starknet_core::codec::Encode;
#[derive(Encode)]
struct CairoType {
a: u32,
b: Option<bool>,
}
let instance = CairoType {
a: 3,
b: Some(true),
};
let mut serialized = vec![];
instance.encode(&mut serialized);
assert_eq!(vec![Felt::THREE, Felt::ZERO, Felt::ONE], serialized);
Required Methods§
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.