FnMatcher

Struct FnMatcher 

Source
pub struct FnMatcher<T, F>
where F: Fn(&T) -> bool,
{ /* private fields */ }
Expand description

Function-based matcher for maximum flexibility in matching logic.

This matcher allows you to provide a custom function that determines whether a key matches. This is the most flexible matcher and can implement any matching logic you need.

§Examples

use simple_cacher::*;
use std::time::Duration;

let mut cache = SimpleCacher::new(Duration::from_secs(300));
cache.insert(2, "Even number".to_string());
cache.insert(3, "Odd number".to_string());
cache.insert(4, "Even number".to_string());
cache.insert(5, "Odd number".to_string());

// Find even numbers
let even_matcher = FnMatcher::new(|&key: &i32| key % 2 == 0);
let even_numbers = cache.get_all_by_matcher(&even_matcher);
assert_eq!(even_numbers.len(), 2); // 2 and 4

Implementations§

Source§

impl<T, F> FnMatcher<T, F>
where F: Fn(&T) -> bool,

Source

pub fn new(matcher_fn: F) -> Self

Creates a new function-based matcher.

§Arguments
  • matcher_fn - A function that takes a key reference and returns true if it matches
§Examples
use simple_cacher::*;

// Match strings longer than 5 characters
let long_string_matcher = FnMatcher::new(|s: &String| s.len() > 5);

Trait Implementations§

Source§

impl<T, F> Matcher<T> for FnMatcher<T, F>
where F: Fn(&T) -> bool,

Source§

fn matches(&self, key: &T) -> bool

Returns true if the given key matches this matcher’s criteria. Read more

Auto Trait Implementations§

§

impl<T, F> Freeze for FnMatcher<T, F>
where F: Freeze,

§

impl<T, F> RefUnwindSafe for FnMatcher<T, F>

§

impl<T, F> Send for FnMatcher<T, F>
where F: Send, T: Send,

§

impl<T, F> Sync for FnMatcher<T, F>
where F: Sync, T: Sync,

§

impl<T, F> Unpin for FnMatcher<T, F>
where F: Unpin, T: Unpin,

§

impl<T, F> UnwindSafe for FnMatcher<T, F>
where F: UnwindSafe, T: 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, 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.