From a0ca80297b7dfb0aa96e72cbc8d6749c34d1ae1e Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Fri, 4 Nov 2022 17:28:18 +0100 Subject: [PATCH] Add Persistent implementations for atomics --- src/params/persist.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/params/persist.rs b/src/params/persist.rs index 54e48084..2c0580af 100644 --- a/src/params/persist.rs +++ b/src/params/persist.rs @@ -109,6 +109,36 @@ macro_rules! impl_persistent_field_parking_lot_mutex { impl_persistent_field_parking_lot_mutex!(parking_lot::Mutex); impl_persistent_field_parking_lot_mutex!(parking_lot::FairMutex); +macro_rules! impl_persistent_atomic { + ($ty:ty, $inner_ty:ty) => { + impl PersistentField<'_, $inner_ty> for $ty { + fn set(&self, new_value: $inner_ty) { + self.store(new_value, std::sync::atomic::Ordering::SeqCst); + } + fn map(&self, f: F) -> R + where + F: Fn(&$inner_ty) -> R, + { + f(&self.load(std::sync::atomic::Ordering::SeqCst)) + } + } + }; +} + +impl_persistent_atomic!(std::sync::atomic::AtomicBool, bool); +impl_persistent_atomic!(std::sync::atomic::AtomicI8, i8); +impl_persistent_atomic!(std::sync::atomic::AtomicI16, i16); +impl_persistent_atomic!(std::sync::atomic::AtomicI32, i32); +impl_persistent_atomic!(std::sync::atomic::AtomicI64, i64); +impl_persistent_atomic!(std::sync::atomic::AtomicIsize, isize); +impl_persistent_atomic!(std::sync::atomic::AtomicU8, u8); +impl_persistent_atomic!(std::sync::atomic::AtomicU16, u16); +impl_persistent_atomic!(std::sync::atomic::AtomicU32, u32); +impl_persistent_atomic!(std::sync::atomic::AtomicU64, u64); +impl_persistent_atomic!(std::sync::atomic::AtomicUsize, usize); +impl_persistent_atomic!(atomic_float::AtomicF32, f32); +impl_persistent_atomic!(atomic_float::AtomicF64, f64); + /// Can be used with the `#[serde(with = "nih_plug::params::internals::serialize_atomic_cell")]` /// attribute to serialize `AtomicCell`s. pub mod serialize_atomic_cell {