Crate cuprate_p2p_bucket

Source
Expand description

Bucket data structure

A collection data structure that discriminates its unique items and place them into “buckets”.

The item must implement the Bucketable trait that defines how to create the discriminant from the item type. The data structure will internally contain any item into “buckets” or vectors of sized capacity N that regroup all the stored items with this specific discriminant.

A practical example of this data structure is for storing N amount of IP discriminated by their subnets. You can store in each “buckets” corresponding to a /16 subnet up to N IPs of that subnet.

§Example

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

// Create a new bucket that can store at most 2 IPs in a particular `/16` subnet.
let mut bucket = Bucket::<2,Ipv4Addr>::new();

// Fulfill the `96.96.0.0/16` bucket.
bucket.push("96.96.0.1".parse().unwrap());
bucket.push("96.96.0.2".parse().unwrap());
assert_eq!(2, bucket.len());
assert_eq!(2, bucket.len_bucket(&[96_u8,96_u8]).unwrap());

// Push a new IP from another subnet
bucket.push("127.0.0.1".parse().unwrap());
assert_eq!(3, bucket.len());
assert_eq!(2, bucket.len_bucket(&[96_u8,96_u8]).unwrap());
assert_eq!(1, bucket.len_bucket(&[127_u8,0_u8]).unwrap());

// Attempting to push a new IP within `96.96.0.0/16` bucket will return the IP back
// as this subnet is already full.
let pushed = bucket.push("96.96.0.3".parse().unwrap());
assert!(pushed.is_some());
assert_eq!(2, bucket.len_bucket(&[96_u8,96_u8]).unwrap());

Structs§

  • 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.

Traits§

  • A discriminant that can be computed from the type.