Packable

Trait Packable 

Source
pub trait Packable {
    // Required methods
    fn pack_sz(&self) -> usize;
    fn pack(&self, out: &mut [u8]);

    // Provided method
    fn stream<W: Write>(&self, writer: &mut W) -> Result<usize, Error>
       where Self: Sized,
             for<'a> &'a Self: Packable { ... }
}
Expand description

Packable objects can be serialized into an &mut [u8].

The actual serialized form of the object is left unspecified by the Packable trait.

Packable objects should avoid interior mutability to the extent necessary to ensure that anyone holding a immutable reference can assume the packed output will not change for the duration of the reference.

Required Methods§

Source

fn pack_sz(&self) -> usize

pack_sz returns the number of bytes required to serialize the Packable object.

Source

fn pack(&self, out: &mut [u8])

pack fills in the buffer out with the packed binary representation of the Packable object. The implementor is responsible to ensure that out is exactly pack_sz() bytes and implementations are encouraged to assert this.

The call to pack should never fail. Good Rust practices dictate that objects should use the type system to enforce their well-formed-ness. Consequently, we will assume here that any well-formed Packable that can be represented will serialize successfully. If there is a need to represent a state that cannot exist, it should be done using a different type that does not implement Packable.

§Panics
  • When out.len() != self.pack_sz()

Provided Methods§

Source

fn stream<W: Write>(&self, writer: &mut W) -> Result<usize, Error>
where Self: Sized, for<'a> &'a Self: Packable,

stream writes the object to the provided writer using the same representation that would be used in a call to pack. The implementor is responsible for making sure that the number of bytes written is exactly equal to the number of required bytes.

A default implementation is provided that will pack a vector and then write said vector to the file with write_all.

Implementations on Foreign Types§

Source§

impl Packable for &[u8]

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for char

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for f32

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for f64

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for i8

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for i16

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for i32

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for i64

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for u8

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for u16

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for u32

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for u64

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl Packable for ()

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, _: &mut [u8])

Source§

impl<A: Packable> Packable for (A,)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable> Packable for (A, B)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable> Packable for (A, B, C)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable> Packable for (A, B, C, D)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable> Packable for (A, B, C, D, E)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable> Packable for (A, B, C, D, E, F)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable> Packable for (A, B, C, D, E, F, G)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable> Packable for (A, B, C, D, E, F, G, H)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable> Packable for (A, B, C, D, E, F, G, H, I)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable> Packable for (A, B, C, D, E, F, G, H, I, J)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable, R: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable, R: Packable, S: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable, R: Packable, S: Packable, T: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable, R: Packable, S: Packable, T: Packable, U: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable, R: Packable, S: Packable, T: Packable, U: Packable, V: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable, R: Packable, S: Packable, T: Packable, U: Packable, V: Packable, W: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable, R: Packable, S: Packable, T: Packable, U: Packable, V: Packable, W: Packable, X: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable, R: Packable, S: Packable, T: Packable, U: Packable, V: Packable, W: Packable, X: Packable, Y: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<A: Packable, B: Packable, C: Packable, D: Packable, E: Packable, F: Packable, G: Packable, H: Packable, I: Packable, J: Packable, K: Packable, L: Packable, M: Packable, N: Packable, O: Packable, P: Packable, Q: Packable, R: Packable, S: Packable, T: Packable, U: Packable, V: Packable, W: Packable, X: Packable, Y: Packable, Z: Packable> Packable for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z)

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, buf: &mut [u8])

Source§

impl<P: Packable> Packable for &P

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Source§

impl<T: Packable, E: Packable> Packable for Result<T, E>

Source§

fn pack_sz(&self) -> usize

Source§

fn pack(&self, out: &mut [u8])

Implementors§

Source§

impl Packable for v64

Source§

impl<'a, P> Packable for LengthPrefixer<'a, P>
where P: Packable + 'a,

Source§

impl<'a, P, T> Packable for StackPacker<'a, P, T>
where P: Packable + 'a, T: Packable + 'a,

Source§

impl<P: Packable> Packable for LengthFree<'_, P>