Skip to main content

CountPrefix

Trait CountPrefix 

Source
pub trait CountPrefix: Bits + Sealed {
    // Required methods
    fn try_from_count(len: usize) -> Result<Self, WidthError>
       where Self: Sized;
    fn to_count(self) -> usize;
}
Expand description

Checked length ⇄ wire-prefix conversions: the types usable as a length/count prefix — by the #[brw(count_prefix = <Ty>)] directive and by the codecs::prefixed string codec.

The prefix is computed from len() on encode and turned back into an element count on decode. try_from_count is checked and never truncates: the length is widened (never narrowed) before the range compare, so 300 elements against a u8 prefix is WidthError::ValueTooLarge — not a silently wrapped 44.

Required Methods§

Source

fn try_from_count(len: usize) -> Result<Self, WidthError>
where Self: Sized,

The prefix for a collection of len elements, or [WidthError::ValueTooLarge] when len exceeds the prefix’s range.

Source

fn to_count(self) -> usize

The prefix as an element count.

For a u64/u128 prefix on a 32-bit target this is a wrapping narrow — the same n as usize the hand-written triad performs; harmless under the codec’s push-based count loop (no pre-allocation, bounded by the input).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl CountPrefix for u8

Source§

impl CountPrefix for u16

Source§

impl CountPrefix for u32

Source§

impl CountPrefix for u64

Source§

impl CountPrefix for u128

Implementors§

Source§

impl<const N: usize> CountPrefix for UInt<u8, N>

Source§

impl<const N: usize> CountPrefix for UInt<u16, N>

Source§

impl<const N: usize> CountPrefix for UInt<u32, N>

Source§

impl<const N: usize> CountPrefix for UInt<u64, N>

Source§

impl<const N: usize> CountPrefix for UInt<u128, N>