Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally implement bytemuck::Zeroable, for safe zero-allocation #287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions lock_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
5 changes: 5 additions & 0 deletions lock_api/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -137,6 +140,8 @@ pub struct Mutex<R, T: ?Sized> {
data: UnsafeCell<T>,
}

#[cfg(feature = "bytemuck")]
unsafe impl<R: RawMutex + Zeroable, T: Zeroable> Zeroable for Mutex<R, T> {}
unsafe impl<R: RawMutex + Send, T: ?Sized + Send> Send for Mutex<R, T> {}
unsafe impl<R: RawMutex + Sync, T: ?Sized + Send> Sync for Mutex<R, T> {}

Expand Down
6 changes: 6 additions & 0 deletions lock_api/src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -313,6 +316,9 @@ pub struct RwLock<R, T: ?Sized> {
data: UnsafeCell<T>,
}

#[cfg(feature = "bytemuck")]
unsafe impl<R: RawRwLock + Zeroable, T: Zeroable> Zeroable for RwLock<R, T> {}

// Copied and modified from serde
#[cfg(feature = "serde")]
impl<R, T> Serialize for RwLock<R, T>
Expand Down
1 change: 1 addition & 0 deletions src/raw_fair_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/raw_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions src/raw_rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down