mirror of
https://github.com/callumio/suck.git
synced 2025-12-17 03:29:21 +00:00
Relax T: Clone bound to only apply to static values
This commit is contained in:
parent
47b3bd5015
commit
b85d563ffa
2 changed files with 17 additions and 5 deletions
|
|
@ -63,13 +63,19 @@ where
|
||||||
|
|
||||||
impl<T, SR, ST> Sourcer<T, SR, ST>
|
impl<T, SR, ST> Sourcer<T, SR, ST>
|
||||||
where
|
where
|
||||||
T: Clone + Send + 'static,
|
T: Send + 'static,
|
||||||
SR: ChannelReceiver<Request>,
|
SR: ChannelReceiver<Request>,
|
||||||
ST: ChannelSender<Response<T>>,
|
ST: ChannelSender<Response<T>>,
|
||||||
{
|
{
|
||||||
/// Set a fixed value
|
/// Set a fixed value
|
||||||
pub fn set_static(&self, value: T) -> Result<(), Error> {
|
pub fn set_static(&self, val: T) -> Result<(), Error>
|
||||||
self.state.swap(Arc::new(ValueSource::Static(value)));
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
self.state.swap(Arc::new(ValueSource::Static {
|
||||||
|
val,
|
||||||
|
clone: T::clone,
|
||||||
|
}));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,7 +136,13 @@ where
|
||||||
let state = self.state.load();
|
let state = self.state.load();
|
||||||
|
|
||||||
match &**state {
|
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) => {
|
ValueSource::Dynamic(closure) => {
|
||||||
let value = self.execute_closure_safely(&mut || closure());
|
let value = self.execute_closure_safely(&mut || closure());
|
||||||
match value {
|
match value {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ pub enum Response<T> {
|
||||||
|
|
||||||
/// Represents the source of values: either static or dynamic
|
/// Represents the source of values: either static or dynamic
|
||||||
pub(crate) enum ValueSource<T> {
|
pub(crate) enum ValueSource<T> {
|
||||||
Static(T),
|
Static { val: T, clone: fn(&T) -> T },
|
||||||
DynamicMut(Mutex<Box<dyn FnMut() -> T + Send + Sync + 'static>>),
|
DynamicMut(Mutex<Box<dyn FnMut() -> T + Send + Sync + 'static>>),
|
||||||
Dynamic(Box<dyn Fn() -> T + Send + Sync + 'static>),
|
Dynamic(Box<dyn Fn() -> T + Send + Sync + 'static>),
|
||||||
None, // Never set
|
None, // Never set
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue