Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add impl example for read_options #246

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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