Struct LendingLibrary

Source
pub struct LendingLibrary<K, V>
where K: Hash,
{ /* private fields */ }
Expand description

A key-value data store that allows you to loan temporary ownership of values.

§Assumptions

The store does it’s best to ensure that no unsafe behaviour occurs, however as a result it may trigger several panics rather than allow an unsafe condition to arise.

The main panic condition is that a Loan object derived from the lend method on a store may never outlive the store it originated from. If this condition happens the store will generate a panic as it goes out of scope, noting the number of outstanding Loan objects.

Implementations§

Source§

impl<K, V> LendingLibrary<K, V>
where K: Hash,

Source

pub fn new() -> LendingLibrary<K, V>

Creates a new empty LendingLibrary.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::new();
Source

pub fn with_capacity(capacity: usize) -> LendingLibrary<K, V>

Creates an empty LendingLibrary with at least the specified capacity. The library will be able to hold at least capacity elements without reallocating.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::with_capacity(100);
Source

pub fn capacity(&self) -> usize

Returns the number of elements the library can store without reallocating. The same bounds as HashMap::capacity() apply.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::with_capacity(100);
assert!(lib.capacity() >= 100);
Source

pub fn reserve(&mut self, additional: usize)

Reserves space such that the library can store at least additional new records without reallocating.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::with_capacity(0);
assert_eq!(lib.capacity(), 0);
lib.reserve(10);
assert!(lib.capacity() >= 10);
Source

pub fn shrink_to_fit(&mut self)

Reduces the stores capacity to the minimum currently required.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::with_capacity(10);
assert!(lib.capacity() >= 10);
lib.shrink_to_fit();
assert_eq!(lib.capacity(), 0);
Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

An iterator visiting all key/value pairs in arbitary order. The item type is (&'a K, &'a V)

§Panics

The iterator will panic if it encounters an item that is currently loaned from the store, so this should only be used where you are sure you have returned all loaned items.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>

An iterator visiting all key/value pairs in arbitary order, with mutable references to the values. The item type is (&'a K, &'a mut V)

§Panics

The iterator will panic if it encounters an item that is currently loaned from the store, so this should only be used where you are sure you have returned all loaned items.

Source

pub fn len(&self) -> usize

Returns the number of items in the store.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::new();
lib.insert(1, 1);
lib.insert(2, 1);
assert_eq!(lib.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the store is empty and false otherwise.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::new();
assert!(lib.is_empty());
lib.insert(1, 1);
lib.insert(2, 1);
assert!(!lib.is_empty());
Source

pub fn clear(&mut self)

Removes all items from the store.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::new();
lib.insert(1, 1);
lib.insert(2, 1);
{
    let v = lib.lend(&2).unwrap();
    assert_eq!(*v, 1);
}
lib.clear();
assert_eq!(lib.lend(&1), None);
Source

pub fn contains_key(&self, key: &K) -> bool

Returns true if a record with key key exists in the store, and false otherwise.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::new();
assert!(!lib.contains_key(&1));
lib.insert(1, 1);
assert!(lib.contains_key(&1));
Source

pub fn insert(&mut self, key: K, val: V) -> Option<V>

Inserts a new key/value pair into the store. If a pair with that key already exists, the previous values will be returned as Some(V), otherwise the method returns None.

§Panics

The method will panic if you attempt to overwrite a key/value pair that is currently loaned.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::new();
lib.insert(1, 1);
lib.insert(2, 1);
Source

pub fn remove(&mut self, key: &K) -> bool

Removes a key/value pair from the store. Returning true if the key was present in the store and false otherwise.

§Example
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::new();
assert!(!lib.remove(&1));
lib.insert(1, 1);
assert!(lib.contains_key(&1));
assert!(lib.remove(&1));
assert!(!lib.contains_key(&1));
assert!(!lib.remove(&1));
Source

pub fn lend(&mut self, key: &K) -> Option<Loan<K, V>>

Loans a value from the library, returning Some(Loan<K, V>) if the value is present, and None if it is not.

§Panics

Will panic if you try and loan a value that still has an outstanding loan.

§Examples
use lending_library::LendingLibrary;
let mut lib: LendingLibrary<i32, i32> = LendingLibrary::with_capacity(0);
lib.insert(1, 1);
{
    let mut v = lib.lend(&1).unwrap();
    *v += 5;
}

Trait Implementations§

Source§

impl<K, V> Default for LendingLibrary<K, V>
where K: Hash,

Source§

fn default() -> Self

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

impl<K, V> Drop for LendingLibrary<K, V>
where K: Hash,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<K, V> Extend<(K, V)> for LendingLibrary<K, V>
where K: Hash,

Source§

fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, K, V> IntoIterator for &'a LendingLibrary<K, V>
where K: Hash,

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V> IntoIterator for &'a mut LendingLibrary<K, V>
where K: Hash,

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V> IntoIterator for LendingLibrary<K, V>
where K: Hash,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V> !Freeze for LendingLibrary<K, V>

§

impl<K, V> RefUnwindSafe for LendingLibrary<K, V>

§

impl<K, V> Send for LendingLibrary<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for LendingLibrary<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for LendingLibrary<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for LendingLibrary<K, V>
where K: UnwindSafe, V: UnwindSafe,

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.