Trait scroll::ctx::TryFromCtx [−][src]
Tries to read Self
from This
using the context Ctx
Implementing Your Own Reader
If you want to implement your own reader for a type Foo
from some kind of buffer (say
[u8]
), then you need to implement this trait
use scroll::{self, ctx, Pread}; #[derive(Debug, PartialEq, Eq)] pub struct Foo(u16); impl<'a> ctx::TryFromCtx<'a, scroll::Endian> for Foo { type Error = scroll::Error; fn try_from_ctx(this: &'a [u8], le: scroll::Endian) -> Result<(Self, usize), Self::Error> { if this.len() < 2 { return Err((scroll::Error::Custom("whatever".to_string())).into()) } let n = this.pread_with(0, le)?; Ok((Foo(n), 2)) } } let bytes: [u8; 4] = [0xde, 0xad, 0, 0]; let foo = bytes.pread_with::<Foo>(0, scroll::LE).unwrap(); assert_eq!(Foo(0xadde), foo); let foo2 = bytes.pread_with::<Foo>(0, scroll::BE).unwrap(); assert_eq!(Foo(0xdeadu16), foo2);
Advanced: Using Your Own Error in TryFromCtx
use scroll::{self, ctx, Pread}; use std::error; use std::fmt::{self, Display}; // make some kind of normal error which also can transformed from a scroll error #[derive(Debug)] pub struct ExternalError {} impl Display for ExternalError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "ExternalError") } } impl error::Error for ExternalError { fn description(&self) -> &str { "ExternalError" } fn cause(&self) -> Option<&error::Error> { None} } impl From<scroll::Error> for ExternalError { fn from(err: scroll::Error) -> Self { match err { _ => ExternalError{}, } } } #[derive(Debug, PartialEq, Eq)] pub struct Foo(u16); impl<'a> ctx::TryFromCtx<'a, scroll::Endian> for Foo { type Error = ExternalError; fn try_from_ctx(this: &'a [u8], le: scroll::Endian) -> Result<(Self, usize), Self::Error> { if this.len() <= 2 { return Err((ExternalError {}).into()) } let offset = &mut 0; let n = this.gread_with(offset, le)?; Ok((Foo(n), *offset)) } } let bytes: [u8; 4] = [0xde, 0xad, 0, 0]; let foo: Result<Foo, ExternalError> = bytes.pread(0);
Associated Types
Loading content...Required methods
Loading content...Implementations on Foreign Types
impl<'a> TryFromCtx<'a, Endian, [u8]> for u8 where
u8: FromCtx<Endian>,
[src]
u8: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for u8 where
u8: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
u8: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for i8 where
i8: FromCtx<Endian>,
[src]
i8: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for i8 where
i8: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
i8: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for u16 where
u16: FromCtx<Endian>,
[src]
u16: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for u16 where
u16: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
u16: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for i16 where
i16: FromCtx<Endian>,
[src]
i16: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for i16 where
i16: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
i16: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for u32 where
u32: FromCtx<Endian>,
[src]
u32: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for u32 where
u32: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
u32: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for i32 where
i32: FromCtx<Endian>,
[src]
i32: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for i32 where
i32: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
i32: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for u64 where
u64: FromCtx<Endian>,
[src]
u64: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for u64 where
u64: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
u64: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for i64 where
i64: FromCtx<Endian>,
[src]
i64: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for i64 where
i64: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
i64: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for u128 where
u128: FromCtx<Endian>,
[src]
u128: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for u128 where
u128: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
u128: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for i128 where
i128: FromCtx<Endian>,
[src]
i128: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a, T> TryFromCtx<'a, Endian, T> for i128 where
i128: FromCtx<Endian, T>,
T: AsRef<[u8]>,
[src]
i128: FromCtx<Endian, T>,
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for f32 where
f32: FromCtx<Endian>,
[src]
f32: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for f64 where
f64: FromCtx<Endian>,
[src]
f64: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, StrCtx, [u8]> for &'a str
[src]
type Error = Error
fn try_from_ctx(
src: &'a [u8],
ctx: StrCtx
) -> Result<(Self, usize), Self::Error>
[src]
src: &'a [u8],
ctx: StrCtx
) -> Result<(Self, usize), Self::Error>
Read a &str
from src
using delimiter
impl<'a, T> TryFromCtx<'a, StrCtx, T> for &'a str where
T: AsRef<[u8]>,
[src]
T: AsRef<[u8]>,
type Error = Error
fn try_from_ctx(src: &'a T, ctx: StrCtx) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, Endian, [u8]> for usize where
usize: FromCtx<Endian>,
[src]
usize: FromCtx<Endian>,
type Error = Error
fn try_from_ctx(src: &'a [u8], le: Endian) -> Result<(Self, usize), Self::Error>
[src]
impl<'a> TryFromCtx<'a, usize, [u8]> for &'a [u8]
[src]
type Error = Error
fn try_from_ctx(
src: &'a [u8],
size: usize
) -> Result<(Self, usize), Self::Error>
[src]
src: &'a [u8],
size: usize
) -> Result<(Self, usize), Self::Error>