[][src]Struct owned_alloc::Cache

pub struct Cache<A> { /* fields omitted */ }
[]

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]
[]

[]

Creates a new cache with no data.

[]

Stores data into the cache.

[]

Takes the data from the cache.

[]

Takes the data from the cache. If there was no data, the passed closure is called to produce the returned data.

Trait Implementations

impl<A> Default for Cache<A>
[src]
[+]

impl<A: Debug> Debug for Cache<A>
[src]
[+]

Auto Trait Implementations

impl<A> Send for Cache<A> where
    A: Send

impl<A> Sync for Cache<A> where
    A: Sync

Blanket Implementations

impl<T> From for T
[src]
[]

impl<T, U> Into for T where
    U: From<T>, 
[src]
[]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]
[]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]
[]

impl<T> Any for T where
    T: 'static + ?Sized
[src]
[]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]
[]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]
[]