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

docs for derive

This commit is contained in:
Robin Appelman 2019-02-28 00:08:35 +01:00
commit b45ca3856e
3 changed files with 29 additions and 3 deletions

View file

@ -17,7 +17,7 @@
//! float: f32, //! float: f32,
//! #[size = 3] //! #[size = 3]
//! asd: u8, //! asd: u8,
//! #[size_bits = 2] // first read 2 bits, then use the resulting value as size as size for the read //! #[size_bits = 2] // first read 2 bits as unsigned integer, then use the resulting value as size for the read
//! dynamic_length: u8, //! dynamic_length: u8,
//! #[size = "asd"] // use a previously defined field as size //! #[size = "asd"] // use a previously defined field as size
//! previous_field: u8, //! previous_field: u8,

View file

@ -12,7 +12,8 @@
//! - [`Read`] is for types that can be read without requiring any size info (e.g. null-terminal strings, floats, whole integers, etc) //! - [`Read`] 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 //! - [`ReadSized`] 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`],
//! when `derive`d for structs, it will read all fields in the struct in the order they are defined in.
//! //!
//! # Examples //! # Examples
//! //!
@ -25,7 +26,6 @@
//! ]; //! ];
//! let buffer = BitBuffer::new(bytes, LittleEndian); //! let buffer = BitBuffer::new(bytes, LittleEndian);
//! let stream = BitStream::new(buffer); //! let stream = BitStream::new(buffer);
//!
//! ``` //! ```
//! //!
//! [`BitBuffer`]: struct.BitBuffer.html //! [`BitBuffer`]: struct.BitBuffer.html

View file

@ -1,6 +1,32 @@
use crate::{BitStream, Endianness, Result}; 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`,
/// 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.
///
/// # Examples
///
/// ```
/// use bitstream_reader::Read;
///
/// #[derive(Read)]
/// struct TestStruct {
/// foo: u8,
/// str: String,
/// #[size = 2] // when `size` is set, the attributed will be read using `read_sized`
/// truncated: String,
/// bar: u16,
/// float: f32,
/// #[size = 3]
/// asd: u8,
/// #[size_bits = 2] // first read 2 bits as unsigned integer, then use the resulting value as size for the read
/// dynamic_length: u8,
/// #[size = "asd"] // use a previously defined field as size
/// previous_field: u8,
/// }
/// ```
pub trait Read<E: Endianness>: Sized { pub trait Read<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>;