Skip to content

Commit

Permalink
Allow use of unsized readers and writers
Browse files Browse the repository at this point in the history
  • Loading branch information
csnover committed Aug 23, 2022
1 parent d83ae1a commit fad0b29
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 66 deletions.
4 changes: 2 additions & 2 deletions binrw/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@
//! ```
//! # use binrw::{prelude::*, io::*, ReadOptions};
//! # use std::collections::HashMap;
//! fn custom_parser<R: Read + Seek>(reader: &mut R, ro: &ReadOptions, _: ())
//! fn custom_parser<R: Read + Seek + ?Sized>(reader: &mut R, ro: &ReadOptions, _: ())
//! -> BinResult<HashMap<u16, u16>>
//! {
//! let mut map = HashMap::new();
Expand Down Expand Up @@ -1065,7 +1065,7 @@
//! ```
//! # use binrw::{prelude::*, io::*, WriteOptions};
//! # use std::collections::BTreeMap;
//! fn custom_writer<R: Write + Seek>(
//! fn custom_writer<R: Write + Seek + ?Sized>(
//! map: &BTreeMap<u16, u16>,
//! writer: &mut R,
//! wo: &WriteOptions,
Expand Down
34 changes: 21 additions & 13 deletions binrw/src/binread/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro_rules! binread_impl {
impl BinRead for $type_name {
type Args = ();

fn read_options<R: Read + Seek>(reader: &mut R, options: &ReadOptions, _: Self::Args) -> BinResult<Self> {
fn read_options<R: Read + Seek + ?Sized>(reader: &mut R, options: &ReadOptions, _: Self::Args) -> BinResult<Self> {
let mut val = [0; core::mem::size_of::<$type_name>()];
let pos = reader.stream_position()?;

Expand Down Expand Up @@ -71,7 +71,7 @@ macro_rules! binread_nonzero_impl {
impl BinRead for $Ty {
type Args = ();

fn read_options<R: Read + Seek>(
fn read_options<R: Read + Seek + ?Sized>(
reader: &mut R,
options: &ReadOptions,
_: Self::Args,
Expand Down Expand Up @@ -159,7 +159,7 @@ pub struct VecArgs<B> {
impl<B: BinRead> BinRead for Vec<B> {
type Args = VecArgs<B::Args>;

fn read_options<R: Read + Seek>(
fn read_options<R: Read + Seek + ?Sized>(
reader: &mut R,
options: &ReadOptions,
args: Self::Args,
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<B: BinRead> BinRead for Vec<B> {
args: Self::Args,
) -> BinResult<()>
where
R: Read + Seek,
R: Read + Seek + ?Sized,
{
for val in self.iter_mut() {
val.after_parse(reader, ro, args.inner.clone())?;
Expand All @@ -204,7 +204,7 @@ impl<B: BinRead> BinRead for Vec<B> {
impl<B: BinRead, const N: usize> BinRead for [B; N] {
type Args = B::Args;

fn read_options<R: Read + Seek>(
fn read_options<R: Read + Seek + ?Sized>(
reader: &mut R,
options: &ReadOptions,
args: Self::Args,
Expand All @@ -214,7 +214,7 @@ impl<B: BinRead, const N: usize> BinRead for [B; N] {

fn after_parse<R>(&mut self, reader: &mut R, ro: &ReadOptions, args: B::Args) -> BinResult<()>
where
R: Read + Seek,
R: Read + Seek + ?Sized,
{
for val in self.iter_mut() {
val.after_parse(reader, ro, args.clone())?;
Expand All @@ -230,7 +230,7 @@ macro_rules! binread_tuple_impl {
impl<$type1: BinRead<Args=()>, $($types: BinRead<Args=()>),*> BinRead for ($type1, $($types),*) {
type Args = ();

fn read_options<R: Read + Seek>(reader: &mut R, options: &ReadOptions, _: Self::Args) -> BinResult<Self> {
fn read_options<R: Read + Seek + ?Sized>(reader: &mut R, options: &ReadOptions, _: Self::Args) -> BinResult<Self> {
Ok((
BinRead::read_options(reader, options, ())?,
$(
Expand All @@ -239,7 +239,7 @@ macro_rules! binread_tuple_impl {
))
}

fn after_parse<R: Read + Seek>(&mut self, reader: &mut R, options: &ReadOptions, _: Self::Args) -> BinResult<()> {
fn after_parse<R: Read + Seek + ?Sized>(&mut self, reader: &mut R, options: &ReadOptions, _: Self::Args) -> BinResult<()> {
let ($type1, $(
$types
),*) = self;
Expand Down Expand Up @@ -267,15 +267,19 @@ binread_tuple_impl!(
impl BinRead for () {
type Args = ();

fn read_options<R: Read + Seek>(_: &mut R, _: &ReadOptions, _: Self::Args) -> BinResult<Self> {
fn read_options<R: Read + Seek + ?Sized>(
_: &mut R,
_: &ReadOptions,
_: Self::Args,
) -> BinResult<Self> {
Ok(())
}
}

impl<T: BinRead> BinRead for Box<T> {
type Args = T::Args;

fn read_options<R: Read + Seek>(
fn read_options<R: Read + Seek + ?Sized>(
reader: &mut R,
options: &ReadOptions,
args: Self::Args,
Expand All @@ -287,7 +291,7 @@ impl<T: BinRead> BinRead for Box<T> {
impl<T: BinRead> BinRead for Option<T> {
type Args = T::Args;

fn read_options<R: Read + Seek>(
fn read_options<R: Read + Seek + ?Sized>(
reader: &mut R,
options: &ReadOptions,
args: Self::Args,
Expand All @@ -302,7 +306,7 @@ impl<T: BinRead> BinRead for Option<T> {
args: Self::Args,
) -> BinResult<()>
where
R: Read + Seek,
R: Read + Seek + ?Sized,
{
match self {
Some(val) => val.after_parse(reader, ro, args),
Expand All @@ -314,7 +318,11 @@ impl<T: BinRead> BinRead for Option<T> {
impl<T: 'static> BinRead for core::marker::PhantomData<T> {
type Args = ();

fn read_options<R: Read + Seek>(_: &mut R, _: &ReadOptions, _: Self::Args) -> BinResult<Self> {
fn read_options<R: Read + Seek + ?Sized>(
_: &mut R,
_: &ReadOptions,
_: Self::Args,
) -> BinResult<Self> {
Ok(core::marker::PhantomData)
}
}
6 changes: 3 additions & 3 deletions binrw/src/binread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub trait BinRead: Sized + 'static {
type Args: Clone;

/// Read `Self` from the reader using default arguments.
fn read<R: Read + Seek>(reader: &mut R) -> BinResult<Self>
fn read<R: Read + Seek + ?Sized>(reader: &mut R) -> BinResult<Self>
where
Self::Args: Default,
{
Expand All @@ -66,15 +66,15 @@ pub trait BinRead: Sized + 'static {

/// Read `Self` from the reader using the given [`ReadOptions`] and
/// arguments.
fn read_options<R: Read + Seek>(
fn read_options<R: Read + Seek + ?Sized>(
reader: &mut R,
options: &ReadOptions,
args: Self::Args,
) -> BinResult<Self>;

/// Runs any post-processing steps required to finalize construction of the
/// object.
fn after_parse<R: Read + Seek>(
fn after_parse<R: Read + Seek + ?Sized>(
&mut self,
_: &mut R,
_: &ReadOptions,
Expand Down
22 changes: 11 additions & 11 deletions binrw/src/binwrite/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro_rules! binwrite_num_impl {
impl BinWrite for $type_name {
type Args = ();

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand All @@ -43,7 +43,7 @@ macro_rules! binwrite_nonzero_num_impl {
impl BinWrite for $non_zero_type {
type Args = ();

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand Down Expand Up @@ -82,7 +82,7 @@ binwrite_nonzero_num_impl!(
impl<T: BinWrite + 'static, const N: usize> BinWrite for [T; N] {
type Args = T::Args;

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand All @@ -103,7 +103,7 @@ impl<T: BinWrite + 'static, const N: usize> BinWrite for [T; N] {
impl<T: BinWrite> BinWrite for [T] {
type Args = T::Args;

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand All @@ -120,7 +120,7 @@ impl<T: BinWrite> BinWrite for [T] {
impl<T: BinWrite + 'static> BinWrite for Vec<T> {
type Args = T::Args;

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand All @@ -145,7 +145,7 @@ impl<T: BinWrite + 'static> BinWrite for Vec<T> {
impl<T: BinWrite + ?Sized> BinWrite for &T {
type Args = T::Args;

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand All @@ -158,7 +158,7 @@ impl<T: BinWrite + ?Sized> BinWrite for &T {
impl<T: BinWrite + ?Sized + 'static> BinWrite for Box<T> {
type Args = T::Args;

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand All @@ -177,7 +177,7 @@ impl<T: BinWrite + ?Sized + 'static> BinWrite for Box<T> {
impl<T: BinWrite> BinWrite for Option<T> {
type Args = T::Args;

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand All @@ -193,7 +193,7 @@ impl<T: BinWrite> BinWrite for Option<T> {
impl<T: BinWrite> BinWrite for PhantomData<T> {
type Args = T::Args;

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
_: &mut W,
_: &WriteOptions,
Expand All @@ -210,7 +210,7 @@ impl<T: BinWrite> BinWrite for PhantomData<T> {
impl BinWrite for () {
type Args = ();

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
_: &mut W,
_: &WriteOptions,
Expand All @@ -228,7 +228,7 @@ macro_rules! binwrite_tuple_impl {
> BinWrite for ($type1, $($types),*) {
type Args = ();

fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand Down
10 changes: 7 additions & 3 deletions binrw/src/binwrite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ pub trait BinWrite {
type Args: Clone;

/// Write a type to a writer while assuming no arguments are needed.
fn write_to<W: Write + Seek>(&self, writer: &mut W) -> BinResult<()>
fn write_to<W: Write + Seek + ?Sized>(&self, writer: &mut W) -> BinResult<()>
where
Self::Args: Default,
{
self.write_options(writer, &WriteOptions::default(), Self::Args::default())
}

/// Write the type to a writer while providing the default [`WriteOptions`]
fn write_with_args<W: Write + Seek>(&self, writer: &mut W, args: Self::Args) -> BinResult<()> {
fn write_with_args<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
args: Self::Args,
) -> BinResult<()> {
self.write_options(writer, &WriteOptions::default(), args)
}

/// Write the type to a writer, given the options on how to write it and the type-specific
/// arguments
fn write_options<W: Write + Seek>(
fn write_options<W: Write + Seek + ?Sized>(
&self,
writer: &mut W,
options: &WriteOptions,
Expand Down
14 changes: 7 additions & 7 deletions binrw/src/file_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<Ptr: BinRead<Args = ()> + IntoSeekFrom, BR: BinRead> BinRead for FilePtr<Pt
///
/// The actual value will not be read until
/// [`after_parse()`](Self::after_parse) is called.
fn read_options<R: Read + Seek>(
fn read_options<R: Read + Seek + ?Sized>(
reader: &mut R,
options: &ReadOptions,
_: Self::Args,
Expand All @@ -98,7 +98,7 @@ impl<Ptr: BinRead<Args = ()> + IntoSeekFrom, BR: BinRead> BinRead for FilePtr<Pt
/// Finalizes the `FilePtr` by seeking to and reading the pointed-to value.
fn after_parse<R>(&mut self, reader: &mut R, ro: &ReadOptions, args: BR::Args) -> BinResult<()>
where
R: Read + Seek,
R: Read + Seek + ?Sized,
{
self.after_parse_with_parser(BR::read_options, BR::after_parse, reader, ro, args)
}
Expand All @@ -113,7 +113,7 @@ impl<Ptr: BinRead<Args = ()> + IntoSeekFrom, T> FilePtr<Ptr, T> {
args: Args,
) -> BinResult<Self>
where
R: Read + Seek,
R: Read + Seek + ?Sized,
Args: Clone,
Parser: Fn(&mut R, &ReadOptions, Args) -> BinResult<T>,
AfterParse: Fn(&mut T, &mut R, &ReadOptions, Args) -> BinResult<()>,
Expand All @@ -135,7 +135,7 @@ impl<Ptr: BinRead<Args = ()> + IntoSeekFrom, T> FilePtr<Ptr, T> {
args: Args,
) -> BinResult<()>
where
R: Read + Seek,
R: Read + Seek + ?Sized,
Args: Clone,
Parser: Fn(&mut R, &ReadOptions, Args) -> BinResult<T>,
AfterParse: Fn(&mut T, &mut R, &ReadOptions, Args) -> BinResult<()>,
Expand All @@ -160,7 +160,7 @@ impl<Ptr: BinRead<Args = ()> + IntoSeekFrom, T> FilePtr<Ptr, T> {
/// value as the result.
pub fn parse<R, Args>(reader: &mut R, options: &ReadOptions, args: Args) -> BinResult<T>
where
R: Read + Seek,
R: Read + Seek + ?Sized,
Args: Clone,
T: BinRead<Args = Args>,
{
Expand All @@ -176,7 +176,7 @@ impl<Ptr: BinRead<Args = ()> + IntoSeekFrom, T> FilePtr<Ptr, T> {
/// value as the result.
pub fn parse_with<R, F, Args>(parser: F) -> impl Fn(&mut R, &ReadOptions, Args) -> BinResult<T>
where
R: Read + Seek,
R: Read + Seek + ?Sized,
Args: Clone,
F: Fn(&mut R, &ReadOptions, Args) -> BinResult<T>,
{
Expand All @@ -192,7 +192,7 @@ impl<Ptr: BinRead<Args = ()> + IntoSeekFrom, T> FilePtr<Ptr, T> {
/// as the result.
pub fn with<R, F, Args>(parser: F) -> impl Fn(&mut R, &ReadOptions, Args) -> BinResult<Self>
where
R: Read + Seek,
R: Read + Seek + ?Sized,
Args: Clone,
F: Fn(&mut R, &ReadOptions, Args) -> BinResult<T>,
{
Expand Down
Loading

0 comments on commit fad0b29

Please sign in to comment.