From 5f1f3fd8c4683dd71fd32c17522feb29d14a5bdf Mon Sep 17 00:00:00 2001 From: Janggun Lee Date: Fri, 1 Nov 2024 11:08:05 +0900 Subject: [PATCH] Polish comments. --- src/lock/api.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/lock/api.rs b/src/lock/api.rs index 1bc66a3b60..65827bc885 100644 --- a/src/lock/api.rs +++ b/src/lock/api.rs @@ -6,13 +6,13 @@ use core::ops::{Deref, DerefMut}; /// /// # Safety /// -/// Implementations of this trait must ensure that the lock is actually -/// exclusive: a lock can't be acquired while the lock is already locked. +/// Implementations of this trait must ensure that the lock is actually exclusive: a lock can't be +/// acquired while the lock is already locked. // TODO: For weak memory, there needs to be a bit more stricter condition. unlock -hb→ lock. pub unsafe trait RawLock: Default + Send + Sync { /// Raw lock's token type. - // - // Send + Sync is needed to make LockGuard Send + Sync. + /// + /// Send + Sync is needed to make LockGuard Send + Sync. type Token: Send + Sync; /// Acquires the raw lock. @@ -30,8 +30,9 @@ pub unsafe trait RawLock: Default + Send + Sync { /// /// # Safety /// -/// Implementations of this trait must ensure that the lock is actually -/// exclusive: a lock can't be acquired while the lock is already locked. +/// Implementations of this trait must ensure that the lock is actually exclusive: a lock can't be +/// acquired while the lock is already locked. +/// /// Also, `try_lock()`, when successful, should return a token that can be used for /// `RawLock::unlock`. pub unsafe trait RawTryLock: RawLock { @@ -41,7 +42,7 @@ pub unsafe trait RawTryLock: RawLock { /// A type-safe lock. #[repr(C)] -#[derive(Debug, Default)] +#[derive(Debug)] pub struct Lock { inner: L, data: UnsafeCell, @@ -50,6 +51,19 @@ pub struct Lock { // Send is automatically implemented for Lock. unsafe impl Sync for Lock {} +impl Default for Lock +where + L: Default, +{ + // Manual impl for minimum trait bound. + fn default() -> Self { + Self { + inner: L::default(), + data: UnsafeCell::default(), + } + } +} + impl Lock { /// Creates a new lock. pub fn new(data: T) -> Self { @@ -85,8 +99,7 @@ impl Lock { } /// A guard that holds the lock and dereferences the inner value. -/// -/// Send/Sync are is automatically implemented. +// Send/Sync are automatically implemented. #[derive(Debug)] pub struct LockGuard<'s, L: RawLock, T> { lock: &'s Lock,