Trait scroll::ctx::TryFromCtx[][src]

pub trait TryFromCtx<'a, Ctx: Copy = (), This: ?Sized = [u8]> where
    Self: 'a + Sized
{ type Error; fn try_from_ctx(
        from: &'a This,
        ctx: Ctx
    ) -> Result<(Self, usize), Self::Error>; }
[]

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

type Error[src]

Required methods

fn try_from_ctx(from: &'a This, ctx: Ctx) -> Result<(Self, usize), Self::Error>[src]

Implementations on Foreign Types

impl<'a> TryFromCtx<'a, Endian, [u8]> for u8 where
    u8: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for u8 where
    u8: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for i8 where
    i8: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for i8 where
    i8: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for u16 where
    u16: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for u16 where
    u16: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for i16 where
    i16: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for i16 where
    i16: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for u32 where
    u32: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for u32 where
    u32: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for i32 where
    i32: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for i32 where
    i32: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for u64 where
    u64: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for u64 where
    u64: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for i64 where
    i64: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for i64 where
    i64: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for u128 where
    u128: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for u128 where
    u128: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for i128 where
    i128: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a, T> TryFromCtx<'a, Endian, T> for i128 where
    i128: FromCtx<Endian, T>,
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for f32 where
    f32: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for f64 where
    f64: FromCtx<Endian>, 
[src][]

type Error = Error

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][]

Read a &str from src using delimiter

impl<'a, T> TryFromCtx<'a, StrCtx, T> for &'a str where
    T: AsRef<[u8]>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, Endian, [u8]> for usize where
    usize: FromCtx<Endian>, 
[src][]

type Error = Error

impl<'a> TryFromCtx<'a, usize, [u8]> for &'a [u8][src][]

type Error = Error

impl<'a> TryFromCtx<'a, (), [u8]> for &'a CStr[src][]

type Error = Error

impl<'a> TryFromCtx<'a, (), [u8]> for CString[src][]

type Error = Error

Implementors

impl<'a> TryFromCtx<'a, (), [u8]> for Sleb128[src][+]

impl<'a> TryFromCtx<'a, (), [u8]> for Uleb128[src][+]

impl<'a> TryFromCtx<'a, Endian, [u8]> for CompressionHeader where
    CompressionHeader: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for CompressionHeader where
    CompressionHeader: 'a, 

impl<'a> TryFromCtx<'a, Ctx, [u8]> for CompressionHeader

impl<'a> TryFromCtx<'a, Endian, [u8]> for Header

impl<'a> TryFromCtx<'a, Endian, [u8]> for Header

impl<'a> TryFromCtx<'a, Endian, [u8]> for Header

impl<'a> TryFromCtx<'a, Ctx, [u8]> for ProgramHeader

impl<'a> TryFromCtx<'a, Endian, [u8]> for ProgramHeader where
    ProgramHeader: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for ProgramHeader where
    ProgramHeader: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SectionHeader where
    SectionHeader: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SectionHeader where
    SectionHeader: 'a, 

impl<'a> TryFromCtx<'a, Ctx, [u8]> for SectionHeader

impl<'a> TryFromCtx<'a, Endian, [u8]> for Sym where
    Sym: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Sym where
    Sym: 'a, 

impl<'a> TryFromCtx<'a, Ctx, [u8]> for Sym

impl<'a> TryFromCtx<'a, Ctx, [u8]> for Dyn

impl<'a> TryFromCtx<'a, Endian, [u8]> for Dyn where
    Dyn: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Dyn where
    Dyn: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Rela where
    Rela: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Rel where
    Rel: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Rela where
    Rela: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Rel where
    Rel: 'a, 

impl<'a> TryFromCtx<'a, (bool, Ctx), [u8]> for Reloc

impl<'a> TryFromCtx<'a, Endian, [u8]> for Nhdr32 where
    Nhdr32: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Nhdr64 where
    Nhdr64: 'a, 

impl<'a> TryFromCtx<'a, (usize, Ctx), [u8]> for Note<'a>

impl<'a> TryFromCtx<'a, (usize, Endian), [u8]> for Elf<'a>

