cache

Trait Cacheable

Source
pub trait Cacheable:
    Serialize
    + DeserializeOwned
    + Hash
    + Eq
    + Send
    + Sync
    + Any {
    type Output: Send + Sync + Serialize + DeserializeOwned;
    type Error: Send + Sync;

    // Required method
    fn generate(&self) -> Result<Self::Output, Self::Error>;
}
Expand description

A cacheable object.

§Examples

use cache::Cacheable;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Hash, Eq, PartialEq)]
pub struct Params {
    param1: u64,
    param2: String,
};

impl Cacheable for Params {
    type Output = u64;
    type Error = anyhow::Error;

    fn generate(&self) -> anyhow::Result<u64> {
        println!("Executing an expensive computation...");

        // ...

        if error_condition {
            anyhow::bail!("an error occured during computation");
        }

        Ok(computation_result)
    }
}

Required Associated Types§

Source

type Output: Send + Sync + Serialize + DeserializeOwned

The output produced by generating the object.

Source

type Error: Send + Sync

The error type returned by Cacheable::generate.

Required Methods§

Source

fn generate(&self) -> Result<Self::Output, Self::Error>

Generates the output of the cacheable object.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: Cacheable> Cacheable for Arc<T>

Source§

type Output = <T as Cacheable>::Output

Source§

type Error = <T as Cacheable>::Error

Source§

fn generate(&self) -> Result<Self::Output, Self::Error>

Implementors§