Trait routing::Xorable
[−]
[src]
pub trait Xorable: Ord + Sized { fn common_prefix(&self, other: &Self) -> usize; fn cmp_distance(&self, lhs: &Self, rhs: &Self) -> Ordering; fn bit(&self, i: usize) -> bool; fn differs_in_bit(&self, other: &Self, i: usize) -> bool; fn with_flipped_bit(self, i: usize) -> Self; fn with_bit(self, i: usize, bit: bool) -> Self; fn binary(&self) -> String; fn debug_binary(&self) -> String; fn set_remaining(self, n: usize, val: bool) -> Self; fn from_hash<T: AsRef<[u8]>>(hash: T) -> Self; fn bit_len() -> usize { ... } }
A sequence of bits, as a point in XOR space.
These are considered points in a space with the XOR metric, and need to implement the
functionality required by RoutingTable
to use them as node names.
Required Methods
fn common_prefix(&self, other: &Self) -> usize
Returns the length of the common prefix with the other
name; e. g.
the when other = 11110000
and self = 11111111
this is 4.
fn cmp_distance(&self, lhs: &Self, rhs: &Self) -> Ordering
Compares the distance of the arguments to self
. Returns Less
if lhs
is closer,
Greater
if rhs
is closer, and Equal
if lhs == rhs
. (The XOR distance can only be
equal if the arguments are equal.)
fn bit(&self, i: usize) -> bool
Returns true
if the i
-th bit is 1
.
fn differs_in_bit(&self, other: &Self, i: usize) -> bool
Returns true
if the i
-th bit of other has a different value to the i
-th bit of self
.
fn with_flipped_bit(self, i: usize) -> Self
Returns a copy of self
, with the index
-th bit flipped.
If index
exceeds the number of bits in self
, an unmodified copy of self
is returned.
fn with_bit(self, i: usize, bit: bool) -> Self
Returns a copy of self
, with the index
-th bit set to bit
.
If index
exceeds the number of bits in self
, an unmodified copy of self
is returned.
fn binary(&self) -> String
Returns a binary format string, with leading zero bits included.
fn debug_binary(&self) -> String
Returns a binary debug format string of ????????...????????
fn set_remaining(self, n: usize, val: bool) -> Self
Returns a copy of self with first n
bits preserved, and remaining bits
set to 0 (val == false) or 1 (val == true).
fn from_hash<T: AsRef<[u8]>>(hash: T) -> Self
Returns a Self
instance constructed from an array of bytes.