use std::any::Any;
use crate::error::ContextError;
pub(crate) trait ContextValue: Any + Send + Sync {
fn clone_boxed(&self) -> Box<dyn ContextValue>;
fn as_any(&self) -> &dyn Any;
fn serialize_value(&self) -> Result<Vec<u8>, ContextError>;
fn is_local(&self) -> bool;
}
impl<T> ContextValue for T
where
T: Clone + Send + Sync + serde::Serialize + serde::de::DeserializeOwned + 'static,
{
fn clone_boxed(&self) -> Box<dyn ContextValue> {
Box::new(self.clone())
}
fn as_any(&self) -> &dyn Any {
self
}
fn serialize_value(&self) -> Result<Vec<u8>, ContextError> {
bincode::serialize(self).map_err(|e| ContextError::SerializationFailed(e.to_string()))
}
fn is_local(&self) -> bool {
false
}
}
pub(crate) struct LocalValue<T>(pub T);
impl<T> ContextValue for LocalValue<T>
where
T: Clone + Send + Sync + 'static,
{
fn clone_boxed(&self) -> Box<dyn ContextValue> {
Box::new(LocalValue(self.0.clone()))
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn serialize_value(&self) -> Result<Vec<u8>, ContextError> {
Err(ContextError::LocalOnlyKey(
std::any::type_name::<T>().to_string(),
))
}
fn is_local(&self) -> bool {
true
}
}