Skip to main content

TypedLifecycle

Struct TypedLifecycle 

Source
pub struct TypedLifecycle<K, F>
where F: Fn(K) + Send + Sync,
{ /* private fields */ }
Expand description

Helper for creating typed lifecycle implementations.

This wrapper provides convenient typed access to lifecycle events when you only use a single key type in your cache.

§Example

use priority_lfu::{TypedLifecycle, CacheBuilder, CacheKey, DeepSizeOf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

#[derive(Hash, Eq, PartialEq, Clone)]
struct MyKey(u64);

#[derive(Clone, DeepSizeOf)]
struct MyValue(String);

impl CacheKey for MyKey {
    type Value = MyValue;
}

let evict_count = Arc::new(AtomicUsize::new(0));
let counter = evict_count.clone();

let lifecycle = TypedLifecycle::<MyKey, _>::new(move |_key| {
    counter.fetch_add(1, Ordering::Relaxed);
});

let cache = CacheBuilder::new(1024)
    .lifecycle(lifecycle)
    .build();

Implementations§

Source§

impl<K, F> TypedLifecycle<K, F>
where F: Fn(K) + Send + Sync,

Source

pub fn new(callback: F) -> Self

Create a new typed lifecycle with the given eviction callback.

Trait Implementations§

Source§

impl<K, F> Lifecycle for TypedLifecycle<K, F>
where K: Clone + Send + Sync + 'static, F: Fn(K) + Send + Sync,

Source§

fn on_evict(&self, key: &dyn Any)

Called when an item is evicted from the cache due to capacity pressure. Read more
Source§

fn on_remove(&self, key: &dyn Any)

Called when an item is explicitly removed via cache.remove(). Read more
Source§

fn on_clear(&self, key: &dyn Any)

Called when the cache is cleared via cache.clear(). Read more

Auto Trait Implementations§

§

impl<K, F> Freeze for TypedLifecycle<K, F>
where F: Freeze,

§

impl<K, F> RefUnwindSafe for TypedLifecycle<K, F>
where F: RefUnwindSafe,

§

impl<K, F> Send for TypedLifecycle<K, F>

§

impl<K, F> Sync for TypedLifecycle<K, F>

§

impl<K, F> Unpin for TypedLifecycle<K, F>
where F: Unpin,

§

impl<K, F> UnwindSafe for TypedLifecycle<K, F>
where F: 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

Source§

fn is_within(&self, b: &G2) -> bool