Trait scroll::Cwrite[][src]

pub trait Cwrite<Ctx: Copy, I = usize>: Index<I> + IndexMut<RangeFrom<I>> {
    fn cwrite<N: IntoCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>(
        &mut self,
        n: N,
        offset: I
    )
    where
        Ctx: Default
, { ... }
fn cwrite_with<N: IntoCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>(
        &mut self,
        n: N,
        offset: I,
        ctx: Ctx
    ) { ... } }
Expand description

Core-write - core, no_std friendly trait for writing basic types into byte buffers. Cannot fail unless the buffer is too small, in which case an assert fires and the program panics. Similar to Cread, if your type implements IntoCtx then you can cwrite(your_type, offset).

Example

use scroll::{ctx, Cwrite};

#[repr(packed)]
struct Bar {
    foo: i32,
    bar: u32,
}

impl ctx::IntoCtx<scroll::Endian> for Bar {
    fn into_ctx(self, bytes: &mut [u8], ctx: scroll::Endian) {
        use scroll::Cwrite;
        bytes.cwrite_with(self.foo, 0, ctx);
        bytes.cwrite_with(self.bar, 4, ctx);
    }
}

let bar = Bar { foo: -1, bar: 0xdeadbeef };
let mut bytes = [0x0; 16];
bytes.cwrite::<Bar>(bar, 0);

Provided methods

Writes n into Self at offset; uses default context. For the primitive types, this will be the target machine’s endianness.

Example
use scroll::{Cwrite, Cread};
let mut bytes = [0x0; 16];
bytes.cwrite::<i64>(42, 0);
bytes.cwrite::<u32>(0xdeadbeef, 8);

assert_eq!(bytes.cread::<i64>(0), 42);
assert_eq!(bytes.cread::<u32>(8), 0xdeadbeef);

Writes n into Self at offset with ctx

Example
use scroll::{Cwrite, Cread, LE, BE};
let mut bytes = [0x0; 0x10];
bytes.cwrite_with::<i64>(42, 0, LE);
bytes.cwrite_with::<u32>(0xdeadbeef, 8, BE);
assert_eq!(bytes.cread_with::<i64>(0, LE), 42);
assert_eq!(bytes.cread_with::<u32>(8, LE), 0xefbeadde);

Implementors