impl<'a> TryFromCtx<'a, Endian, [u8]> for FatHeader where
    FatHeader: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for FatArch where
    FatArch: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Header32 where
    Header32: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Header64 where
    Header64: 'a, 

impl<'a> TryFromCtx<'a, Ctx, [u8]> for Header

impl<'a> TryFromCtx<'a, Endian, [u8]> for LoadCommandHeader where
    LoadCommandHeader: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Section32 where
    Section32: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Section64 where
    Section64: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SegmentCommand32 where
    SegmentCommand32: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SegmentCommand64 where
    SegmentCommand64: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Fvmlib where
    Fvmlib: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for FvmlibCommand where
    FvmlibCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Dylib where
    Dylib: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DylibCommand where
    DylibCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SubFrameworkCommand where
    SubFrameworkCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SubClientCommand where
    SubClientCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SubUmbrellaCommand where
    SubUmbrellaCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SubLibraryCommand where
    SubLibraryCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for PreboundDylibCommand where
    PreboundDylibCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DylinkerCommand where
    DylinkerCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for ThreadCommand

impl<'a> TryFromCtx<'a, Endian, [u8]> for RoutinesCommand32 where
    RoutinesCommand32: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for RoutinesCommand64 where
    RoutinesCommand64: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SymtabCommand where
    SymtabCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DysymtabCommand where
    DysymtabCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DylibTableOfContents where
    DylibTableOfContents: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DylibModule where
    DylibModule: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DylibModule64 where
    DylibModule64: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DylibReference where
    DylibReference: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for TwolevelHintsCommand where
    TwolevelHintsCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for TwolevelHint where
    TwolevelHint: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for PrebindCksumCommand where
    PrebindCksumCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for UuidCommand where
    UuidCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for RpathCommand where
    RpathCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for LinkeditDataCommand where
    LinkeditDataCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for EncryptionInfoCommand32 where
    EncryptionInfoCommand32: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for EncryptionInfoCommand64 where
    EncryptionInfoCommand64: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for VersionMinCommand where
    VersionMinCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DyldInfoCommand where
    DyldInfoCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for LinkerOptionCommand where
    LinkerOptionCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SymsegCommand where
    SymsegCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for IdentCommand where
    IdentCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for FvmfileCommand where
    FvmfileCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for EntryPointCommand where
    EntryPointCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for SourceVersionCommand where
    SourceVersionCommand: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DataInCodeEntry where
    DataInCodeEntry: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for CommandVariant

impl<'a> TryFromCtx<'a, Endian, [u8]> for RelocationInfo where
    RelocationInfo: 'a, 

impl<'a> TryFromCtx<'a, Ctx, [u8]> for Section

impl<'a> TryFromCtx<'a, Endian, [u8]> for Nlist32 where
    Nlist32: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Nlist64 where
    Nlist64: 'a, 

impl<'a> TryFromCtx<'a, Ctx, [u8]> for Nlist

impl<'a, T: ?Sized> TryFromCtx<'a, SymbolsCtx, T> for Symbols<'a> where
    T: AsRef<[u8]>, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for DataDirectory where
    DataDirectory: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for ImageDebugDirectory where
    ImageDebugDirectory: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for RuntimeFunction where
    RuntimeFunction: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for ExportDirectoryTable where
    ExportDirectoryTable: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Reexport<'a>

impl<'a> TryFromCtx<'a, Endian, [u8]> for CoffHeader where
    CoffHeader: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for ImportDirectoryEntry where
    ImportDirectoryEntry: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for StandardFields32 where
    StandardFields32: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for StandardFields64 where
    StandardFields64: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for WindowsFields32 where
    WindowsFields32: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for WindowsFields64 where
    WindowsFields64: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for OptionalHeader

impl<'a> TryFromCtx<'a, Endian, [u8]> for Relocation where
    Relocation: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for Symbol where
    Symbol: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for AuxFunctionDefinition where
    AuxFunctionDefinition: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for AuxBeginAndEndFunction where
    AuxBeginAndEndFunction: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for AuxWeakExternal where
    AuxWeakExternal: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for AuxSectionDefinition where
    AuxSectionDefinition: 'a, 

impl<'a> TryFromCtx<'a, Endian, [u8]> for MemberHeader where
    MemberHeader: 'a,