[Question] `unsafe` on the traits `RawLock` and `RawTryLock`
ICubE- opened this issue · 2 comments
In src/lock/api.rs, we have RawLock
and RawTryLock
traits and they are unsafe
.
Why are they unsafe trait
s? Removing unsafe
seems to work fine.
Is it just for indicating api developers that the trait needs more attention when they're implemented?
pub unsafe trait RawLock: Default + Send + Sync {
type Token;
fn lock(&self) -> Self::Token;
unsafe fn unlock(&self, token: Self::Token);
}
pub unsafe trait RawTryLock: RawLock {
fn try_lock(&self) -> Result<Self::Token, ()>;
}
Yes you're correct. See related explanation here: https://google.github.io/comprehensive-rust/unsafe-rust/unsafe-traits.html#implementing-unsafe-traits.
Thank you for the answer!
So, unsafe trait
on RawLock
means that "Any struct implementing this trait must guarantee the additional safety requirements to be hold. The developer have to ensure it." And the safety requirement is:
/// # 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.