1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
mod impls;
use crate::{
io::{Read, Seek},
BinResult, Endian,
__private::Required,
meta::ReadEndian,
};
pub use impls::VecArgs;
/// The `BinRead` trait reads data from streams and converts it into objects.
///
/// This trait is usually derived, but can also be manually implemented by
/// writing an appropriate [`Args`] type and [`read_options()`] function.
///
/// [`Args`]: Self::Args
/// [`read_options()`]: Self::read_options
///
/// # Derivable
///
/// This trait can be used with `#[derive]` or `#[binread]`. Each field of a
/// derived type must either implement `BinRead` or be annotated with an
/// attribute containing a [`map`], [`try_map`], or [`parse_with`] directive.
///
/// [`map`]: crate::docs::attribute#map
/// [`parse_with`]: crate::docs::attribute#custom-parserswriters
/// [`try_map`]: crate::docs::attribute#map
///
/// Using `#[binread]` instead of `#[derive]` is required when using
/// [temporary fields].
///
/// [temporary fields]: crate::docs::attribute#temp
pub trait BinRead: Sized {
/// The type used for the `args` parameter of [`read_args()`] and
/// [`read_options()`].
///
/// When the given type implements [`Default`], convenience functions like
/// [`read()`] are enabled. `BinRead` implementations that don’t receive any
/// arguments should use the `()` type.
///
/// When `BinRead` is derived, the [`import`] and [`import_tuple`]
/// directives define this type.
///
/// [`import`]: crate::docs::attribute#arguments
/// [`import_tuple`]: crate::docs::attribute#arguments
/// [`read()`]: Self::read
/// [`read_args()`]: Self::read_args
/// [`read_options()`]: Self::read_options
type Args<'a>;
/// Read `Self` from the reader using default arguments.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read<R: Read + Seek>(reader: &mut R) -> BinResult<Self>
where
Self: ReadEndian,
for<'a> Self::Args<'a>: Required,
{
Self::read_args(reader, Self::Args::args())
}
/// Read `Self` from the reader using default arguments and assuming
/// big-endian byte order.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_be<R: Read + Seek>(reader: &mut R) -> BinResult<Self>
where
for<'a> Self::Args<'a>: Required,
{
Self::read_be_args(reader, Self::Args::args())
}
/// Read `Self` from the reader using default arguments and assuming
/// little-endian byte order.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_le<R: Read + Seek>(reader: &mut R) -> BinResult<Self>
where
for<'a> Self::Args<'a>: Required,
{
Self::read_le_args(reader, Self::Args::args())
}
/// Read `T` from the reader assuming native-endian byte order.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_ne<R: Read + Seek>(reader: &mut R) -> BinResult<Self>
where
for<'a> Self::Args<'a>: Required,
{
Self::read_ne_args(reader, Self::Args::args())
}
/// Read `Self` from the reader using the given arguments.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_args<R: Read + Seek>(reader: &mut R, args: Self::Args<'_>) -> BinResult<Self>
where
Self: ReadEndian,
{
Self::read_options(reader, Endian::Little, args)
}
/// Read `Self` from the reader, assuming big-endian byte order, using the
/// given arguments.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_be_args<R: Read + Seek>(reader: &mut R, args: Self::Args<'_>) -> BinResult<Self> {
Self::read_options(reader, Endian::Big, args)
}
/// Read `Self` from the reader, assuming little-endian byte order, using
/// the given arguments.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_le_args<R: Read + Seek>(reader: &mut R, args: Self::Args<'_>) -> BinResult<Self> {
Self::read_options(reader, Endian::Little, args)
}
/// Read `T` from the reader, assuming native-endian byte order, using the
/// given arguments.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_ne_args<R: Read + Seek>(reader: &mut R, args: Self::Args<'_>) -> BinResult<Self> {
Self::read_options(reader, Endian::NATIVE, args)
}
/// Read `Self` from the reader using the given [`Endian`] and
/// arguments.
///
/// # 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,
args: Self::Args<'_>,
) -> BinResult<Self>;
}
/// Extension methods for reading [`BinRead`] objects directly from a reader.
///
/// # Examples
///
/// ```
/// use binrw::{BinReaderExt, Endian, io::Cursor};
///
/// let mut reader = Cursor::new(b"\x07\0\0\0\xCC\0\0\x05");
/// let x: u32 = reader.read_le().unwrap();
/// let y: u16 = reader.read_type(Endian::Little).unwrap();
/// let z = reader.read_be::<u16>().unwrap();
///
/// assert_eq!((x, y, z), (7u32, 0xCCu16, 5u16));
/// ```
pub trait BinReaderExt: Read + Seek + Sized {
/// Read `T` from the reader with the given byte order.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_type<'a, T>(&mut self, endian: Endian) -> BinResult<T>
where
T: BinRead,
T::Args<'a>: Required,
{
self.read_type_args(endian, T::Args::args())
}
/// Read `T` from the reader assuming big-endian byte order.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_be<'a, T>(&mut self) -> BinResult<T>
where
T: BinRead,
T::Args<'a>: Required,
{
self.read_type(Endian::Big)
}
/// Read `T` from the reader assuming little-endian byte order.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_le<'a, T>(&mut self) -> BinResult<T>
where
T: BinRead,
T::Args<'a>: Required,
{
self.read_type(Endian::Little)
}
/// Read `T` from the reader assuming native-endian byte order.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_ne<'a, T>(&mut self) -> BinResult<T>
where
T: BinRead,
T::Args<'a>: Required,
{
self.read_type(Endian::NATIVE)
}
/// Read `T` from the reader with the given byte order and arguments.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
fn read_type_args<T>(&mut self, endian: Endian, args: T::Args<'_>) -> BinResult<T>
where
T: BinRead,
{
T::read_options(self, endian, args)
}
/// Read `T` from the reader, assuming big-endian byte order, using the
/// given arguments.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_be_args<T>(&mut self, args: T::Args<'_>) -> BinResult<T>
where
T: BinRead,
{
self.read_type_args(Endian::Big, args)
}
/// Read `T` from the reader, assuming little-endian byte order, using the
/// given arguments.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_le_args<T>(&mut self, args: T::Args<'_>) -> BinResult<T>
where
T: BinRead,
{
self.read_type_args(Endian::Little, args)
}
/// Read `T` from the reader, assuming native-endian byte order, using the
/// given arguments.
///
/// # Errors
///
/// If reading fails, an [`Error`](crate::Error) variant will be returned.
#[inline]
fn read_ne_args<T>(&mut self, args: T::Args<'_>) -> BinResult<T>
where
T: BinRead,
{
self.read_type_args(Endian::NATIVE, args)
}
}
impl<R: Read + Seek + Sized> BinReaderExt for R {}