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>
impl<const N: usize, I: Bucketable> Bucket<N, I>
Sourcepub fn push(&mut self, item: I) -> Option<I>
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());
Sourcepub fn remove(&mut self, item: &I) -> Option<I>
pub fn remove(&mut self, item: &I) -> Option<I>
Will attempt to remove an item from the bucket.
Sourcepub fn len_bucket(&self, discriminant: &I::Discriminant) -> Option<usize>
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.
Sourcepub fn get_random(&mut self) -> Option<&I>
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§
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>
impl<const N: usize, I> Sync for Bucket<N, I>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
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