Trait cache::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.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

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

§

type Output = <T as Cacheable>::Output

§

type Error = <T as Cacheable>::Error

source§

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

Implementors§