Trait scroll::Lwrite
[−]
[src]
pub trait Lwrite<Ctx = Endian, E = Error>: Write where
Ctx: Copy + Default + Debug,
E: Debug, { fn lwrite<N: SizeWith<Ctx, Units = usize> + IntoCtx<Ctx>>(
&mut self,
n: N
) -> Result<()> { ... } fn lwrite_with<N: SizeWith<Ctx, Units = usize> + IntoCtx<Ctx>>(
&mut self,
n: N,
ctx: Ctx
) -> Result<()> { ... } }
An extension trait to std::io::Write
streams; this only serializes simple types, like u8
, i32
, f32
, usize
, etc.
To write custom types with a single lwrite::<YourType>
call, implement IntoCtx
and SizeWith
for YourType
.
Provided Methods
fn lwrite<N: SizeWith<Ctx, Units = usize> + IntoCtx<Ctx>>(
&mut self,
n: N
) -> Result<()>
&mut self,
n: N
) -> Result<()>
Writes the type N
into Self
, with the parsing context ctx
.
NB: this will panic if the type you're writing has a size greater than 256. Plans are to have this allocate in larger cases.
For the primitive numeric types, this will be at the host machine's endianness.
Example
use scroll::Lwrite; use std::io::Cursor; let mut bytes = [0x0u8; 4]; let mut bytes = Cursor::new(&mut bytes[..]); bytes.lwrite(0xdeadbeef as u32).unwrap(); assert_eq!(bytes.into_inner(), [0xef, 0xbe, 0xad, 0xde,]);
fn lwrite_with<N: SizeWith<Ctx, Units = usize> + IntoCtx<Ctx>>(
&mut self,
n: N,
ctx: Ctx
) -> Result<()>
&mut self,
n: N,
ctx: Ctx
) -> Result<()>
Writes the type N
into Self
, with the parsing context ctx
.
NB: this will panic if the type you're writing has a size greater than 256. Plans are to have this allocate in larger cases.
For the primitive numeric types, this will be at the host machine's endianness.
Example
use scroll::{Lwrite, LE, BE}; use std::io::{Write, Cursor}; let mut bytes = [0x0u8; 10]; let mut cursor = Cursor::new(&mut bytes[..]); cursor.write_all(b"hello").unwrap(); cursor.lwrite_with(0xdeadbeef as u32, BE).unwrap(); assert_eq!(cursor.into_inner(), [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xde, 0xad, 0xbe, 0xef, 0x0]);