Trait iptrie::BitSlot

source ·
pub trait BitSlot: Clone + Copy + Default + Debug + Binary + Eq + PartialEq + Hash + Not<Output = Self> + BitAnd<Output = Self> + BitOr<Output = Self> + BitXor<Output = Self> + Shl<u8, Output = Self> + Shr<u8, Output = Self> {
    const LEN: u8;

    // Required methods
    fn single_bit(pos: u8) -> Self;
    fn bitmask(len: u8) -> Self;
    fn first_bit(&self) -> u8;
    fn is_set(&self, pos: u8) -> bool;
    fn last_16_bits(&self) -> u16;
}
Expand description

A fixed-length slot of bits.

This is the slot on which all the prefix (and so the tries) computations are made.

As an example, u32 or u128 are good candidates for Ipv4 or Ipv6 prefixes. Moreover, u64 could be used to save memory for truncated Ipv6 prefixes.

Required Associated Constants§

source

const LEN: u8

The length of the slot (number of bits)

Example
assert_eq!( <u32 as BitSlot>::LEN, 32);
assert_eq!( <u64 as BitSlot>::LEN, 64);
assert_eq!( <u128 as BitSlot>::LEN, 128);

Required Methods§

source

fn single_bit(pos: u8) -> Self

Returns a slot with the bit of the specified position set to 1.

As this is a prefix, the first bits are the highest ones.

Example
assert_eq!( <u32 as BitSlot>::single_bit(30), 4);
assert_eq!( <u32 as BitSlot>::single_bit(24), 256);
assert_eq!( <u64 as BitSlot>::single_bit(60), 16);
assert_eq!( <u64 as BitSlot>::single_bit(1), u64::MAX/2 + 1);
assert_eq!( <u128 as BitSlot>::single_bit(120), 256);
source

fn bitmask(len: u8) -> Self

Returns a slot with the len first bits set to 1 and the others set to 0

source

fn first_bit(&self) -> u8

Returns the position of the first bit set to 1

If all the bits are set to 0, the position LEN+1 is returned (i.e. 33 for 32bit slot or 65 for a 64bit slot).

Example
assert_eq!( <u32 as BitSlot>::first_bit(&0), 33);
assert_eq!( <u32 as BitSlot>::first_bit(&1024), 22);
assert_eq!( <u64 as BitSlot>::first_bit(&1024), 54);
assert_eq!( <u128 as BitSlot>::first_bit(&0), 129);
source

fn is_set(&self, pos: u8) -> bool

Checks if the bit at the given position is set to 1 or not

Example
assert!( ! <u32 as BitSlot>::is_set(&0, 12));
assert!( <u32 as BitSlot>::is_set(&!0, 12));
assert!( <u32 as BitSlot>::is_set(&1024, 22));
source

fn last_16_bits(&self) -> u16

Truncates the end of the slot to the last 16 bits.

This is used to compress the trie (LC-Trie) and implicitly defines the maximum compression level (i.e. 16 bits)

Example
assert_eq!( <u32 as BitSlot>::last_16_bits(&!0), 65535);
assert_eq!( <u32 as BitSlot>::last_16_bits(&65), 65);

Implementations on Foreign Types§

source§

impl BitSlot for u64

source§

const LEN: u8 = 64u8

source§

fn first_bit(&self) -> u8

source§

fn single_bit(pos: u8) -> Self

source§

fn is_set(&self, pos: u8) -> bool

source§

fn bitmask(len: u8) -> Self

source§

fn last_16_bits(&self) -> u16

source§

impl BitSlot for u128

source§

const LEN: u8 = 128u8

source§

fn first_bit(&self) -> u8

source§

fn single_bit(pos: u8) -> Self

source§

fn is_set(&self, pos: u8) -> bool

source§

fn bitmask(len: u8) -> Self

source§

fn last_16_bits(&self) -> u16

source§

impl BitSlot for u32

source§

const LEN: u8 = 32u8

source§

fn first_bit(&self) -> u8

source§

fn single_bit(pos: u8) -> Self

source§

fn is_set(&self, pos: u8) -> bool

source§

fn bitmask(len: u8) -> Self

source§

fn last_16_bits(&self) -> u16

Implementors§