Struct anchorhash::Builder
source · [−]pub struct Builder<R, B>where
B: BuildHasher,{ /* private fields */ }
Expand description
Initialise a new AnchorHash
instance.
An AnchorHash instance can be pre-populated with some set of resources using
with_resources
:
// Anything can be used as the resource!
//
// Here &str instances are used, but SockAddr instances, structs, Strings,
// or anything that makes sense for you can be used too.
let backend_servers = vec![
"cache1.itsallbroken.com",
"cache2.itsallbroken.com",
"cache3.itsallbroken.com",
];
let anchor = anchorhash::Builder::default()
.with_resources(backend_servers)
.build(100);
// In this example, strings are used as keys but anything that implements
// std::hash::Hash can be used.
let example_user_email = "dom@itsallbroken.com".to_string();
// Get a backend for this user
let backend = anchor.get_resource(example_user_email).unwrap();
println!("user mapped to: {}", backend);
By default, a builder constructs an empty AnchorHash, using the
DefaultHasher
. A DefaultHasher
is unlikely to be the fastest option
and therefore the hash implementation can be specified by the user with
with_hasher
:
use fnv::FnvBuildHasher;
// Use the Fowler–Noll–Vo hash function - fantastic at very small keys, but
// there are better algorithms for larger keys!
let mut anchor = anchorhash::Builder::with_hasher(FnvBuildHasher::default()).build(50);
Implementations
sourceimpl<R, B> Builder<R, B>where
B: BuildHasher,
impl<R, B> Builder<R, B>where
B: BuildHasher,
sourcepub fn build<K: Hash>(self, capacity: u16) -> AnchorHash<K, R, B>
pub fn build<K: Hash>(self, capacity: u16) -> AnchorHash<K, R, B>
Initialise the AnchorHash
instance with support for up to capacity
number of resources.
Panics
This method panics if the number of resources given to
with_resources()
exceeds capacity
.
sourcepub fn with_hasher(builder: B) -> Self
pub fn with_hasher(builder: B) -> Self
Use the provided hash algorithm when hashing keys.
sourcepub fn with_resources(self, resources: impl IntoIterator<Item = R>) -> Self
pub fn with_resources(self, resources: impl IntoIterator<Item = R>) -> Self
Construct the AnchorHash
with an initial set of resources.
Trait Implementations
sourceimpl<R> Default for Builder<R, RandomState>
impl<R> Default for Builder<R, RandomState>
Initialise an empty AnchorHash instance using the DefaultHasher
and no
pre-populated resources.
Auto Trait Implementations
impl<R, B> RefUnwindSafe for Builder<R, B>where
B: RefUnwindSafe,
R: RefUnwindSafe,
impl<R, B> Send for Builder<R, B>where
B: Send,
R: Send,
impl<R, B> Sync for Builder<R, B>where
B: Sync,
R: Sync,
impl<R, B> Unpin for Builder<R, B>where
B: Unpin,
R: Unpin,
impl<R, B> UnwindSafe for Builder<R, B>where
B: UnwindSafe,
R: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more