Struct binrw::FilePtr

source ·
pub struct FilePtr<Ptr: IntoSeekFrom, T> {
    pub ptr: Ptr,
    pub value: T,
}
Expand description

A wrapper type which represents a layer of indirection within a file.

The pointer type Ptr is an offset to a value within the data stream, and the value type T is the value at that offset. Dereferencing a FilePtr yields the pointed-to value. When deriving BinRead, the offset directive can be used to adjust the offset before the pointed-to value is read.

FilePtr is not efficient when reading offset tables; see the module documentation for more information.

Examples

#[derive(BinRead)]
struct Test {
    indirect_value: FilePtr<u32, u8>
}

let test: Test = Cursor::new(b"\0\0\0\x08\0\0\0\0\xff").read_be().unwrap();
assert_eq!(test.indirect_value.ptr, 8);
assert_eq!(*test.indirect_value, 0xFF);

Example data mapped out:

          [pointer]           [value]
00000000: 0000 0008 0000 0000 ff                   ............

Fields§

§ptr: Ptr

The raw offset to the value.

§value: T

The pointed-to value.

Implementations§

source§

impl<Ptr, Value> FilePtr<Ptr, Value>where Ptr: IntoSeekFrom,

source

pub fn parse<Args, __BinrwGeneratedStreamT: Read + Seek>( reader: &mut __BinrwGeneratedStreamT, endian: Endian, args: FilePtrArgs<Args> ) -> BinResult<Value>where Ptr: for<'a> BinRead<Args<'a> = ()> + IntoSeekFrom, Value: for<'a> BinRead<Args<'a> = Args>,

Reads an offset, then seeks to and parses the pointed-to value using the BinRead implementation for Value. Returns the pointed-to value.

Errors

If reading fails, an Error variant will be returned.

source

pub fn parse_with<R, F, Args>( parser: F ) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Value>where R: Read + Seek, F: Fn(&mut R, Endian, Args) -> BinResult<Value>, Ptr: for<'a> BinRead<Args<'a> = ()> + IntoSeekFrom,

Creates a parser that reads an offset, then seeks to and parses the pointed-to value using the given parser function. Returns the pointed-to value.

Errors

If reading fails, an Error variant will be returned.

Examples
use binrw::FilePtr16;

#[derive(BinRead)]
struct Test {
    #[br(parse_with = FilePtr16::parse_with(read_u24))]
    value: u32
}

let mut data = binrw::io::Cursor::new(b"\x02\x00\x07\x0f\x10");
let result = Test::read_le(&mut data).unwrap();
assert_eq!(result.value, 0x100f07);
source

pub fn with<R, F, Args>( parser: F ) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Self>where R: Read + Seek, F: Fn(&mut R, Endian, Args) -> BinResult<Value>, Ptr: for<'a> BinRead<Args<'a> = ()> + IntoSeekFrom,

Creates a parser that reads an offset, then seeks to and parses the pointed-to value using the given parser function. Returns a FilePtr containing the offset and value.

Errors

If reading fails, an Error variant will be returned.

Examples
use binrw::FilePtr16;

#[derive(BinRead)]
struct Test {
    #[br(parse_with = FilePtr16::with(read_u24))]
    value: FilePtr16<u32>
}

let mut data = binrw::io::Cursor::new(b"\x02\x00\x07\x0f\x10");
let result = Test::read_le(&mut data).unwrap();
assert_eq!(result.value.ptr, 2);
assert_eq!(result.value.value, 0x100f07);
source

pub fn into_inner(self) -> Value

Consumes this object, returning the pointed-to value.

Trait Implementations§

source§

impl<Ptr, Value> BinRead for FilePtr<Ptr, Value>where Ptr: for<'a> BinRead<Args<'a> = ()> + IntoSeekFrom, Value: BinRead,

§

type Args<'a> = FilePtrArgs<<Value as BinRead>::Args<'a>>

The type used for the args parameter of read_args() and read_options(). Read more
source§

fn read_options<R: Read + Seek>( reader: &mut R, endian: Endian, args: Self::Args<'_> ) -> BinResult<Self>

Read Self from the reader using the given Endian and arguments. Read more
source§

fn read_be_args<R: Read + Seek>( reader: &mut R, args: Self::Args<'_> ) -> BinResult<Self>

Read Self from the reader, assuming big-endian byte order, using the given arguments. Read more
source§

fn read_le_args<R: Read + Seek>( reader: &mut R, args: Self::Args<'_> ) -> BinResult<Self>

Read Self from the reader, assuming little-endian byte order, using the given arguments. Read more
source§

fn read_ne_args<R: Read + Seek>( reader: &mut R, args: Self::Args<'_> ) -> BinResult<Self>

Read T from the reader, assuming native-endian byte order, using the given arguments. Read more
source§

impl<Ptr: Debug + IntoSeekFrom, T: Debug> Debug for FilePtr<Ptr, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Ptr, Value> Deref for FilePtr<Ptr, Value>where Ptr: IntoSeekFrom,

source§

fn deref(&self) -> &Self::Target

Dereferences the value stored by FilePtr.

Examples
use binrw::FilePtr16;

#[derive(BinRead)]
struct Test {
    value: FilePtr16<u16>
}

let mut data = binrw::io::Cursor::new(b"\x02\x00\x01\x00");
let result = Test::read_le(&mut data).unwrap();
assert_eq!(result.value.ptr, 2);
assert_eq!(result.value.value, 1);
assert_eq!(*result.value, 1);
§

type Target = Value

The resulting type after dereferencing.
source§

impl<Ptr, Value> DerefMut for FilePtr<Ptr, Value>where Ptr: IntoSeekFrom,

source§

fn deref_mut(&mut self) -> &mut Value

Mutably dereferences the value stored by FilePtr.

Examples
use binrw::FilePtr16;

#[derive(BinRead)]
struct Test {
    value: FilePtr16<u16>
}

let mut data = binrw::io::Cursor::new(b"\x02\x00\x01\x00");
let mut result = Test::read_le(&mut data).unwrap();
assert_eq!(result.value.ptr, 2);
assert_eq!(result.value.value, 1);
*result.value = 42;
assert_eq!(result.value.value, 42);
source§

impl<Ptr, Value> PartialEq for FilePtr<Ptr, Value>where Ptr: IntoSeekFrom, Value: PartialEq,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Ptr: Eq + IntoSeekFrom, T: Eq> Eq for FilePtr<Ptr, T>

source§

impl<Ptr: IntoSeekFrom, T> StructuralEq for FilePtr<Ptr, T>

Auto Trait Implementations§

§

impl<Ptr, T> RefUnwindSafe for FilePtr<Ptr, T>where Ptr: RefUnwindSafe, T: RefUnwindSafe,

§

impl<Ptr, T> Send for FilePtr<Ptr, T>where Ptr: Send, T: Send,

§

impl<Ptr, T> Sync for FilePtr<Ptr, T>where Ptr: Sync, T: Sync,

§

impl<Ptr, T> Unpin for FilePtr<Ptr, T>where Ptr: Unpin, T: Unpin,

§

impl<Ptr, T> UnwindSafe for FilePtr<Ptr, T>where Ptr: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.