Struct Id

Source
pub struct Id { /* private fields */ }
Expand description

Represents the id of a Node or a BitTorrent info-hash. Basically, it’s a 20-byte identifier.

Implementations§

Source§

impl Id

Source

pub const ZERO: Self

An Id with all of its bits set to 0

Source

pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Id, RustyDHTError>

Create a new Id from some bytes. Returns Err if bytes is not of length ID_SIZE.

Source

pub fn from_ip(ip: &IpAddr) -> Id

Generates a random Id for a mainline DHT node with the provided IP address. The generated Id will be valid with respect to BEP0042.

Source

pub fn from_random<T: RngCore>(rng: &mut T) -> Id

Generates a completely random Id. The returned Id is not guaranteed to be valid with respect to BEP0042.

Source

pub fn to_vec(&self) -> Vec<u8>

Copies the byes that make up the Id and returns them in a Vec

Source

pub fn is_valid_for_ip(&self, ip: &IpAddr) -> bool

Evaluates the Id and decides if it’s a valid Id for a DHT node with the provided IP address (based on BEP0042). Note: the current implementation does not handle non-globally-routable address space properly. It will likely return false for non-routable IPv4 address space (against the spec).

Source

pub fn matching_prefix_bits(&self, other: &Self) -> usize

Returns the number of bits of prefix that the two ids have in common.

Consider two Ids with binary values 10100000 and 10100100. This function would return 5 because the Ids share the common 5-bit prefix 10100.

Source

pub fn from_hex(h: &str) -> Result<Id, RustyDHTError>

Creates an Id from a hex string.

For example: let id = Id::from_hex("88ffb73943354a00dc2dadd14c54d28020a513c8").unwrap();

Source

pub fn xor(&self, other: &Id) -> Id

Computes the exclusive or (XOR) of this Id with another. The BitTorrent DHT uses XOR as its distance metric.

Example: let distance_between_nodes = id.xor(other_id);

Source

pub fn make_mutant(&self, identical_bytes: usize) -> Result<Id, RustyDHTError>

Makes a new id that’s similar to this one. identical_bytes specifies how many bytes of the resulting Id should be the same as this. identical_bytes must be in the range (0, ID_SIZE) otherwise Err is returned.

Trait Implementations§

Source§

impl Clone for Id

Source§

fn clone(&self) -> Id

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Id

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Id

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Id

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Id

Source§

fn eq(&self, other: &Id) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Id

Source§

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 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Id

Source§

impl Eq for Id

Source§

impl StructuralPartialEq for Id

Auto Trait Implementations§

§

impl Freeze for Id

§

impl RefUnwindSafe for Id

§

impl Send for Id

§

impl Sync for Id

§

impl Unpin for Id

§

impl UnwindSafe for Id

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CallHasher for T
where T: Hash + ?Sized,

Source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V