Trait scroll::ctx::TryIntoCtx [−][src]
Tries to write Self
into This
using the context Ctx
To implement writing into an arbitrary byte buffer, implement TryIntoCtx
Example
use scroll::{self, ctx, LE, Endian, Pwrite}; #[derive(Debug, PartialEq, Eq)] pub struct Foo(u16); // this will use the default `DefaultCtx = scroll::Endian` impl ctx::TryIntoCtx<Endian> for Foo { // you can use your own error here too, but you will then need to specify it in fn generic parameters type Error = scroll::Error; // you can write using your own context type, see `leb128.rs` fn try_into_ctx(self, this: &mut [u8], le: Endian) -> Result<usize, Self::Error> { if this.len() < 2 { return Err((scroll::Error::Custom("whatever".to_string())).into()) } this.pwrite_with(self.0, 0, le)?; Ok(2) } } // now we can write a `Foo` into some buffer (in this case, a byte buffer, because that's what we implemented it for above) let mut bytes: [u8; 4] = [0, 0, 0, 0]; bytes.pwrite_with(Foo(0x7f), 1, LE).unwrap();
Associated Types
Loading content...Required methods
Loading content...Implementations on Foreign Types
impl TryIntoCtx<Endian, [u8]> for u8 where
u8: IntoCtx<Endian>,
[src]
u8: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a u8
[src]
impl TryIntoCtx<Endian, [u8]> for i8 where
i8: IntoCtx<Endian>,
[src]
i8: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a i8
[src]
impl TryIntoCtx<Endian, [u8]> for u16 where
u16: IntoCtx<Endian>,
[src]
u16: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a u16
[src]
impl TryIntoCtx<Endian, [u8]> for i16 where
i16: IntoCtx<Endian>,
[src]
i16: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a i16
[src]
impl TryIntoCtx<Endian, [u8]> for u32 where
u32: IntoCtx<Endian>,
[src]
u32: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a u32
[src]
impl TryIntoCtx<Endian, [u8]> for i32 where
i32: IntoCtx<Endian>,
[src]
i32: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a i32
[src]
impl TryIntoCtx<Endian, [u8]> for u64 where
u64: IntoCtx<Endian>,
[src]
u64: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a u64
[src]
impl TryIntoCtx<Endian, [u8]> for i64 where
i64: IntoCtx<Endian>,
[src]
i64: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a i64
[src]
impl TryIntoCtx<Endian, [u8]> for u128 where
u128: IntoCtx<Endian>,
[src]
u128: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a u128
[src]
impl TryIntoCtx<Endian, [u8]> for i128 where
i128: IntoCtx<Endian>,
[src]
i128: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a i128
[src]
impl TryIntoCtx<Endian, [u8]> for f32 where
f32: IntoCtx<Endian>,
[src]
f32: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a f32
[src]
impl TryIntoCtx<Endian, [u8]> for f64 where
f64: IntoCtx<Endian>,
[src]
f64: IntoCtx<Endian>,
impl<'a> TryIntoCtx<Endian, [u8]> for &'a f64
[src]
impl<'a> TryIntoCtx<(), [u8]> for &'a [u8]
[src]
impl<'a> TryIntoCtx<(), [u8]> for &'a str
[src]
impl TryIntoCtx<Endian, [u8]> for usize where
usize: IntoCtx<Endian>,
[src]
usize: IntoCtx<Endian>,