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>
impl<T: Default> MemoryBank<T>
Sourcepub fn take_loan(&self) -> Loan<T>
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>
impl<T> MemoryBank<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty MemoryBank<T>. The first loan is guaranteed to be a heap allocation.
Sourcepub fn take_old_loan(&self) -> Option<Loan<T>>
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());Sourcepub fn take_old_loan_timeout(&self, timeout: Duration) -> Option<Loan<T>>
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>
impl<T: Clone> Clone for MemoryBank<T>
Source§fn clone(&self) -> MemoryBank<T>
fn clone(&self) -> MemoryBank<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto 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> 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
Mutably borrows from an owned value. Read more