Struct deku::error::NeedSize

source ·
pub struct NeedSize { /* private fields */ }
Expand description

Number of bits needed to retry parsing

Implementations§

Create new NeedSize from bits

Examples found in repository?
src/impls/primitive.rs (line 27)
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
    fn read(
        input: &BitSlice<u8, Msb0>,
        (_, size): (Endian, ByteSize),
    ) -> Result<(&BitSlice<u8, Msb0>, Self), DekuError> {
        const MAX_TYPE_BITS: usize = BitSize::of::<u8>().0;
        let bit_size: usize = size.0 * 8;

        // TODO
        // if they never give [bits] or [bytes] we don't need to check the size
        if bit_size > MAX_TYPE_BITS {
            return Err(DekuError::Parse(format!(
                "too much data: container of {} bits cannot hold {} bits",
                MAX_TYPE_BITS, bit_size
            )));
        }

        if input.len() < bit_size {
            return Err(DekuError::Incomplete(crate::error::NeedSize::new(bit_size)));
        }

        let (bit_slice, rest) = input.split_at(bit_size);
        let pad = 8 * ((bit_slice.len() + 7) / 8) - bit_slice.len();

        let value = if pad == 0
            && bit_slice.len() == MAX_TYPE_BITS
            && bit_slice.domain().region().unwrap().1.len() * 8 == MAX_TYPE_BITS
        {
            // if everything is aligned, just read the value
            bit_slice.load::<u8>()
        } else {
            let mut bits: BitVec<u8, Msb0> = BitVec::with_capacity(bit_slice.len() + pad);

            // Copy bits to new BitVec
            bits.extend_from_bitslice(bit_slice);

            // Force align
            //i.e. [1110, 10010110] -> [11101001, 0110]
            bits.force_align();

            let bytes: &[u8] = bits.as_raw_slice();

            // cannot use from_X_bytes as we don't have enough bytes for $typ
            // read manually
            let mut res: u8 = 0;
            for b in bytes.iter().rev() {
                res |= *b as u8;
            }

            res as u8
        };

        Ok((rest, value))
    }

Number of bits needed

Examples found in repository?
src/error.rs (line 72)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match *self {
            DekuError::Incomplete(ref size) => write!(
                f,
                "Not enough data, need {} bits (or {} bytes)",
                size.bit_size(),
                size.byte_size()
            ),
            DekuError::Parse(ref err) => write!(f, "Parse error: {}", err),
            DekuError::InvalidParam(ref err) => write!(f, "Invalid param error: {}", err),
            DekuError::Unexpected(ref err) => write!(f, "Unexpected error: {}", err),
            DekuError::Assertion(ref err) => write!(f, "Assertion error: {}", err),
            DekuError::IdVariantNotFound => write!(f, "Could not resolve `id` for variant"),
        }
    }

Number of bytes needed

Examples found in repository?
src/error.rs (line 73)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match *self {
            DekuError::Incomplete(ref size) => write!(
                f,
                "Not enough data, need {} bits (or {} bytes)",
                size.bit_size(),
                size.byte_size()
            ),
            DekuError::Parse(ref err) => write!(f, "Parse error: {}", err),
            DekuError::InvalidParam(ref err) => write!(f, "Invalid param error: {}", err),
            DekuError::Unexpected(ref err) => write!(f, "Unexpected error: {}", err),
            DekuError::Assertion(ref err) => write!(f, "Assertion error: {}", err),
            DekuError::IdVariantNotFound => write!(f, "Could not resolve `id` for variant"),
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts self into T using Into<T>. Read more
Causes self to use its Binary implementation when Debug-formatted.
Causes self to use its Display implementation when Debug-formatted.
Causes self to use its LowerExp implementation when Debug-formatted.
Causes self to use its LowerHex implementation when Debug-formatted.
Causes self to use its Octal implementation when Debug-formatted.
Causes self to use its Pointer implementation when Debug-formatted.
Causes self to use its UpperExp implementation when Debug-formatted.
Causes self to use its UpperHex implementation when Debug-formatted.
Formats each item in a sequence. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Pipes by value. This is generally the method you want to use. Read more
Borrows self and passes that borrow into the pipe function. Read more
Mutably borrows self and passes that borrow into the pipe function. Read more
Borrows self, then passes self.borrow() into the pipe function. Read more
Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Borrows self, then passes self.as_ref() into the pipe function.
Mutably borrows self, then passes self.as_mut() into the pipe function.
Borrows self, then passes self.deref() into the pipe function.
Mutably borrows self, then passes self.deref_mut() into the pipe function.
Immutable access to a value. Read more
Mutable access to a value. Read more
Immutable access to the Borrow<B> of a value. Read more
Mutable access to the BorrowMut<B> of a value. Read more
Immutable access to the AsRef<R> view of a value. Read more
Mutable access to the AsMut<R> view of a value. Read more
Immutable access to the Deref::Target of a value. Read more
Mutable access to the Deref::Target of a value. Read more
Calls .tap() only in debug builds, and is erased in release builds.
Calls .tap_mut() only in debug builds, and is erased in release builds.
Calls .tap_borrow() only in debug builds, and is erased in release builds.
Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Calls .tap_ref() only in debug builds, and is erased in release builds.
Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Calls .tap_deref() only in debug builds, and is erased in release builds.
Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Attempts to convert self into T using TryInto<T>. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.