[−][src]Struct owned_alloc::Cache
A general purpouse cache suitable for saving discarted memory allocations in a tight loop.
Dummy Example
extern crate owned_alloc; use owned_alloc::{Cache, RawVec, UninitAlloc}; fn do_some_stuff(iter: usize, n: usize) -> usize { let mut cache = Cache::new(); let mut res = 0usize; for i in 1 ..= iter { let alloc = cache.take_or(|| UninitAlloc::from(RawVec::with_capacity(n))); let inited = unsafe { alloc.init_in_place(|slice| { for j in 0 .. slice.len() { (&mut slice[j] as *mut usize) .write(i.wrapping_mul(j + 1)) } }) }; for &item in &*inited { res = res.wrapping_add(item); } cache.store(inited.drop_in_place()); } res } assert_eq!(do_some_stuff(2, 3), 1 + 2 + 3 + 2 + 4 + 6);
Methods
impl<A> Cache<A>
[src]
[−]
impl<A> Cache<A>
pub fn new() -> Self
[src]
[−]
pub fn new() -> Self
Creates a new cache with no data.
pub fn store(&mut self, val: A)
[src]
[−]
pub fn store(&mut self, val: A)
Stores data into the cache.
pub fn take(&mut self) -> Option<A>
[src]
[−]
pub fn take(&mut self) -> Option<A>
Takes the data from the cache.
pub fn take_or<F>(&mut self, create: F) -> A where
F: FnOnce() -> A,
[src]
[−]
pub fn take_or<F>(&mut self, create: F) -> A where
F: FnOnce() -> A,
Takes the data from the cache. If there was no data, the passed closure is called to produce the returned data.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
[−]
impl<T> From for T
impl<T, U> Into for T where
U: From<T>,
[src]
[−]
impl<T, U> Into for T where
U: From<T>,
impl<T, U> TryFrom for T where
T: From<U>,
[src]
[−]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
🔬 This is a nightly-only experimental API. (
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
[−]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T> Borrow for T where
T: ?Sized,
[src]
[−]
impl<T> Borrow for T where
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
[−]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
[−]
fn get_type_id(&self) -> TypeId
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
[−]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
[−]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T> BorrowMut for T where
T: ?Sized,
[src]
[−]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
[−]
fn borrow_mut(&mut self) -> &mut T