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,
impl<Ptr, Value> FilePtr<Ptr, Value>where
Ptr: IntoSeekFrom,
sourcepub fn parse<Args, __BinrwGeneratedStreamT: Read + Seek>(
reader: &mut __BinrwGeneratedStreamT,
endian: Endian,
args: FilePtrArgs<Args>,
) -> BinResult<Value>
pub fn parse<Args, __BinrwGeneratedStreamT: Read + Seek>( reader: &mut __BinrwGeneratedStreamT, endian: Endian, args: FilePtrArgs<Args>, ) -> BinResult<Value>
sourcepub fn parse_with<R, F, Args>(
parser: F,
) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Value>
pub fn parse_with<R, F, Args>( parser: F, ) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Value>
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);
sourcepub fn with<R, F, Args>(
parser: F,
) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Self>
pub fn with<R, F, Args>( parser: F, ) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Self>
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);
sourcepub fn into_inner(self) -> Value
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>
impl<Ptr, Value> BinRead for FilePtr<Ptr, Value>
§type Args<'a> = FilePtrArgs<<Value as BinRead>::Args<'a>>
type Args<'a> = FilePtrArgs<<Value as BinRead>::Args<'a>>
source§fn read_options<R: Read + Seek>(
reader: &mut R,
endian: Endian,
args: Self::Args<'_>,
) -> BinResult<Self>
fn read_options<R: Read + Seek>( reader: &mut R, endian: Endian, args: Self::Args<'_>, ) -> BinResult<Self>
source§fn read_be_args<R: Read + Seek>(
reader: &mut R,
args: Self::Args<'_>,
) -> BinResult<Self>
fn read_be_args<R: Read + Seek>( reader: &mut R, args: Self::Args<'_>, ) -> BinResult<Self>
Self
from the reader, assuming big-endian byte order, using the
given arguments. Read moresource§impl<Ptr, Value> Deref for FilePtr<Ptr, Value>where
Ptr: IntoSeekFrom,
impl<Ptr, Value> Deref for FilePtr<Ptr, Value>where
Ptr: IntoSeekFrom,
source§fn deref(&self) -> &Self::Target
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);
source§impl<Ptr, Value> DerefMut for FilePtr<Ptr, Value>where
Ptr: IntoSeekFrom,
impl<Ptr, Value> DerefMut for FilePtr<Ptr, Value>where
Ptr: IntoSeekFrom,
source§fn deref_mut(&mut self) -> &mut Value
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);