mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
allow deriving BitReadSized for enums
This commit is contained in:
parent
5ec499f42f
commit
a2b0d4ffb4
4 changed files with 113 additions and 22 deletions
|
|
@ -48,7 +48,7 @@ use std::fmt;
|
|||
use std::fmt::Display;
|
||||
pub use std::string::FromUtf8Error;
|
||||
|
||||
pub use bitstream_reader_derive::BitRead;
|
||||
pub use bitstream_reader_derive::{BitRead, BitReadSized};
|
||||
pub use buffer::BitBuffer;
|
||||
pub use endianness::*;
|
||||
pub use read::{BitRead, BitReadSized};
|
||||
|
|
|
|||
33
src/read.rs
33
src/read.rs
|
|
@ -40,10 +40,12 @@ use crate::{BitStream, Endianness, Result};
|
|||
///
|
||||
/// # Enums
|
||||
///
|
||||
/// The implementation can be derived for an enum as long as every variant of the enum either has no field, or an unnamed field that implements `BitRead`
|
||||
/// The implementation can be derived for an enum as long as every variant of the enum either has no field, or an unnamed field that implements `BitRead` or [`BitReadSized`]
|
||||
///
|
||||
/// The enum is read by first reading a set number of bits as the discriminant of the enum, then the variant for the read discriminant is read.
|
||||
///
|
||||
/// For details about setting the input size for fields implementing [`BitReadSized`] see the block about size in the `Structs` section above.
|
||||
///
|
||||
/// The discriminant for the variants defaults to incrementing by one for every field, starting with `0`.
|
||||
/// You can overwrite the discriminant for a field, which will also change the discriminant for every following field.
|
||||
///
|
||||
|
|
@ -67,6 +69,7 @@ use crate::{BitStream, Endianness, Result};
|
|||
/// #[derive(BitRead)]
|
||||
/// #[discriminant_bits = 2]
|
||||
/// enum TestUnnamedFieldEnum {
|
||||
/// #[size = 5]
|
||||
/// Foo(i8),
|
||||
/// Bar(bool),
|
||||
/// #[discriminant = 3] // since rust only allows setting the discriminant on field-less enums, you can use an attribute instead
|
||||
|
|
@ -165,6 +168,34 @@ impl<E: Endianness> BitRead<E> for String {
|
|||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Enums
|
||||
///
|
||||
/// The implementation can be derived for an enum as long as every variant of the enum either has no field, or an unnamed field that implements [`BitRead`] or `BitReadSized`
|
||||
///
|
||||
/// The enum is read by first reading a set number of bits as the discriminant of the enum, then the variant for the read discriminant is read.
|
||||
///
|
||||
/// For details about setting the input size for fields implementing `BitReadSized` see the block about size in the `Structs` section above.
|
||||
///
|
||||
/// The discriminant for the variants defaults to incrementing by one for every field, starting with `0`.
|
||||
/// You can overwrite the discriminant for a field, which will also change the discriminant for every following field.
|
||||
///
|
||||
/// ## Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use bitstream_reader_derive::BitReadSized;
|
||||
/// #
|
||||
/// #[derive(BitReadSized)]
|
||||
/// #[discriminant_bits = 2]
|
||||
/// enum TestUnnamedFieldEnum {
|
||||
/// #[size = 5]
|
||||
/// Foo(i8),
|
||||
/// Bar(bool),
|
||||
/// #[discriminant = 3] // since rust only allows setting the discriminant on field-less enums, you can use an attribute instead
|
||||
/// #[size = "input_size"]
|
||||
/// Asd(u8),
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// [`BitRead`]: trait.BitRead.html
|
||||
/// [read_sized]: struct.BitStream.html#method.read_sized
|
||||
/// [read]: struct.BitStream.html#method.read
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue