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§
sourcetype Output: Send + Sync + Serialize + DeserializeOwned
type Output: Send + Sync + Serialize + DeserializeOwned
The output produced by generating the object.
sourcetype Error: Send + Sync
type Error: Send + Sync
The error type returned by Cacheable::generate
.
Required Methods§
Object Safety§
This trait is not object safe.