Skip to content

Commit

Permalink
add impl example for read_options
Browse files Browse the repository at this point in the history
  • Loading branch information
ScanMountGoat authored and jam1garner committed Apr 30, 2024
1 parent 4726598 commit 5a63ecd
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions binrw/src/binread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,38 @@ pub trait BinRead: Sized {
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
///
/// # Examples
///
/// ```
/// # use binrw::{BinRead, BinResult};
/// # use binrw::io::{Read, Seek, SeekFrom};
/// struct CustomPtr32<T>(T);
///
/// impl<T> BinRead for CustomPtr32<T>
/// where
/// for<'a> T: BinRead<Args<'a> = ()>,
/// {
/// type Args<'a> = u64;
///
/// fn read_options<R: Read + Seek>(
/// reader: &mut R,
/// endian: binrw::Endian,
/// args: Self::Args<'_>,
/// ) -> BinResult<Self> {
/// let offset = u32::read_options(reader, endian, ())?;
/// let saved_position = reader.stream_position()?;
///
/// // Read from an offset with a provided base offset.
/// reader.seek(SeekFrom::Start(args + offset as u64))?;
/// let value = T::read_options(reader, endian, ())?;
///
/// reader.seek(SeekFrom::Start(saved_position))?;
///
/// Ok(CustomPtr32(value))
/// }
/// }
/// ```
fn read_options<R: Read + Seek>(
reader: &mut R,
endian: Endian,
Expand Down

0 comments on commit 5a63ecd

Please sign in to comment.