Encode

Trait Encode 

Source
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§

Source

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Converts the type into a list of Felt and append them into the writer.

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.

Implementations on Foreign Types§

Source§

impl Encode for bool

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Source§

impl Encode for i128

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Source§

impl Encode for u8

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Source§

impl Encode for u16

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Source§

impl Encode for u32

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Source§

impl Encode for u64

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Source§

impl Encode for u128

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Source§

impl<T> Encode for Option<T>
where T: Encode,

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Source§

impl<T> Encode for [T]
where T: Encode,

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Source§

impl<T, const N: usize> Encode for [T; N]
where T: Encode,

Source§

fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error>

Implementors§

Source§

impl Encode for ByteArray

Source§

impl Encode for Felt

Source§

impl Encode for U256

Source§

impl<T> Encode for Vec<T>
where T: Encode,