From b85d563ffa45625068c546125eb42d7fd73c52f4 Mon Sep 17 00:00:00 2001 From: Roman Moisieiev Date: Thu, 9 Oct 2025 11:02:20 +0100 Subject: [PATCH] Relax T: Clone bound to only apply to static values --- src/channel.rs | 20 ++++++++++++++++---- src/types.rs | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/channel.rs b/src/channel.rs index 3f889e1..c3c8159 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -63,13 +63,19 @@ where impl Sourcer where - T: Clone + Send + 'static, + T: Send + 'static, SR: ChannelReceiver, ST: ChannelSender>, { /// 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 { diff --git a/src/types.rs b/src/types.rs index 937ce51..244584b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -17,7 +17,7 @@ pub enum Response { /// Represents the source of values: either static or dynamic pub(crate) enum ValueSource { - Static(T), + Static { val: T, clone: fn(&T) -> T }, DynamicMut(Mutex T + Send + Sync + 'static>>), Dynamic(Box T + Send + Sync + 'static>), None, // Never set