diff --git a/binrw/doc/attribute.md b/binrw/doc/attribute.md
index b755efb9..91ad5543 100644
--- a/binrw/doc/attribute.md
+++ b/binrw/doc/attribute.md
@@ -643,13 +643,9 @@ within an object is:
1. A directive on a field
2. A directive on an enum variant
3. A directive on the struct or enum
-4. The [`endian`](crate::ReadOptions::endian) property of the
- [`ReadOptions`](crate::ReadOptions) object passed to
- [`BinRead::read_options`](crate::BinRead::read_options) by the caller
- The [`endian`](crate::WriteOptions::endian) property of the
- [`WriteOptions`](crate::WriteOptions) object passed to
- [`BinWrite::write_options`](crate::BinWrite::write_options) by the caller
-5. The host machine’s native byte order
+4. The `endian` parameter of the
+ [`BinRead::read_options`](crate::BinRead::read_options)
+ [`BinWrite::write_options`](crate::BinWrite::write_options) call
However, if a byte order directive is added to a struct or enum, that byte
order will *always* be used, even if the object is embedded in another
@@ -658,7 +654,7 @@ object or explicitly called with a different byte order:
```
-# use binrw::{Endian, ReadOptions, prelude::*, io::Cursor};
+# use binrw::{Endian, prelude::*, io::Cursor};
#[derive(BinRead)]
# #[derive(Debug, PartialEq)]
#[br(little)] // ← this *forces* the struct to be little-endian
@@ -671,16 +667,16 @@ struct Parent {
child: Child,
};
-let mut options = ReadOptions::new(Endian::Big /* ← this will be ignored */);
+let endian = Endian::Big; /* ← this will be ignored */
# assert_eq!(
-Parent::read_options(&mut Cursor::new(b"\x01\0\0\0"), &options, ())
+Parent::read_options(&mut Cursor::new(b"\x01\0\0\0"), endian, ())
# .unwrap(), Parent { child: Child(1) });
```
```
-# use binrw::{Endian, WriteOptions, prelude::*, io::Cursor};
+# use binrw::{Endian, prelude::*, io::Cursor};
#[derive(BinWrite)]
# #[derive(Debug, PartialEq)]
#[bw(little)] // ← this *forces* the struct to be little-endian
@@ -695,9 +691,9 @@ struct Parent {
let object = Parent { child: Child(1) };
-let mut options = WriteOptions::new(Endian::Big /* ← this will be ignored */);
+let endian = Endian::Big; /* ← this will be ignored */
let mut output = Cursor::new(vec![]);
-object.write_options(&mut output, &options, ())
+object.write_options(&mut output, endian, ())
# .unwrap();
# assert_eq!(output.into_inner(), b"\x01\0\0\0");
```
@@ -706,8 +702,7 @@ object.write_options(&mut output, &options, ())
When manually implementing
[`BinRead::read_options`](crate::BinRead::read_options)[`BinWrite::write_options`](crate::BinWrite::write_options) or a
[custom
parserwriter function](#custom-parserswriters),
-the byte order is accessible from
-
[`ReadOptions::endian`](crate::ReadOptions::endian)[`WriteOptions::endian`](crate::WriteOptions::endian).
+the byte order is accessible from the `endian` parameter.
## Examples
@@ -1098,15 +1093,15 @@ calling a function generator).
### Using a custom parser to generate a [`HashMap`](std::collections::HashMap)
```
-# use binrw::{prelude::*, io::{prelude::*, Cursor}, ReadOptions};
+# use binrw::{prelude::*, io::{prelude::*, Cursor}, Endian};
# use std::collections::HashMap;
-fn custom_parser
(reader: &mut R, ro: &ReadOptions, _: ())
+fn custom_parser(reader: &mut R, endian: Endian, _: ())
-> BinResult>
{
let mut map = HashMap::new();
map.insert(
- <_>::read_options(reader, ro, ())?,
- <_>::read_options(reader, ro, ())?,
+ <_>::read_options(reader, endian, ())?,
+ <_>::read_options(reader, endian, ())?,
);
Ok(map)
}
@@ -1126,17 +1121,17 @@ struct MyType {
### Using a custom serialiser to write a [`BTreeMap`](std::collections::BTreeMap)
```
-# use binrw::{prelude::*, io::{prelude::*, Cursor}, WriteOptions};
+# use binrw::{prelude::*, io::{prelude::*, Cursor}, Endian};
# use std::collections::BTreeMap;
fn custom_writer(
map: &BTreeMap,
writer: &mut R,
- wo: &WriteOptions,
+ endian: Endian,
_: ()
) -> BinResult<()> {
for (key, val) in map.iter() {
- key.write_options(writer, wo, ())?;
- val.write_options(writer, wo, ())?;
+ key.write_options(writer, endian, ())?;
+ val.write_options(writer, endian, ())?;
}
Ok(())
}
diff --git a/binrw/src/binread/impls.rs b/binrw/src/binread/impls.rs
index d5c9e4d4..45764f06 100644
--- a/binrw/src/binread/impls.rs
+++ b/binrw/src/binread/impls.rs
@@ -1,6 +1,6 @@
use crate::{
io::{self, Read, Seek, SeekFrom},
- BinRead, BinResult, Endian, Error, NamedArgs, ReadOptions,
+ BinRead, BinResult, Endian, Error, NamedArgs,
};
use alloc::{boxed::Box, vec::Vec};
use core::num::{
@@ -14,7 +14,7 @@ macro_rules! binread_impl {
impl BinRead for $type_name {
type Args = ();
- fn read_options(reader: &mut R, options: &ReadOptions, _: Self::Args) -> BinResult {
+ fn read_options(reader: &mut R, endian: Endian, _: Self::Args) -> BinResult {
let mut val = [0; core::mem::size_of::<$type_name>()];
let pos = reader.stream_position()?;
@@ -22,7 +22,7 @@ macro_rules! binread_impl {
reader.seek(SeekFrom::Start(pos))?;
Err(e)
})?;
- Ok(match options.endian() {
+ Ok(match endian {
Endian::Big => {
<$type_name>::from_be_bytes(val)
}
@@ -53,10 +53,10 @@ macro_rules! binread_nonzero_impl {
fn read_options(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
_: Self::Args,
) -> BinResult {
- match <$Ty>::new(<$Int>::read_options(reader, options, ())?) {
+ match <$Ty>::new(<$Int>::read_options(reader, endian, ())?) {
Some(x) => Ok(x),
None => Err(unexpected_zero_num()),
}
@@ -138,23 +138,18 @@ impl BinRead for Vec {
fn read_options(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult {
- crate::helpers::count_with(args.count, B::read_options)(reader, options, args.inner)
+ crate::helpers::count_with(args.count, B::read_options)(reader, endian, args.inner)
}
- fn after_parse(
- &mut self,
- reader: &mut R,
- ro: &ReadOptions,
- args: Self::Args,
- ) -> BinResult<()>
+ fn after_parse(&mut self, reader: &mut R, endian: Endian, args: Self::Args) -> BinResult<()>
where
R: Read + Seek,
{
for val in self.iter_mut() {
- val.after_parse(reader, ro, args.inner.clone())?;
+ val.after_parse(reader, endian, args.inner.clone())?;
}
Ok(())
@@ -166,18 +161,18 @@ impl BinRead for [B; N] {
fn read_options(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult {
- array_init::try_array_init(|_| BinRead::read_options(reader, options, args.clone()))
+ array_init::try_array_init(|_| BinRead::read_options(reader, endian, args.clone()))
}
- fn after_parse(&mut self, reader: &mut R, ro: &ReadOptions, args: B::Args) -> BinResult<()>
+ fn after_parse(&mut self, reader: &mut R, endian: Endian, args: B::Args) -> BinResult<()>
where
R: Read + Seek,
{
for val in self.iter_mut() {
- val.after_parse(reader, ro, args.clone())?;
+ val.after_parse(reader, endian, args.clone())?;
}
Ok(())
@@ -190,23 +185,23 @@ macro_rules! binread_tuple_impl {
impl, $($types: BinRead),*> BinRead for ($type1, $($types),*) {
type Args = Args;
- fn read_options(reader: &mut R, options: &ReadOptions, args: Self::Args) -> BinResult {
+ fn read_options(reader: &mut R, endian: Endian, args: Self::Args) -> BinResult {
Ok((
- BinRead::read_options(reader, options, args.clone())?,
+ BinRead::read_options(reader, endian, args.clone())?,
$(
- <$types>::read_options(reader, options, args.clone())?
+ <$types>::read_options(reader, endian, args.clone())?
),*
))
}
- fn after_parse(&mut self, reader: &mut R, options: &ReadOptions, args: Self::Args) -> BinResult<()> {
+ fn after_parse(&mut self, reader: &mut R, endian: Endian, args: Self::Args) -> BinResult<()> {
let ($type1, $(
$types
),*) = self;
- $type1.after_parse(reader, options, args.clone())?;
+ $type1.after_parse(reader, endian, args.clone())?;
$(
- $types.after_parse(reader, options, args.clone())?;
+ $types.after_parse(reader, endian, args.clone())?;
)*
Ok(())
@@ -227,7 +222,7 @@ binread_tuple_impl!(
impl BinRead for () {
type Args = ();
- fn read_options(_: &mut R, _: &ReadOptions, _: Self::Args) -> BinResult {
+ fn read_options(_: &mut R, _: Endian, _: Self::Args) -> BinResult {
Ok(())
}
}
@@ -237,10 +232,10 @@ impl BinRead for Box {
fn read_options(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult {
- Ok(Box::new(T::read_options(reader, options, args)?))
+ Ok(Box::new(T::read_options(reader, endian, args)?))
}
}
@@ -249,23 +244,18 @@ impl BinRead for Option {
fn read_options(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult {
- Ok(Some(T::read_options(reader, options, args)?))
+ Ok(Some(T::read_options(reader, endian, args)?))
}
- fn after_parse(
- &mut self,
- reader: &mut R,
- ro: &ReadOptions,
- args: Self::Args,
- ) -> BinResult<()>
+ fn after_parse(&mut self, reader: &mut R, endian: Endian, args: Self::Args) -> BinResult<()>
where
R: Read + Seek,
{
match self {
- Some(val) => val.after_parse(reader, ro, args),
+ Some(val) => val.after_parse(reader, endian, args),
None => Ok(()),
}
}
@@ -274,7 +264,7 @@ impl BinRead for Option {
impl BinRead for core::marker::PhantomData {
type Args = ();
- fn read_options(_: &mut R, _: &ReadOptions, _: Self::Args) -> BinResult {
+ fn read_options(_: &mut R, _: Endian, _: Self::Args) -> BinResult {
Ok(core::marker::PhantomData)
}
}
diff --git a/binrw/src/binread/mod.rs b/binrw/src/binread/mod.rs
index 735fba21..5c590426 100644
--- a/binrw/src/binread/mod.rs
+++ b/binrw/src/binread/mod.rs
@@ -1,5 +1,4 @@
mod impls;
-mod options;
use crate::{
io::{Read, Seek},
@@ -8,7 +7,6 @@ use crate::{
meta::ReadEndian,
};
pub use impls::VecArgs;
-pub use options::ReadOptions;
/// The `BinRead` trait reads data from streams and converts it into objects.
///
@@ -61,7 +59,7 @@ pub trait BinRead: Sized + 'static {
Self: ReadEndian,
Self::Args: Required,
{
- Self::read_options(reader, &ReadOptions::default(), Self::Args::field_args())
+ Self::read_options(reader, Endian::Little, Self::Args::field_args())
}
/// Read `Self` from the reader using default arguments and assuming
@@ -115,7 +113,7 @@ pub trait BinRead: Sized + 'static {
where
Self: ReadEndian,
{
- Self::read_options(reader, &ReadOptions::default(), args)
+ Self::read_options(reader, Endian::Little, args)
}
/// Read `Self` from the reader, assuming big-endian byte order, using the
@@ -126,7 +124,7 @@ pub trait BinRead: Sized + 'static {
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_be_args(reader: &mut R, args: Self::Args) -> BinResult {
- Self::read_options(reader, &ReadOptions::new(Endian::Big), args)
+ Self::read_options(reader, Endian::Big, args)
}
/// Read `Self` from the reader, assuming little-endian byte order, using
@@ -137,7 +135,7 @@ pub trait BinRead: Sized + 'static {
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_le_args(reader: &mut R, args: Self::Args) -> BinResult {
- Self::read_options(reader, &ReadOptions::new(Endian::Little), args)
+ Self::read_options(reader, Endian::Little, args)
}
/// Read `T` from the reader, assuming native-endian byte order, using the
@@ -148,10 +146,10 @@ pub trait BinRead: Sized + 'static {
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_ne_args(reader: &mut R, args: Self::Args) -> BinResult {
- Self::read_options(reader, &ReadOptions::new(Endian::NATIVE), args)
+ Self::read_options(reader, Endian::NATIVE, args)
}
- /// Read `Self` from the reader using the given [`ReadOptions`] and
+ /// Read `Self` from the reader using the given [`Endian`] and
/// arguments.
///
/// # Errors
@@ -159,7 +157,7 @@ pub trait BinRead: Sized + 'static {
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
fn read_options(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult;
@@ -173,7 +171,7 @@ pub trait BinRead: Sized + 'static {
fn after_parse(
&mut self,
_: &mut R,
- _: &ReadOptions,
+ _: Endian,
_: Self::Args,
) -> BinResult<()> {
Ok(())
@@ -253,10 +251,8 @@ pub trait BinReaderExt: Read + Seek + Sized {
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
fn read_type_args(&mut self, endian: Endian, args: T::Args) -> BinResult {
- let options = ReadOptions::new(endian);
-
- let mut res = T::read_options(self, &options, args.clone())?;
- res.after_parse(self, &options, args)?;
+ let mut res = T::read_options(self, endian, args.clone())?;
+ res.after_parse(self, endian, args)?;
Ok(res)
}
diff --git a/binrw/src/binread/options.rs b/binrw/src/binread/options.rs
deleted file mode 100644
index daf7d4a7..00000000
--- a/binrw/src/binread/options.rs
+++ /dev/null
@@ -1,42 +0,0 @@
-use super::Endian;
-#[cfg(all(doc, not(feature = "std")))]
-use alloc::vec::Vec;
-
-/// Runtime options for
-/// [`BinRead::read_options()`](crate::BinRead::read_options).
-#[derive(Default, Clone, Copy)]
-pub struct ReadOptions {
- /// The [byte order](crate::Endian) to use when reading data.
- ///
- /// Note that if a derived type uses one of the
- /// [byte order directives](crate::docs::attribute#byte-order), this option
- /// will be overridden by the directive.
- endian: Endian,
-}
-
-impl ReadOptions {
- /// Creates a new `ReadOptions` with the given [endianness](crate::Endian).
- #[must_use]
- pub fn new(endian: Endian) -> Self {
- Self { endian }
- }
-
- /// The [byte order](crate::Endian) to use when reading data.
- ///
- /// Note that if a derived type uses one of the
- /// [byte order directives](crate::docs::attribute#byte-order), this option
- /// will be overridden by the directive.
- #[must_use]
- pub fn endian(&self) -> Endian {
- self.endian
- }
-
- /// 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 }
- }
-}
diff --git a/binrw/src/binwrite/impls.rs b/binrw/src/binwrite/impls.rs
index 3b755159..db538c58 100644
--- a/binrw/src/binwrite/impls.rs
+++ b/binrw/src/binwrite/impls.rs
@@ -1,6 +1,6 @@
use crate::{
io::{Seek, Write},
- BinResult, BinWrite, Endian, WriteOptions,
+ BinResult, BinWrite, Endian,
};
use alloc::{boxed::Box, vec::Vec};
use core::{
@@ -21,10 +21,10 @@ macro_rules! binwrite_num_impl {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
_: Self::Args,
) -> BinResult<()> {
- writer.write_all(&match options.endian() {
+ writer.write_all(&match endian {
Endian::Big => self.to_be_bytes(),
Endian::Little => self.to_le_bytes(),
}).map_err(Into::into)
@@ -45,12 +45,12 @@ macro_rules! binwrite_nonzero_num_impl {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
_: Self::Args,
) -> BinResult<()> {
let num = <$type_name>::from(*self);
- writer.write_all(&match options.endian() {
+ writer.write_all(&match endian {
Endian::Big => num.to_be_bytes(),
Endian::Little => num.to_le_bytes(),
}).map_err(Into::into)
@@ -79,14 +79,14 @@ impl BinWrite for [T; N] {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult<()> {
if let Some(this) = ::downcast_ref::<[u8; N]>(self) {
writer.write_all(&this[..])?;
} else {
for item in self {
- T::write_options(item, writer, options, args.clone())?;
+ T::write_options(item, writer, endian, args.clone())?;
}
}
@@ -100,11 +100,11 @@ impl BinWrite for [T] {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult<()> {
for item in self {
- T::write_options(item, writer, options, args.clone())?;
+ T::write_options(item, writer, endian, args.clone())?;
}
Ok(())
@@ -117,7 +117,7 @@ impl BinWrite for Vec {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult<()> {
if let Some(this) = ::downcast_ref::>(self) {
@@ -126,7 +126,7 @@ impl BinWrite for Vec {
writer.write_all(bytemuck::cast_slice(this.as_slice()))?;
} else {
for item in self {
- T::write_options(item, writer, options, args.clone())?;
+ T::write_options(item, writer, endian, args.clone())?;
}
}
@@ -140,10 +140,10 @@ impl BinWrite for &T {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult<()> {
- (**self).write_options(writer, options, args)
+ (**self).write_options(writer, endian, args)
}
}
@@ -153,13 +153,13 @@ impl BinWrite for Box {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult<()> {
if let Some(this) = ::downcast_ref::>(self) {
writer.write_all(this)?;
} else {
- (**self).write_options(writer, options, args)?;
+ (**self).write_options(writer, endian, args)?;
}
Ok(())
@@ -172,11 +172,11 @@ impl BinWrite for Option {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult<()> {
match self {
- Some(inner) => inner.write_options(writer, options, args),
+ Some(inner) => inner.write_options(writer, endian, args),
None => Ok(()),
}
}
@@ -185,12 +185,7 @@ impl BinWrite for Option {
impl BinWrite for PhantomData {
type Args = T::Args;
- fn write_options(
- &self,
- _: &mut W,
- _: &WriteOptions,
- _: Self::Args,
- ) -> BinResult<()> {
+ fn write_options(&self, _: &mut W, _: Endian, _: Self::Args) -> BinResult<()> {
Ok(())
}
}
@@ -198,12 +193,7 @@ impl BinWrite for PhantomData {
impl BinWrite for () {
type Args = ();
- fn write_options(
- &self,
- _: &mut W,
- _: &WriteOptions,
- _: Self::Args,
- ) -> BinResult<()> {
+ fn write_options(&self, _: &mut W, _: Endian, _: Self::Args) -> BinResult<()> {
Ok(())
}
}
@@ -219,16 +209,16 @@ macro_rules! binwrite_tuple_impl {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult<()> {
let ($type1, $(
$types
),*) = self;
- $type1.write_options(writer, options, args.clone())?;
+ $type1.write_options(writer, endian, args.clone())?;
$(
- $types.write_options(writer, options, args.clone())?;
+ $types.write_options(writer, endian, args.clone())?;
)*
Ok(())
diff --git a/binrw/src/binwrite/mod.rs b/binrw/src/binwrite/mod.rs
index 95a92cc8..e8f3704d 100644
--- a/binrw/src/binwrite/mod.rs
+++ b/binrw/src/binwrite/mod.rs
@@ -96,7 +96,7 @@ pub trait BinWrite {
where
Self: crate::meta::WriteEndian,
{
- self.write_options(writer, &WriteOptions::default(), args)
+ self.write_options(writer, Endian::Little, args)
}
/// Write `Self` to the writer, assuming big-endian byte order, using the
@@ -107,7 +107,7 @@ pub trait BinWrite {
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn write_be_args(&self, writer: &mut W, args: Self::Args) -> BinResult<()> {
- self.write_options(writer, &WriteOptions::new(Endian::Big), args)
+ self.write_options(writer, Endian::Big, args)
}
/// Write `Self` to the writer, assuming little-endian byte order, using the
@@ -118,10 +118,10 @@ pub trait BinWrite {
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn write_le_args(&self, writer: &mut W, args: Self::Args) -> BinResult<()> {
- self.write_options(writer, &WriteOptions::new(Endian::Little), args)
+ self.write_options(writer, Endian::Little, args)
}
- /// Write `Self` to the writer using the given [`WriteOptions`] and
+ /// Write `Self` to the writer using the given [`Endian`] and
/// arguments.
///
/// # Errors
@@ -130,50 +130,11 @@ pub trait BinWrite {
fn write_options(
&self,
writer: &mut W,
- options: &WriteOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult<()>;
}
-/// Runtime options for
-/// [`BinWrite::write_options()`](crate::BinWrite::write_options).
-#[derive(Default, Clone, Copy)]
-pub struct WriteOptions {
- /// The [byte order](crate::Endian) to use when writing data.
- ///
- /// Note that if a derived type uses one of the
- /// [byte order directives](crate::docs::attribute#byte-order), this option
- /// will be overridden by the directive.
- endian: Endian,
-}
-
-impl WriteOptions {
- /// Creates a new `WriteOptions` with the given [endianness](crate::Endian).
- #[must_use]
- pub fn new(endian: Endian) -> Self {
- Self { endian }
- }
-
- /// The [byte order](crate::Endian) to use when writing data.
- ///
- /// Note that if a derived type uses one of the
- /// [byte order directives](crate::docs::attribute#byte-order), this option
- /// will be overridden by the directive.
- #[must_use]
- pub fn endian(&self) -> Endian {
- self.endian
- }
-
- /// Creates a copy of this `WriteOptions` using the given
- /// [endianness](crate::Endian).
- #[must_use]
- // Lint: For symmetry with `ReadOptions`.
- #[allow(clippy::unused_self)]
- pub fn with_endian(self, endian: Endian) -> Self {
- Self { endian }
- }
-}
-
/// Extension methods for writing [`BinWrite`] objects directly to a writer.
///
/// # Examples
@@ -250,9 +211,7 @@ pub trait BinWriterExt: Write + Seek + Sized {
endian: Endian,
args: T::Args,
) -> BinResult<()> {
- let options = WriteOptions::new(endian);
-
- T::write_options(value, self, &options, args)?;
+ T::write_options(value, self, endian, args)?;
Ok(())
}
diff --git a/binrw/src/file_ptr.rs b/binrw/src/file_ptr.rs
index 39aea459..b8fcbb15 100644
--- a/binrw/src/file_ptr.rs
+++ b/binrw/src/file_ptr.rs
@@ -1,6 +1,11 @@
//! Type definitions for wrappers which represent a layer of indirection within
//! a file.
+use crate::NamedArgs;
+use crate::{
+ io::{Read, Seek, SeekFrom},
+ BinRead, BinResult, Endian,
+};
use core::fmt;
use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
@@ -8,12 +13,6 @@ use core::num::{
};
use core::ops::{Deref, DerefMut};
-use crate::NamedArgs;
-use crate::{
- io::{Read, Seek, SeekFrom},
- BinRead, BinResult, ReadOptions,
-};
-
/// A type alias for [`FilePtr`] with 8-bit offsets.
pub type FilePtr8 = FilePtr;
/// A type alias for [`FilePtr`] with 16-bit offsets.
@@ -87,11 +86,11 @@ impl + IntoSeekFrom, Value: BinRead> BinRead for FilePtr
/// [`after_parse()`](Self::after_parse) is called.
fn read_options(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
_: Self::Args,
) -> BinResult {
Ok(FilePtr {
- ptr: Ptr::read_options(reader, options, ())?,
+ ptr: Ptr::read_options(reader, endian, ())?,
value: None,
})
}
@@ -100,64 +99,66 @@ impl + IntoSeekFrom, Value: BinRead> BinRead for FilePtr
fn after_parse(
&mut self,
reader: &mut R,
- ro: &ReadOptions,
+ endian: Endian,
args: FilePtrArgs,
) -> BinResult<()>
where
R: Read + Seek,
{
- self.after_parse_with_parser(Value::read_options, Value::after_parse, reader, ro, args)
+ self.after_parse_with_parser(
+ Value::read_options,
+ Value::after_parse,
+ reader,
+ endian,
+ args,
+ )
}
}
impl + IntoSeekFrom, Value> FilePtr {
- // Lint: Non-consumed argument is required to match the API.
- #[allow(clippy::trivially_copy_pass_by_ref)]
fn read_with_parser(
parser: Parser,
after_parse: AfterParse,
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: FilePtrArgs,
) -> BinResult
where
R: Read + Seek,
Args: Clone,
- Parser: Fn(&mut R, &ReadOptions, Args) -> BinResult,
- AfterParse: Fn(&mut Value, &mut R, &ReadOptions, Args) -> BinResult<()>,
+ Parser: Fn(&mut R, Endian, Args) -> BinResult,
+ AfterParse: Fn(&mut Value, &mut R, Endian, Args) -> BinResult<()>,
{
let mut file_ptr = Self {
- ptr: Ptr::read_options(reader, options, ())?,
+ ptr: Ptr::read_options(reader, endian, ())?,
value: None,
};
- file_ptr.after_parse_with_parser(parser, after_parse, reader, options, args)?;
+ file_ptr.after_parse_with_parser(parser, after_parse, reader, endian, args)?;
Ok(file_ptr)
}
- // Lint: Non-consumed argument is required to match the API.
- #[allow(clippy::trivially_copy_pass_by_ref)]
fn after_parse_with_parser(
&mut self,
parser: Parser,
after_parse: AfterParse,
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: FilePtrArgs,
) -> BinResult<()>
where
R: Read + Seek,
Args: Clone,
- Parser: Fn(&mut R, &ReadOptions, Args) -> BinResult,
- AfterParse: Fn(&mut Value, &mut R, &ReadOptions, Args) -> BinResult<()>,
+ Parser: Fn(&mut R, Endian, Args) -> BinResult,
+ AfterParse: Fn(&mut Value, &mut R, Endian, Args) -> BinResult<()>,
{
let relative_to = args.offset;
let before = reader.stream_position()?;
reader.seek(SeekFrom::Start(relative_to))?;
reader.seek(self.ptr.into_seek_from())?;
- let mut inner: Value = parser(reader, options, args.inner.clone())?;
+ let mut inner: Value = parser(reader, endian, args.inner.clone())?;
- after_parse(&mut inner, reader, options, args.inner)?;
+ after_parse(&mut inner, reader, endian, args.inner)?;
reader.seek(SeekFrom::Start(before))?;
self.value = Some(inner);
@@ -174,7 +175,7 @@ impl + IntoSeekFrom, Value> FilePtr {
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
pub fn parse(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: FilePtrArgs,
) -> BinResult
where
@@ -186,7 +187,7 @@ impl + IntoSeekFrom, Value> FilePtr {
Value::read_options,
Value::after_parse,
reader,
- options,
+ endian,
args,
)?
.into_inner())
@@ -202,15 +203,15 @@ impl + IntoSeekFrom, Value> FilePtr {
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
pub fn parse_with(
parser: F,
- ) -> impl Fn(&mut R, &ReadOptions, FilePtrArgs) -> BinResult
+ ) -> impl Fn(&mut R, Endian, FilePtrArgs) -> BinResult
where
R: Read + Seek,
Args: Clone,
- F: Fn(&mut R, &ReadOptions, Args) -> BinResult,
+ F: Fn(&mut R, Endian, Args) -> BinResult,
{
- move |reader, ro, args| {
- let after_parse = |_: &mut Value, _: &mut R, _: &ReadOptions, _: Args| Ok(());
- Ok(Self::read_with_parser(&parser, after_parse, reader, ro, args)?.into_inner())
+ move |reader, endian, args| {
+ let after_parse = |_: &mut Value, _: &mut R, _: Endian, _: Args| Ok(());
+ Ok(Self::read_with_parser(&parser, after_parse, reader, endian, args)?.into_inner())
}
}
@@ -224,15 +225,15 @@ impl + IntoSeekFrom, Value> FilePtr {
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
pub fn with(
parser: F,
- ) -> impl Fn(&mut R, &ReadOptions, FilePtrArgs) -> BinResult
+ ) -> impl Fn(&mut R, Endian, FilePtrArgs) -> BinResult
where
R: Read + Seek,
Args: Clone,
- F: Fn(&mut R, &ReadOptions, Args) -> BinResult,
+ F: Fn(&mut R, Endian, Args) -> BinResult,
{
- move |reader, ro, args| {
- let after_parse = |_: &mut Value, _: &mut R, _: &ReadOptions, _: Args| Ok(());
- Self::read_with_parser(&parser, after_parse, reader, ro, args)
+ move |reader, endian, args| {
+ let after_parse = |_: &mut Value, _: &mut R, _: Endian, _: Args| Ok(());
+ Self::read_with_parser(&parser, after_parse, reader, endian, args)
}
}
diff --git a/binrw/src/helpers.rs b/binrw/src/helpers.rs
index e98262ae..176bf44b 100644
--- a/binrw/src/helpers.rs
+++ b/binrw/src/helpers.rs
@@ -2,7 +2,7 @@
use crate::{
io::{self, Read, Seek},
- BinRead, BinResult, Error, ReadOptions,
+ BinRead, BinResult, Endian, Error,
};
use alloc::vec::Vec;
use core::iter::from_fn;
@@ -29,7 +29,7 @@ use core::iter::from_fn;
/// ```
pub fn until(
cond: CondFn,
-) -> impl Fn(&mut Reader, &ReadOptions, Arg) -> BinResult
+) -> impl Fn(&mut Reader, Endian, Arg) -> BinResult
where
T: BinRead,
Reader: Read + Seek,
@@ -69,21 +69,21 @@ where
pub fn until_with(
cond: CondFn,
read: ReadFn,
-) -> impl Fn(&mut Reader, &ReadOptions, Arg) -> BinResult
+) -> impl Fn(&mut Reader, Endian, Arg) -> BinResult
where
Reader: Read + Seek,
CondFn: Fn(&T) -> bool,
Arg: Clone,
- ReadFn: Fn(&mut Reader, &ReadOptions, Arg) -> BinResult,
+ ReadFn: Fn(&mut Reader, Endian, Arg) -> BinResult,
Ret: FromIterator,
{
- move |reader, ro, args| {
+ move |reader, endian, args| {
let mut last = false;
from_fn(|| {
if last {
None
} else {
- match read(reader, ro, args.clone()) {
+ match read(reader, endian, args.clone()) {
Ok(value) => {
if cond(&value) {
last = true;
@@ -121,7 +121,7 @@ where
/// ```
pub fn until_exclusive(
cond: CondFn,
-) -> impl Fn(&mut Reader, &ReadOptions, Arg) -> BinResult
+) -> impl Fn(&mut Reader, Endian, Arg) -> BinResult
where
T: BinRead,
Reader: Read + Seek,
@@ -161,16 +161,16 @@ where
pub fn until_exclusive_with(
cond: CondFn,
read: ReadFn,
-) -> impl Fn(&mut Reader, &ReadOptions, Arg) -> BinResult
+) -> impl Fn(&mut Reader, Endian, Arg) -> BinResult
where
Reader: Read + Seek,
CondFn: Fn(&T) -> bool,
Arg: Clone,
- ReadFn: Fn(&mut Reader, &ReadOptions, Arg) -> BinResult,
+ ReadFn: Fn(&mut Reader, Endian, Arg) -> BinResult,
Ret: FromIterator,
{
- move |reader, ro, args| {
- from_fn(|| match read(reader, ro, args.clone()) {
+ move |reader, endian, args| {
+ from_fn(|| match read(reader, endian, args.clone()) {
Ok(value) => {
if cond(&value) {
None
@@ -212,7 +212,7 @@ where
/// ```
pub fn until_eof(
reader: &mut Reader,
- ro: &ReadOptions,
+ endian: Endian,
args: Arg,
) -> BinResult
where
@@ -221,7 +221,7 @@ where
Arg: Clone,
Ret: FromIterator,
{
- until_eof_with(default_reader)(reader, ro, args)
+ until_eof_with(default_reader)(reader, endian, args)
}
/// Creates a parser that uses a given function to read items into a collection
@@ -257,15 +257,15 @@ where
/// ```
pub fn until_eof_with(
read: ReadFn,
-) -> impl Fn(&mut Reader, &ReadOptions, Arg) -> BinResult
+) -> impl Fn(&mut Reader, Endian, Arg) -> BinResult
where
Reader: Read + Seek,
Arg: Clone,
- ReadFn: Fn(&mut Reader, &ReadOptions, Arg) -> BinResult,
+ ReadFn: Fn(&mut Reader, Endian, Arg) -> BinResult,
Ret: FromIterator,
{
- move |reader, ro, args| {
- from_fn(|| match read(reader, ro, args.clone()) {
+ move |reader, endian, args| {
+ from_fn(|| match read(reader, endian, args.clone()) {
ok @ Ok(_) => Some(ok),
Err(err) if err.is_eof() => None,
err => Some(err),
@@ -298,7 +298,7 @@ where
/// # let x: CountBytes = x.read_be().unwrap();
/// # assert_eq!(x.data, &[1, 2, 3]);
/// ```
-pub fn count(n: usize) -> impl Fn(&mut R, &ReadOptions, Arg) -> BinResult
+pub fn count(n: usize) -> impl Fn(&mut R, Endian, Arg) -> BinResult
where
T: BinRead,
R: Read + Seek,
@@ -339,17 +339,17 @@ where
pub fn count_with(
n: usize,
read: ReadFn,
-) -> impl Fn(&mut R, &ReadOptions, Arg) -> BinResult
+) -> impl Fn(&mut R, Endian, Arg) -> BinResult
where
R: Read + Seek,
Arg: Clone,
- ReadFn: Fn(&mut R, &ReadOptions, Arg) -> BinResult,
+ ReadFn: Fn(&mut R, Endian, Arg) -> BinResult,
Ret: FromIterator + 'static,
{
- move |reader, ro, args| {
+ move |reader, endian, args| {
let mut container = core::iter::empty::().collect::();
- vec_fast_int!(try (i8 i16 u16 i32 u32 i64 u64 i128 u128) using (container, reader, ro.endian(), n) else {
+ vec_fast_int!(try (i8 i16 u16 i32 u32 i64 u64 i128 u128) using (container, reader, endian, n) else {
// This extra branch for `Vec` makes it faster than
// `vec_fast_int`, but *only* because `vec_fast_int` is not allowed
// to use unsafe code to eliminate the unnecessary zero-fill.
@@ -367,7 +367,7 @@ where
Err(not_enough_bytes(()))
}
} else {
- core::iter::repeat_with(|| read(reader, ro, args.clone()))
+ core::iter::repeat_with(|| read(reader, endian, args.clone()))
.take(n)
.collect()
}
@@ -379,11 +379,11 @@ where
#[allow(clippy::trivially_copy_pass_by_ref)]
fn default_reader>(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: T::Args,
) -> BinResult {
- let mut value = T::read_options(reader, options, args.clone())?;
- value.after_parse(reader, options, args)?;
+ let mut value = T::read_options(reader, endian, args.clone())?;
+ value.after_parse(reader, endian, args)?;
Ok(value)
}
diff --git a/binrw/src/pos_value.rs b/binrw/src/pos_value.rs
index 5780f0fc..1ed9b2fd 100644
--- a/binrw/src/pos_value.rs
+++ b/binrw/src/pos_value.rs
@@ -1,6 +1,6 @@
use crate::{
io::{Read, Seek},
- BinRead, BinResult, ReadOptions,
+ BinRead, BinResult, Endian,
};
use core::fmt;
@@ -34,24 +34,24 @@ impl BinRead for PosValue {
fn read_options(
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: T::Args,
) -> BinResult {
let pos = reader.stream_position()?;
Ok(PosValue {
pos,
- val: T::read_options(reader, options, args)?,
+ val: T::read_options(reader, endian, args)?,
})
}
fn after_parse(
&mut self,
reader: &mut R,
- options: &ReadOptions,
+ endian: Endian,
args: Self::Args,
) -> BinResult<()> {
- self.val.after_parse(reader, options, args)
+ self.val.after_parse(reader, endian, args)
}
}
diff --git a/binrw/src/private.rs b/binrw/src/private.rs
index 15eba009..3e1b971e 100644
--- a/binrw/src/private.rs
+++ b/binrw/src/private.rs
@@ -1,7 +1,7 @@
use crate::{
error::CustomError,
io::{self, Seek, Write},
- BinRead, BinResult, Error, ReadOptions, WriteOptions,
+ BinRead, BinResult, Endian, Error,
};
use alloc::{boxed::Box, string::String};
@@ -72,13 +72,13 @@ where
f
}
-pub fn magic(reader: &mut R, expected: B, options: &ReadOptions) -> BinResult<()>
+pub fn magic(reader: &mut R, expected: B, endian: Endian) -> BinResult<()>
where
B: BinRead + core::fmt::Debug + PartialEq + Sync + Send + Clone + Copy + 'static,
R: io::Read + io::Seek,
{
let pos = reader.stream_position()?;
- let val = B::read_options(reader, options, ())?;
+ let val = B::read_options(reader, endian, ())?;
if val == expected {
Ok(())
} else {
@@ -92,7 +92,7 @@ where
pub fn parse_function_args_type_hint(_: F, a: Args) -> Args
where
R: crate::io::Read + Seek,
- F: FnOnce(&mut R, &crate::ReadOptions, Args) -> crate::BinResult,
+ F: FnOnce(&mut R, Endian, Args) -> crate::BinResult,
{
a
}
@@ -100,7 +100,7 @@ where
pub fn write_function_args_type_hint(_: F, a: Args) -> Args
where
W: Write + Seek,
- F: FnOnce(&T, &mut W, &crate::WriteOptions, Args) -> crate::BinResult<()>,
+ F: FnOnce(&T, &mut W, Endian, Args) -> crate::BinResult<()>,
{
a
}
@@ -117,7 +117,7 @@ pub fn write_fn_type_hint(x: WriterFn) -> WriterFn
where
Args: Clone,
Writer: Write + Seek,
- WriterFn: Fn(&T, &mut Writer, &WriteOptions, Args) -> BinResult<()>,
+ WriterFn: Fn(&T, &mut Writer, Endian, Args) -> BinResult<()>,
{
x
}
@@ -157,7 +157,7 @@ where
MapFn: FnOnce(Input) -> Output,
Args: Clone,
Writer: Write + Seek,
- WriteFn: Fn(&Output, &mut Writer, &WriteOptions, Args) -> BinResult<()>,
+ WriteFn: Fn(&Output, &mut Writer, Endian, Args) -> BinResult<()>,
{
func
}
@@ -171,7 +171,7 @@ where
MapFn: FnOnce(Input) -> Result