Trait scroll::ctx::TryIntoCtx

source ·
pub trait TryIntoCtx<Ctx: Copy = (), This: ?Sized = [u8]>: Sized {
    type Error;

    // Required method
    fn try_into_ctx(self, _: &mut This, ctx: Ctx) -> Result<usize, Self::Error>;
}
Expand description

Tries to write Self into This using the context Ctx To implement writing into an arbitrary byte buffer, implement TryIntoCtx

Example

#[cfg(feature = "std")] {
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();

Required Associated Types§

Required Methods§

source

fn try_into_ctx(self, _: &mut This, ctx: Ctx) -> Result<usize, Self::Error>

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl TryIntoCtx for CString

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> Result<usize>

source§

impl TryIntoCtx<Endian> for f32
where f32: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for f64
where f64: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for i8
where i8: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for i16
where i16: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for i32
where i32: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for i64
where i64: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for i128

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for u8
where u8: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for u16
where u16: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for u32
where u32: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for u64
where u64: IntoCtx<Endian>,

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl TryIntoCtx<Endian> for u128

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx for &'a str

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> Result<usize>

source§

impl<'a> TryIntoCtx for &'a CStr

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> Result<usize>

source§

impl<'a> TryIntoCtx for &'a [u8]

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a f32

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a f64

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a i8

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a i16

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a i32

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a i64

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a i128

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a u8

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a u16

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a u32

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a u64

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<'a> TryIntoCtx<Endian> for &'a u128

§

type Error = Error

source§

fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> Result<usize>

source§

impl<Ctx: Copy, T: TryIntoCtx<Ctx, Error = Error>, const N: usize> TryIntoCtx<Ctx> for [T; N]

§

type Error = Error

source§

fn try_into_ctx(self, buf: &mut [u8], ctx: Ctx) -> Result<usize, Self::Error>

Implementors§