Skip to content

Commit

Permalink
Move offset for FilePtr from ReadOptions to named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
csnover committed Sep 12, 2022
1 parent 078440d commit bd7d8ea
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 209 deletions.
2 changes: 1 addition & 1 deletion binrw/doc/attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ The `offset` and `offset_after` directives are shorthands for passing
When manually implementing
[`BinRead::read_options`](crate::BinRead::read_options) or a
[custom parser function](#custom-parserswriters), the offset is accessible
from [`ReadOptions::offset`](crate::ReadOptions::offset).
from a named argument named `offset`.

For `offset`, any earlier field or [import](#arguments) can be referenced by
the expression in the directive.
Expand Down
27 changes: 4 additions & 23 deletions binrw/src/binread/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@ pub struct ReadOptions {
/// [byte order directives](crate::docs::attribute#byte-order), this option
/// will be overridden by the directive.
endian: Endian,

/// An absolute offset added to the [`FilePtr::ptr`](crate::FilePtr::ptr)
/// offset before reading the pointed-to value.
offset: u64,
}

impl ReadOptions {
/// Creates a new `ReadOptions` with the given [endianness](crate::Endian).
#[must_use]
pub fn new(endian: Endian) -> Self {
Self {
endian,
offset: <_>::default(),
}
Self { endian }
}

/// The [byte order](crate::Endian) to use when reading data.
Expand All @@ -38,24 +31,12 @@ impl ReadOptions {
self.endian
}

/// An absolute offset added to the [`FilePtr::ptr`](crate::FilePtr::ptr)
/// offset before reading the pointed-to value.
#[must_use]
pub fn offset(&self) -> u64 {
self.offset
}

/// Creates a copy of this `ReadOptions` using the given
/// [endianness](crate::Endian).
#[must_use]
// Lint: API compatibility.
#[allow(clippy::unused_self)]
pub fn with_endian(self, endian: Endian) -> Self {
Self { endian, ..self }
}

/// Creates a copy of this `ReadOptions` using the given
/// [offset](crate::docs::attribute#offset).
#[must_use]
pub fn with_offset(self, offset: u64) -> Self {
Self { offset, ..self }
Self { endian }
}
}
Loading

0 comments on commit bd7d8ea

Please sign in to comment.