pub struct FilePtr<Ptr: IntoSeekFrom, T> {
pub ptr: Ptr,
pub value: Option<T>,
}
Expand description
A wrapper type which represents a layer of indirection within a file.
FilePtr<P, T>
is composed of two types. The pointer type P
is the
absolute offset to a value within the data, and the value type T
is
the actual pointed-to value. Once a FilePtr
has been
finalized, dereferencing it will yield the
pointed-to value.
When deriving BinRead
, offset directives
can be used to adjust the offset before the pointed-to value is read.
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: Option<T>
The pointed-to value.
Implementations§
source§impl<Ptr, Value> FilePtr<Ptr, Value>where
Ptr: for<'a> BinRead<Args<'a> = ()> + IntoSeekFrom,
impl<Ptr, Value> FilePtr<Ptr, Value>where Ptr: for<'a> BinRead<Args<'a> = ()> + IntoSeekFrom,
sourcepub fn parse<Args, __BinrwGeneratedStreamT: Read + Seek>(
reader: &mut __BinrwGeneratedStreamT,
endian: Endian,
args: FilePtrArgs<Args>
) -> BinResult<Value>where
Args: Clone,
Value: for<'a> BinRead<Args<'a> = Args>,
pub fn parse<Args, __BinrwGeneratedStreamT: Read + Seek>( reader: &mut __BinrwGeneratedStreamT, endian: Endian, args: FilePtrArgs<Args> ) -> BinResult<Value>where Args: Clone, Value: for<'a> BinRead<Args<'a> = Args>,
Custom parser for use with the
parse_with
directive that reads
and then immediately finalizes a FilePtr
, returning the pointed-to
value as the result.
Errors
If reading fails, an Error
variant will be returned.
sourcepub fn parse_with<R, F, Args>(
parser: F
) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Value>where
R: Read + Seek,
Args: Clone,
F: Fn(&mut R, Endian, Args) -> BinResult<Value>,
pub fn parse_with<R, F, Args>( parser: F ) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Value>where R: Read + Seek, Args: Clone, F: Fn(&mut R, Endian, Args) -> BinResult<Value>,
Custom parser for use with the
parse_with
directive that reads and then
immediately finalizes a FilePtr
using the specified parser, returning the pointed-to
value as the result.
Errors
If reading fails, an Error
variant will be returned.
sourcepub fn with<R, F, Args>(
parser: F
) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Self>where
R: Read + Seek,
Args: Clone,
F: Fn(&mut R, Endian, Args) -> BinResult<Value>,
pub fn with<R, F, Args>( parser: F ) -> impl Fn(&mut R, Endian, FilePtrArgs<Args>) -> BinResult<Self>where R: Read + Seek, Args: Clone, F: Fn(&mut R, Endian, Args) -> BinResult<Value>,
Custom parser for use with the
parse_with
directive that reads and then
immediately finalizes a FilePtr
using the specified parser, returning the FilePtr
as the result.
Errors
If reading fails, an Error
variant will be returned.
sourcepub fn into_inner(self) -> Value
pub fn into_inner(self) -> Value
Consumes this object, returning the pointed-to value.
Panics
Will panic if FilePtr
hasn’t been finalized by calling
after_parse()
.
Trait Implementations§
source§impl<Ptr, Value> BinRead for FilePtr<Ptr, Value>where
Ptr: for<'a> BinRead<Args<'a> = ()> + IntoSeekFrom,
Value: BinRead,
for<'a> Value::Args<'a>: Clone,
impl<Ptr, Value> BinRead for FilePtr<Ptr, Value>where Ptr: for<'a> BinRead<Args<'a> = ()> + IntoSeekFrom, Value: BinRead, for<'a> Value::Args<'a>: Clone,
source§fn read_options<R: Read + Seek>(
reader: &mut R,
endian: Endian,
_: Self::Args<'_>
) -> BinResult<Self>
fn read_options<R: Read + Seek>( reader: &mut R, endian: Endian, _: Self::Args<'_> ) -> BinResult<Self>
Reads the offset of the value from the reader.
The actual value will not be read until
after_parse()
is called.
source§fn after_parse<R>(
&mut self,
reader: &mut R,
endian: Endian,
args: Self::Args<'_>
) -> BinResult<()>where
R: Read + Seek,
fn after_parse<R>( &mut self, reader: &mut R, endian: Endian, args: Self::Args<'_> ) -> BinResult<()>where R: Read + Seek,
Finalizes the FilePtr
by seeking to and reading the pointed-to value.
§type Args<'a> = FilePtrArgs<<Value as BinRead>::Args<'a>>
type Args<'a> = FilePtrArgs<<Value as BinRead>::Args<'a>>
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: IntoSeekFrom, Value: BinRead> Deref for FilePtr<Ptr, Value>
impl<Ptr: IntoSeekFrom, Value: BinRead> Deref for FilePtr<Ptr, Value>
Dereferences the value.
Panics
Will panic if FilePtr
hasn’t been finalized by calling
after_parse()
.
source§impl<Ptr: IntoSeekFrom, Value: BinRead> DerefMut for FilePtr<Ptr, Value>
impl<Ptr: IntoSeekFrom, Value: BinRead> DerefMut for FilePtr<Ptr, Value>
Panics
Will panic if the FilePtr
has not been read yet using
BinRead::after_parse