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
//! Core traits for bitframe layout types.
//!
//! These traits are implemented automatically by the `#[bitframe]` proc-macro.
//! They enable generic code over any bitframe layout.
//!
//! # Examples
//!
//! ```
//! use bitframe::traits::BitLayout;
//!
//! fn print_size<T: BitLayout>() {
//! // Use T::SIZE_BITS and T::SIZE_BYTES in generic code
//! }
//! ```
use crateError;
/// Compile-time size information for a bit-level layout.
///
/// Every `#[bitframe]` struct implements this trait, providing the total
/// size in bits and bytes. `SIZE_BYTES` is always `ceil(SIZE_BITS / 8)`.
///
/// # Examples
///
/// ```
/// use bitframe::traits::BitLayout;
///
/// // Implemented by generated types:
/// // assert_eq!(CcsdsPrimaryHeaderRef::SIZE_BITS, 48);
/// // assert_eq!(CcsdsPrimaryHeaderRef::SIZE_BYTES, 6);
/// ```
/// Zero-copy parsing from a byte slice.
///
/// Implemented by `#[bitframe]` structs. The `View` associated type is the
/// generated `FooRef<'a>` that borrows the input bytes.
///
/// # Examples
///
/// ```
/// use bitframe::prelude::*;
///
/// #[bitframe]
/// pub struct Msg {
/// pub id: u4,
/// pub data: u12,
/// }
///
/// fn parse_any<'a, T: Parseable<'a>>(bytes: &'a [u8]) -> Result<T::View, bitframe::Error>
/// where T::View: core::fmt::Debug {
/// let (view, _rest) = T::parse(bytes)?;
/// Ok(view)
/// }
///
/// let view = parse_any::<Msg>(&[0x1F, 0xFF])?;
/// # Ok::<(), bitframe::Error>(())
/// ```