Relax T: Clone bound to only apply to static values

This commit is contained in:
Roman Moisieiev 2025-10-09 11:02:20 +01:00
parent 47b3bd5015
commit b85d563ffa
2 changed files with 17 additions and 5 deletions

View file

@ -63,13 +63,19 @@ where
impl<T, SR, ST> Sourcer<T, SR, ST>
where
T: Clone + Send + 'static,
T: Send + 'static,
SR: ChannelReceiver<Request>,
ST: ChannelSender<Response<T>>,
{
/// Set a fixed value
pub fn set_static(&self, value: T) -> Result<(), Error> {
self.state.swap(Arc::new(ValueSource::Static(value)));
pub fn set_static(&self, val: T) -> Result<(), Error>
where
T: Clone,
{
self.state.swap(Arc::new(ValueSource::Static {
val,
clone: T::clone,
}));
Ok(())
}
@ -130,7 +136,13 @@ where
let state = self.state.load();
match &**state {
ValueSource::Static(value) => Ok(Response::Value(value.clone())),
ValueSource::Static { val, clone } => {
let value = self.execute_closure_safely(&mut || clone(val));
match value {
Ok(v) => Ok(Response::Value(v)),
Err(_) => Ok(Response::NoSource), // Closure execution failed
}
}
ValueSource::Dynamic(closure) => {
let value = self.execute_closure_safely(&mut || closure());
match value {

View file

@ -17,7 +17,7 @@ pub enum Response<T> {
/// Represents the source of values: either static or dynamic
pub(crate) enum ValueSource<T> {
Static(T),
Static { val: T, clone: fn(&T) -> T },
DynamicMut(Mutex<Box<dyn FnMut() -> T + Send + Sync + 'static>>),
Dynamic(Box<dyn Fn() -> T + Send + Sync + 'static>),
None, // Never set