1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 08:34:07 +02:00

rename Read to BitRead to avoid confusion with Read trait from std

This commit is contained in:
Robin Appelman 2019-02-28 16:01:34 +01:00
commit af83f5e3b3
6 changed files with 37 additions and 35 deletions

View file

@ -1,13 +1,13 @@
//! Automatically generate `Read` implementations for structs //! Automatically generate `BitRead` implementations for structs
//! //!
//! The implementation can be derived as long as every field in the struct implements `Read` or `ReadSized` //! The implementation can be derived as long as every field in the struct implements `BitRead` or `BitReadSized`
//! //!
//! # Examples //! # Examples
//! //!
//! ``` //! ```
//! use bitstream_reader_derive::Read; //! use bitstream_reader_derive::Read;
//! //!
//! #[derive(Read)] //! #[derive(BitRead)]
//! struct TestStruct { //! struct TestStruct {
//! foo: u8, //! foo: u8,
//! str: String, //! str: String,
@ -34,7 +34,7 @@ use syn::{
}; };
/// See the [crate documentation](index.html) for details /// See the [crate documentation](index.html) for details
#[proc_macro_derive(Read, attributes(size, size_bits))] #[proc_macro_derive(BitRead, attributes(size, size_bits))]
pub fn derive_helper_attr(input: proc_macro::TokenStream) -> proc_macro::TokenStream { pub fn derive_helper_attr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput); let input = parse_macro_input!(input as DeriveInput);
@ -52,7 +52,7 @@ pub fn derive_helper_attr(input: proc_macro::TokenStream) -> proc_macro::TokenSt
let parse = parse(&input.data, &name); let parse = parse(&input.data, &name);
let expanded = quote! { let expanded = quote! {
impl #impl_generics ::bitstream_reader::Read<_E> for #name #ty_generics #where_clause { impl #impl_generics ::bitstream_reader::BitRead<_E> for #name #ty_generics #where_clause {
fn read(stream: &mut ::bitstream_reader::BitStream<_E>) -> ::bitstream_reader::Result<Self> { fn read(stream: &mut ::bitstream_reader::BitStream<_E>) -> ::bitstream_reader::Result<Self> {
#parse #parse
} }

View file

@ -1,7 +1,7 @@
use bitstream_reader::{BitBuffer, BitStream, LittleEndian}; use bitstream_reader::{BitBuffer, BitStream, LittleEndian};
use bitstream_reader_derive::Read; use bitstream_reader_derive::BitRead;
#[derive(Read, PartialEq, Debug)] #[derive(BitRead, PartialEq, Debug)]
struct TestStruct { struct TestStruct {
foo: u8, foo: u8,
str: String, str: String,

View file

@ -8,11 +8,11 @@
//! Once you have a BitStream, there are 2 different approaches of reading data //! Once you have a BitStream, there are 2 different approaches of reading data
//! //!
//! - read primitives, Strings and byte arrays, using [`read_bool`], [`read_int`], [`read_float`], [`read_byes`] and [`read_string`] //! - read primitives, Strings and byte arrays, using [`read_bool`], [`read_int`], [`read_float`], [`read_byes`] and [`read_string`]
//! - read any type implementing the [`Read`] or [`ReadSized`] traits using [`read`] and [`read_sized`] //! - read any type implementing the [`BitRead`] or [`BitReadSized`] traits using [`read`] and [`read_sized`]
//! - [`Read`] is for types that can be read without requiring any size info (e.g. null-terminal strings, floats, whole integers, etc) //! - [`BitRead`] is for types that can be read without requiring any size info (e.g. null-terminal strings, floats, whole integers, etc)
//! - [`ReadSized`] is for types that require external sizing information to be read (fixed length strings, arbitrary length integers //! - [`BitReadSized`] is for types that require external sizing information to be read (fixed length strings, arbitrary length integers
//! //!
//! The [`Read`] trait can be used with `#[derive]` if all fields implement [`Read`] or [`ReadSized`], //! The [`BitRead`] trait can be used with `#[derive]` if all fields implement [`BitRead`] or [`BitReadSized`],
//! when `derive`d for structs, it will read all fields in the struct in the order they are defined in. //! when `derive`d for structs, it will read all fields in the struct in the order they are defined in.
//! //!
//! # Examples //! # Examples
@ -35,8 +35,8 @@
//! [`read_float`]: struct.BitStream.html#method.read_float //! [`read_float`]: struct.BitStream.html#method.read_float
//! [`read_byes`]: struct.BitStream.html#method.read_bytes //! [`read_byes`]: struct.BitStream.html#method.read_bytes
//! [`read_string`]: struct.BitStream.html#method.read_string //! [`read_string`]: struct.BitStream.html#method.read_string
//! [`Read`]: trait.Read.html //! [`BitRead`]: trait.BitRead.html
//! [`ReadSized`]: trait.ReadSized.html //! [`BitReadSized`]: trait.BitReadSized.html
#![warn(missing_docs)] #![warn(missing_docs)]
//#![feature(test)] //#![feature(test)]
@ -49,10 +49,10 @@ use std::fmt;
use std::fmt::Display; use std::fmt::Display;
pub use std::string::FromUtf8Error; pub use std::string::FromUtf8Error;
pub use bitstream_reader_derive::Read; pub use bitstream_reader_derive::BitRead;
pub use buffer::BitBuffer; pub use buffer::BitBuffer;
pub use endianness::*; pub use endianness::*;
pub use read::{Read, ReadSized}; pub use read::{BitRead, BitReadSized};
pub use stream::BitStream; pub use stream::BitStream;
mod buffer; mod buffer;

View file

@ -2,16 +2,16 @@ use crate::{BitStream, Endianness, Result};
/// Trait for types that can be read from a stream without requiring the size to be configured /// Trait for types that can be read from a stream without requiring the size to be configured
/// ///
/// The `Read` trait can be used with `#[derive]` is all fields implement `Read` or `ReadSized`, /// The `BitRead` trait can be used with `#[derive]` if all fields implement `BitRead` or [`BitReadSized`],
/// when `derive`d for structs, it will read all fields in the struct in the order they are defined in. /// when `derive`d for structs, it will read all fields in the struct in the order they are defined in.
/// If a field only implements `ReadSized` then the size needs to be defined using a field attribute. /// If a field only implements [`BitReadSized`] then the size needs to be defined using a field attribute.
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use bitstream_reader::Read; /// use bitstream_reader::BitRead;
/// ///
/// #[derive(Read)] /// #[derive(BitRead)]
/// struct TestStruct { /// struct TestStruct {
/// foo: u8, /// foo: u8,
/// str: String, /// str: String,
@ -27,14 +27,16 @@ use crate::{BitStream, Endianness, Result};
/// previous_field: u8, /// previous_field: u8,
/// } /// }
/// ``` /// ```
pub trait Read<E: Endianness>: Sized { ///
/// [`BitReadSized`]: trait.BitReadSized.html
pub trait BitRead<E: Endianness>: Sized {
/// Read the type from stream /// Read the type from stream
fn read(stream: &mut BitStream<E>) -> Result<Self>; fn read(stream: &mut BitStream<E>) -> Result<Self>;
} }
macro_rules! impl_read_int { macro_rules! impl_read_int {
($type:ty, $len:expr) => { ($type:ty, $len:expr) => {
impl<E: Endianness> Read<E> for $type { impl<E: Endianness> BitRead<E> for $type {
#[inline(always)] #[inline(always)]
fn read(stream: &mut BitStream<E>) -> Result<$type> { fn read(stream: &mut BitStream<E>) -> Result<$type> {
stream.read_int::<$type>($len) stream.read_int::<$type>($len)
@ -54,28 +56,28 @@ impl_read_int!(i32, 32);
impl_read_int!(i64, 64); impl_read_int!(i64, 64);
impl_read_int!(i128, 128); impl_read_int!(i128, 128);
impl<E: Endianness> Read<E> for f32 { impl<E: Endianness> BitRead<E> for f32 {
#[inline(always)] #[inline(always)]
fn read(stream: &mut BitStream<E>) -> Result<f32> { fn read(stream: &mut BitStream<E>) -> Result<f32> {
stream.read_float::<f32>() stream.read_float::<f32>()
} }
} }
impl<E: Endianness> Read<E> for f64 { impl<E: Endianness> BitRead<E> for f64 {
#[inline(always)] #[inline(always)]
fn read(stream: &mut BitStream<E>) -> Result<f64> { fn read(stream: &mut BitStream<E>) -> Result<f64> {
stream.read_float::<f64>() stream.read_float::<f64>()
} }
} }
impl<E: Endianness> Read<E> for bool { impl<E: Endianness> BitRead<E> for bool {
#[inline(always)] #[inline(always)]
fn read(stream: &mut BitStream<E>) -> Result<bool> { fn read(stream: &mut BitStream<E>) -> Result<bool> {
stream.read_bool() stream.read_bool()
} }
} }
impl<E: Endianness> Read<E> for String { impl<E: Endianness> BitRead<E> for String {
#[inline(always)] #[inline(always)]
fn read(stream: &mut BitStream<E>) -> Result<String> { fn read(stream: &mut BitStream<E>) -> Result<String> {
stream.read_string(None) stream.read_string(None)
@ -83,14 +85,14 @@ impl<E: Endianness> Read<E> for String {
} }
/// Trait for types that can be read from a stream wit requiring the size to be configured /// Trait for types that can be read from a stream wit requiring the size to be configured
pub trait ReadSized<E: Endianness>: Sized { pub trait BitReadSized<E: Endianness>: Sized {
/// Read the type from stream /// Read the type from stream
fn read(stream: &mut BitStream<E>, size: usize) -> Result<Self>; fn read(stream: &mut BitStream<E>, size: usize) -> Result<Self>;
} }
macro_rules! impl_read_int_sized { macro_rules! impl_read_int_sized {
($type:ty) => { ($type:ty) => {
impl<E: Endianness> ReadSized<E> for $type { impl<E: Endianness> BitReadSized<E> for $type {
#[inline(always)] #[inline(always)]
fn read(stream: &mut BitStream<E>, size: usize) -> Result<$type> { fn read(stream: &mut BitStream<E>, size: usize) -> Result<$type> {
stream.read_int::<$type>(size) stream.read_int::<$type>(size)
@ -110,7 +112,7 @@ impl_read_int_sized!(i32);
impl_read_int_sized!(i64); impl_read_int_sized!(i64);
impl_read_int_sized!(i128); impl_read_int_sized!(i128);
impl<E: Endianness> ReadSized<E> for String { impl<E: Endianness> BitReadSized<E> for String {
#[inline(always)] #[inline(always)]
fn read(stream: &mut BitStream<E>, size: usize) -> Result<String> { fn read(stream: &mut BitStream<E>, size: usize) -> Result<String> {
stream.read_string(Some(size)) stream.read_string(Some(size))
@ -118,7 +120,7 @@ impl<E: Endianness> ReadSized<E> for String {
} }
/// Read a boolean, if true, read `T`, else return `None` /// Read a boolean, if true, read `T`, else return `None`
impl<E: Endianness, T: Read<E>> Read<E> for Option<T> { impl<E: Endianness, T: BitRead<E>> BitRead<E> for Option<T> {
fn read(stream: &mut BitStream<E>) -> Result<Self> { fn read(stream: &mut BitStream<E>) -> Result<Self> {
if stream.read()? { if stream.read()? {
Ok(Some(stream.read()?)) Ok(Some(stream.read()?))
@ -129,7 +131,7 @@ impl<E: Endianness, T: Read<E>> Read<E> for Option<T> {
} }
/// Read `T` `size` times and return as `Vec<T>` /// Read `T` `size` times and return as `Vec<T>`
impl<E: Endianness, T: Read<E>> ReadSized<E> for Vec<T> { impl<E: Endianness, T: BitRead<E>> BitReadSized<E> for Vec<T> {
fn read(stream: &mut BitStream<E>, size: usize) -> Result<Self> { fn read(stream: &mut BitStream<E>, size: usize) -> Result<Self> {
let mut vec = Vec::with_capacity(size); let mut vec = Vec::with_capacity(size);
for _ in 0..size { for _ in 0..size {

View file

@ -7,7 +7,7 @@ use num_traits::{Float, PrimInt};
use crate::endianness::Endianness; use crate::endianness::Endianness;
use crate::is_signed::IsSigned; use crate::is_signed::IsSigned;
use crate::BitBuffer; use crate::BitBuffer;
use crate::{Read, ReadError, ReadSized, Result}; use crate::{BitRead, ReadError, BitReadSized, Result};
/// Stream that provides an easy way to iterate trough a [`BitBuffer`] /// Stream that provides an easy way to iterate trough a [`BitBuffer`]
/// ///
@ -488,7 +488,7 @@ where
/// # } /// # }
/// ``` /// ```
/// #[inline(always)] /// #[inline(always)]
pub fn read<T: Read<E>>(&mut self) -> Result<T> { pub fn read<T: BitRead<E>>(&mut self) -> Result<T> {
T::read(self) T::read(self)
} }
@ -513,7 +513,7 @@ where
/// # } /// # }
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn read_sized<T: ReadSized<E>>(&mut self, size: usize) -> Result<T> { pub fn read_sized<T: BitReadSized<E>>(&mut self, size: usize) -> Result<T> {
T::read(self, size) T::read(self, size)
} }
} }

View file

@ -1,4 +1,4 @@
use bitstream_reader::{BigEndian, BitBuffer, BitStream, LittleEndian, Read}; use bitstream_reader::{BigEndian, BitBuffer, BitStream, LittleEndian, BitRead};
// for bench on nightly // for bench on nightly
//use std::fs; //use std::fs;
@ -272,7 +272,7 @@ fn read_sized_trait() {
assert_eq!(vec![0b1011_0101, 0b0110_1010, 0b1010_1100], vec); assert_eq!(vec![0b1011_0101, 0b0110_1010, 0b1010_1100], vec);
} }
#[derive(Read, PartialEq, Debug)] #[derive(BitRead, PartialEq, Debug)]
struct TestStruct { struct TestStruct {
foo: u8, foo: u8,
str: String, str: String,