Struct MemoryBank

Source
pub struct MemoryBank<T> { /* private fields */ }
Expand description

A structure that reuses old data of type T to reduce the number of new allocations (useful for heap allocations).

Implementations§

Source§

impl<T: Default> MemoryBank<T>

Source

pub fn take_loan(&self) -> Loan<T>

Loans the user some memory to use. Will reuse an element from old memory if there are any, otherwise it will allocate on the heap.

If the loaned memory was previously used, it will be in the exact same state it was in right before the previous Loan got dropped.

Source§

impl<T> MemoryBank<T>

Source

pub fn new() -> Self

Creates a new, empty MemoryBank<T>. The first loan is guaranteed to be a heap allocation.

Source

pub fn deposit(&self, value: T) -> Loan<T>

Gives the bank ownership of value and returns it in a Loan.

§Examples
use membank::MemoryBank;

let bank = MemoryBank::new();

let v = vec![2.1, 4.3, 1.0, 0.98];
let v_clone = v.clone();

let loan = bank.deposit(v);

assert_eq!(*loan, v_clone);
Source

pub fn take_old_loan(&self) -> Option<Loan<T>>

Only take a loan if it’s from previously used memory, if there is no previously allocated memory, returns None.

§Example
use membank::MemoryBank;

let bank: MemoryBank<Vec<i32>> = MemoryBank::new();
assert!(bank.take_old_loan().is_none());

let loan1 = bank.take_loan();

assert!(bank.take_old_loan().is_none());

drop(loan1);
assert!(bank.take_old_loan().is_some());
Source

pub fn take_old_loan_timeout(&self, timeout: Duration) -> Option<Loan<T>>

Only take previously used memory. Will return None if it waits past timeout.

§Example
use membank::MemoryBank;
use std::time::Duration;

let bank: MemoryBank<Vec<f64>> = MemoryBank::new();
assert!(bank.take_old_loan_timeout(Duration::from_millis(100)).is_none());

let mut loan1 = bank.take_loan();
loan1.push(3.21);
drop(loan1);

let loan2 = bank.take_old_loan_timeout(Duration::from_millis(100)).unwrap();
assert_eq!(*loan2, vec![3.21]);

Trait Implementations§

Source§

impl<T: Clone> Clone for MemoryBank<T>

Source§

fn clone(&self) -> MemoryBank<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Default for MemoryBank<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for MemoryBank<T>

§

impl<T> RefUnwindSafe for MemoryBank<T>

§

impl<T> Send for MemoryBank<T>
where T: Send,

§

impl<T> Sync for MemoryBank<T>
where T: Send,

§

impl<T> Unpin for MemoryBank<T>
where T: Unpin,

§

impl<T> UnwindSafe for MemoryBank<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.