diff --git a/Cargo.toml b/Cargo.toml index 67a2555d..e17869c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ edition = "2018" [dependencies] parking_lot_core = { path = "core", version = "0.8.0" } lock_api = { path = "lock_api", version = "0.4.0" } +bytemuck = { version = "1.5.1", optional = true, features = ["derive"] } instant = "0.1.4" [dev-dependencies] @@ -23,6 +24,7 @@ bincode = "1.3.0" [features] default = [] +bytemuck_traits = ["bytemuck", "lock_api/bytemuck"] owning_ref = ["lock_api/owning_ref"] nightly = ["parking_lot_core/nightly", "lock_api/nightly"] deadlock_detection = ["parking_lot_core/deadlock_detection"] diff --git a/lock_api/Cargo.toml b/lock_api/Cargo.toml index 55b5c358..ce4e6cc0 100644 --- a/lock_api/Cargo.toml +++ b/lock_api/Cargo.toml @@ -10,6 +10,7 @@ categories = ["concurrency", "no-std"] edition = "2018" [dependencies] +bytemuck = { version = "1.5.1", optional = true } scopeguard = { version = "1.1.0", default-features = false } owning_ref = { version = "0.4.1", optional = true } diff --git a/lock_api/src/mutex.rs b/lock_api/src/mutex.rs index aded96d1..c8da52ce 100644 --- a/lock_api/src/mutex.rs +++ b/lock_api/src/mutex.rs @@ -11,6 +11,9 @@ use core::marker::PhantomData; use core::mem; use core::ops::{Deref, DerefMut}; +#[cfg(feature = "bytemuck")] +use bytemuck::Zeroable; + #[cfg(feature = "owning_ref")] use owning_ref::StableAddress; @@ -137,6 +140,8 @@ pub struct Mutex { data: UnsafeCell, } +#[cfg(feature = "bytemuck")] +unsafe impl Zeroable for Mutex {} unsafe impl Send for Mutex {} unsafe impl Sync for Mutex {} diff --git a/lock_api/src/rwlock.rs b/lock_api/src/rwlock.rs index e97de980..031edec1 100644 --- a/lock_api/src/rwlock.rs +++ b/lock_api/src/rwlock.rs @@ -11,6 +11,9 @@ use core::marker::PhantomData; use core::mem; use core::ops::{Deref, DerefMut}; +#[cfg(feature = "bytemuck")] +use bytemuck::Zeroable; + #[cfg(feature = "owning_ref")] use owning_ref::StableAddress; @@ -313,6 +316,9 @@ pub struct RwLock { data: UnsafeCell, } +#[cfg(feature = "bytemuck")] +unsafe impl Zeroable for RwLock {} + // Copied and modified from serde #[cfg(feature = "serde")] impl Serialize for RwLock diff --git a/src/raw_fair_mutex.rs b/src/raw_fair_mutex.rs index 0da6828e..44da0cd7 100644 --- a/src/raw_fair_mutex.rs +++ b/src/raw_fair_mutex.rs @@ -9,6 +9,7 @@ use crate::raw_mutex::RawMutex; use lock_api::RawMutexFair; /// Raw fair mutex type backed by the parking lot. +#[cfg_attr(feature = "bytemuck", derive(bytemuck::Zeroable))] pub struct RawFairMutex(RawMutex); unsafe impl lock_api::RawMutex for RawFairMutex { diff --git a/src/raw_mutex.rs b/src/raw_mutex.rs index 06667d32..4fc65f22 100644 --- a/src/raw_mutex.rs +++ b/src/raw_mutex.rs @@ -55,6 +55,9 @@ pub struct RawMutex { state: AtomicU8, } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for RawMutex {} + unsafe impl lock_api::RawMutex for RawMutex { const INIT: RawMutex = RawMutex { state: AtomicU8::new(0), diff --git a/src/raw_rwlock.rs b/src/raw_rwlock.rs index 75a98128..2fdd2b51 100644 --- a/src/raw_rwlock.rs +++ b/src/raw_rwlock.rs @@ -57,6 +57,9 @@ pub struct RawRwLock { state: AtomicUsize, } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for RawRwLock {} + unsafe impl lock_api::RawRwLock for RawRwLock { const INIT: RawRwLock = RawRwLock { state: AtomicUsize::new(0),