From 5566dd7fbbd2cb588d4d9c4bd3c19e73d72bba2f Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 26 Feb 2023 17:49:33 +0100 Subject: [PATCH] Add PersistentField implementation for AtomicCell This API really needs to change. --- src/params/persist.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/params/persist.rs b/src/params/persist.rs index 437476a5..047f88eb 100644 --- a/src/params/persist.rs +++ b/src/params/persist.rs @@ -49,6 +49,22 @@ macro_rules! impl_persistent_arc { } }; + ($ty:ty, T: $($bounds:tt)*) => { + impl<'a, T> PersistentField<'a, T> for Arc<$ty> + where + T: $($bounds)*, + { + fn set(&self, new_value: T) { + self.as_ref().set(new_value); + } + fn map(&self, f: F) -> R + where + F: Fn(&T) -> R, + { + self.as_ref().map(f) + } + } + }; ($ty:ty, $inner_ty:ty) => { impl<'a> PersistentField<'a, $inner_ty> for Arc<$ty> { fn set(&self, new_value: $inner_ty) { @@ -184,6 +200,23 @@ impl_persistent_atomic!(std::sync::atomic::AtomicUsize, usize); impl_persistent_atomic!(atomic_float::AtomicF32, f32); impl_persistent_atomic!(atomic_float::AtomicF64, f64); +impl<'a, T> PersistentField<'a, T> for crossbeam::atomic::AtomicCell +where + T: serde::Serialize + serde::Deserialize<'a> + Copy + Send, +{ + fn set(&self, new_value: T) { + self.store(new_value); + } + fn map(&self, f: F) -> R + where + F: Fn(&T) -> R, + { + f(&self.load()) + } +} +impl_persistent_arc!(crossbeam::atomic::AtomicCell, + T: serde::Serialize + serde::Deserialize<'a> + Copy + Send); + /// Can be used with the `#[serde(with = "nih_plug::params::internals::serialize_atomic_cell")]` /// attribute to serialize `AtomicCell`s. pub mod serialize_atomic_cell {