This is my code:
pub struct WithLock<T> {
pub(crate) data: Mutex<T>,
}
impl<T> WithLock<T> {
pub fn with_lock<F, U>(self, function: F) -> U
where
F: FnOnce(&mut T) -> U,
{
let mut lock = self.data.lock();
function(&mut *lock)
}
}
The issue I am facing, is when in another struct (using this WithLock struct) and attempting to do this:
pub fn update<F>(&self, f: F) -> T
where
F: FnOnce(T) -> T,
{
let old = self.get();
let new = f(old);
self.set(new);
new
}
I get an error about the train bound T: std::marker::Copy not being satisfied. The get function is this:
pub fn get(self) -> T
where
T: Copy,
{
self.data.with_lock(|s| *s)
}
I'm aware I am making T Copy, but, I'm trying to figure out why the with_lock closure requires it to be Copy. Ideally, the value wouldn't have to be copy. Any ideas?