pub struct Prefix { /* private fields */ }
Expand description
A section prefix, i.e. a sequence of bits specifying the part of the network’s name space consisting of all names that start with this sequence.
Implementations
sourceimpl Prefix
impl Prefix
sourcepub fn new(bit_count: usize, name: XorName) -> Self
pub fn new(bit_count: usize, name: XorName) -> Self
Creates a new Prefix
with the first bit_count
bits of name
. Insignificant bits are all
set to 0.
sourcepub fn pushed(self, bit: bool) -> Self
pub fn pushed(self, bit: bool) -> Self
Returns self
with an appended bit: 0
if bit
is false
, and 1
if bit
is true
. If
self.bit_count
is already at the maximum for this type, then an unmodified copy of self
is returned.
sourcepub fn popped(self) -> Self
pub fn popped(self) -> Self
Returns a prefix copying the first bitcount() - 1
bits from self
,
or self
if it is already empty.
sourcepub fn is_compatible(&self, other: &Self) -> bool
pub fn is_compatible(&self, other: &Self) -> bool
Returns true
if self
is a prefix of other
or vice versa.
sourcepub fn is_extension_of(&self, other: &Self) -> bool
pub fn is_extension_of(&self, other: &Self) -> bool
Returns true
if other
is compatible but strictly shorter than self
.
sourcepub fn is_neighbour(&self, other: &Self) -> bool
pub fn is_neighbour(&self, other: &Self) -> bool
Returns true
if the other
prefix differs in exactly one bit from this one.
sourcepub fn common_prefix(&self, name: &XorName) -> usize
pub fn common_prefix(&self, name: &XorName) -> usize
Returns the number of common leading bits with the input name, capped with prefix length.
sourcepub fn matches(&self, name: &XorName) -> bool
pub fn matches(&self, name: &XorName) -> bool
Returns true
if this is a prefix of the given name
.
sourcepub fn cmp_distance(&self, other: &Self, target: &XorName) -> Ordering
pub fn cmp_distance(&self, other: &Self, target: &XorName) -> Ordering
Compares the distance of self
and other
to target
. Returns Less
if self
is closer,
Greater
if other
is closer, and compares the prefix directly if of equal distance
(this is to make sorting deterministic).
sourcepub fn cmp_breadth_first(&self, other: &Self) -> Ordering
pub fn cmp_breadth_first(&self, other: &Self) -> Ordering
Compares the prefixes using breadth-first order. That is, shorter prefixes are ordered
before longer. This is in contrast with the default Ord
impl of Prefix
which uses
depth-first order.
sourcepub fn lower_bound(&self) -> XorName
pub fn lower_bound(&self) -> XorName
Returns the smallest name matching the prefix
sourcepub fn upper_bound(&self) -> XorName
pub fn upper_bound(&self) -> XorName
Returns the largest name matching the prefix
sourcepub fn range_inclusive(&self) -> RangeInclusive<XorName>
pub fn range_inclusive(&self) -> RangeInclusive<XorName>
Inclusive range from lower_bound to upper_bound
sourcepub fn is_covered_by<'a, I>(&self, prefixes: I) -> bool where
I: IntoIterator<Item = &'a Self> + Clone,
pub fn is_covered_by<'a, I>(&self, prefixes: I) -> bool where
I: IntoIterator<Item = &'a Self> + Clone,
Returns whether the namespace defined by self
is covered by prefixes in the prefixes
set
sourcepub fn with_flipped_bit(&self, i: u8) -> Self
pub fn with_flipped_bit(&self, i: u8) -> Self
Returns the neighbouring prefix differing in the i
-th bit
If i
is larger than our bit count, self
is returned
sourcepub fn substituted_in(&self, name: XorName) -> XorName
pub fn substituted_in(&self, name: XorName) -> XorName
Returns the given name
with first bits replaced by self
sourcepub fn sibling(&self) -> Self
pub fn sibling(&self) -> Self
Returns the same prefix, with the last bit flipped, or unchanged, if empty.
Trait Implementations
sourceimpl<'de> Deserialize<'de> for Prefix
impl<'de> Deserialize<'de> for Prefix
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Display for Prefix
impl Display for Prefix
Format Prefix
as bit string, e.g. "010"
with a Prefix::bit_count
of 3
.
sourceimpl Ord for Prefix
impl Ord for Prefix
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl PartialOrd<Prefix> for Prefix
impl PartialOrd<Prefix> for Prefix
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Copy for Prefix
impl Eq for Prefix
impl StructuralEq for Prefix
Auto Trait Implementations
impl RefUnwindSafe for Prefix
impl Send for Prefix
impl Sync for Prefix
impl Unpin for Prefix
impl UnwindSafe for Prefix
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more