cuprate_p2p_bucket

Struct Bucket

Source
pub struct Bucket<const N: usize, I: Bucketable> { /* private fields */ }
Expand description

A collection data structure discriminating its unique items with a specified method. Limiting the amount of items stored with that discriminant to the const N.

Implementations§

Source§

impl<const N: usize, I: Bucketable> Bucket<N, I>

Source

pub const fn new() -> Self

Create a new Bucket

Source

pub fn push(&mut self, item: I) -> Option<I>

Push a new element into the Bucket

Will internally create a new vector for each new discriminant being generated from an item.

This function WILL NOT push the element if it already exists.

Return None if the item has been pushed or ignored. Some(I) if the vector is full.

§Example
use cuprate_p2p_bucket::Bucket;
use std::net::Ipv4Addr;

let mut bucket = Bucket::<8,Ipv4Addr>::new();

// Push a first IP address.
bucket.push("127.0.0.1".parse().unwrap());
assert_eq!(1, bucket.len());

// Push the same IP address a second time.
bucket.push("127.0.0.1".parse().unwrap());
assert_eq!(1, bucket.len());
Source

pub fn remove(&mut self, item: &I) -> Option<I>

Will attempt to remove an item from the bucket.

Source

pub fn len(&self) -> usize

Return the number of item stored within the storage

Source

pub fn len_bucket(&self, discriminant: &I::Discriminant) -> Option<usize>

Return the number of item stored with a specific discriminant.

This method returns None if the bucket with this discriminant doesn’t exist.

Source

pub fn is_empty(&self) -> bool

Return true if the storage contains no items

Source

pub fn get_random(&mut self) -> Option<&I>

Return a reference to an item chosen at random.

Repeated use of this function will provide a normal distribution of items based on their discriminants.

Trait Implementations§

Source§

impl<const N: usize, I: Bucketable> Default for Bucket<N, I>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<const N: usize, I> Freeze for Bucket<N, I>

§

impl<const N: usize, I> RefUnwindSafe for Bucket<N, I>

§

impl<const N: usize, I> Send for Bucket<N, I>
where <I as Bucketable>::Discriminant: Send, I: Send,

§

impl<const N: usize, I> Sync for Bucket<N, I>
where <I as Bucketable>::Discriminant: Sync, I: Sync,

§

impl<const N: usize, I> Unpin for Bucket<N, I>

§

impl<const N: usize, I> UnwindSafe for Bucket<N, I>

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> 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, 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

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 24 bytes