Trait scroll::IOwrite [−][src]
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 iowrite::<YourType>
call, implement IntoCtx
and SizeWith
for YourType
.
Provided methods
fn iowrite<N: SizeWith<Ctx> + IntoCtx<Ctx>>(&mut self, n: N) -> Result<()> where
Ctx: Default,
[src]
Ctx: Default,
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::IOwrite; use std::io::Cursor; let mut bytes = [0x0u8; 4]; let mut bytes = Cursor::new(&mut bytes[..]); bytes.iowrite(0xdeadbeef as u32).unwrap(); #[cfg(target_endian = "little")] assert_eq!(bytes.into_inner(), [0xef, 0xbe, 0xad, 0xde,]); #[cfg(target_endian = "big")] assert_eq!(bytes.into_inner(), [0xde, 0xad, 0xbe, 0xef,]);
fn iowrite_with<N: SizeWith<Ctx> + IntoCtx<Ctx>>(
&mut self,
n: N,
ctx: Ctx
) -> Result<()>
[src]
&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::{IOwrite, 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.iowrite_with(0xdeadbeef as u32, BE).unwrap(); assert_eq!(cursor.into_inner(), [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xde, 0xad, 0xbe, 0xef, 0x0]);
Implementors
impl<Ctx: Copy, W: Write + ?Sized> IOwrite<Ctx> for W
[src]
Types that implement Write
get methods defined in IOwrite
for free.