mirror of
https://github.com/callumio/suck.git
synced 2025-12-17 19:49:21 +00:00
Relax T: Clone bound to only apply to static values
This commit is contained in:
parent
7c8fc42f27
commit
4c13facc96
2 changed files with 17 additions and 5 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue