Merge pull request #625 from nbdd0121/sync_unsized

Use ?Sized in Mutex and RwLock
split-by-pattern
Yoshua Wuyts 5 years ago committed by GitHub
commit 61eb52cb36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -39,14 +39,14 @@ use crate::task::{Context, Poll};
/// #
/// # })
/// ```
pub struct Mutex<T> {
pub struct Mutex<T: ?Sized> {
locked: AtomicBool,
wakers: WakerSet,
value: UnsafeCell<T>,
}
unsafe impl<T: Send> Send for Mutex<T> {}
unsafe impl<T: Send> Sync for Mutex<T> {}
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
impl<T> Mutex<T> {
/// Creates a new mutex.
@ -65,7 +65,9 @@ impl<T> Mutex<T> {
value: UnsafeCell::new(t),
}
}
}
impl<T: ?Sized> Mutex<T> {
/// Acquires the lock.
///
/// Returns a guard that releases the lock when dropped.
@ -91,12 +93,12 @@ impl<T> Mutex<T> {
/// # })
/// ```
pub async fn lock(&self) -> MutexGuard<'_, T> {
pub struct LockFuture<'a, T> {
pub struct LockFuture<'a, T: ?Sized> {
mutex: &'a Mutex<T>,
opt_key: Option<usize>,
}
impl<'a, T> Future for LockFuture<'a, T> {
impl<'a, T: ?Sized> Future for LockFuture<'a, T> {
type Output = MutexGuard<'a, T>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
@ -123,7 +125,7 @@ impl<T> Mutex<T> {
}
}
impl<T> Drop for LockFuture<'_, T> {
impl<T: ?Sized> Drop for LockFuture<'_, T> {
fn drop(&mut self) {
// If the current task is still in the set, that means it is being cancelled now.
if let Some(key) = self.opt_key {
@ -189,7 +191,7 @@ impl<T> Mutex<T> {
/// let mutex = Mutex::new(10);
/// assert_eq!(mutex.into_inner(), 10);
/// ```
pub fn into_inner(self) -> T {
pub fn into_inner(self) -> T where T: Sized {
self.value.into_inner()
}
@ -216,7 +218,7 @@ impl<T> Mutex<T> {
}
}
impl<T: fmt::Debug> fmt::Debug for Mutex<T> {
impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct Locked;
impl fmt::Debug for Locked {
@ -238,19 +240,19 @@ impl<T> From<T> for Mutex<T> {
}
}
impl<T: Default> Default for Mutex<T> {
impl<T: ?Sized + Default> Default for Mutex<T> {
fn default() -> Mutex<T> {
Mutex::new(Default::default())
}
}
/// A guard that releases the lock when dropped.
pub struct MutexGuard<'a, T>(&'a Mutex<T>);
pub struct MutexGuard<'a, T: ?Sized>(&'a Mutex<T>);
unsafe impl<T: Send> Send for MutexGuard<'_, T> {}
unsafe impl<T: Sync> Sync for MutexGuard<'_, T> {}
unsafe impl<T: ?Sized + Send> Send for MutexGuard<'_, T> {}
unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}
impl<T> Drop for MutexGuard<'_, T> {
impl<T: ?Sized> Drop for MutexGuard<'_, T> {
fn drop(&mut self) {
// Use `SeqCst` ordering to synchronize with `WakerSet::insert()` and `WakerSet::update()`.
self.0.locked.store(false, Ordering::SeqCst);
@ -260,19 +262,19 @@ impl<T> Drop for MutexGuard<'_, T> {
}
}
impl<T: fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
impl<T: ?Sized +fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: fmt::Display> fmt::Display for MutexGuard<'_, T> {
impl<T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T> Deref for MutexGuard<'_, T> {
impl<T: ?Sized> Deref for MutexGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
@ -280,7 +282,7 @@ impl<T> Deref for MutexGuard<'_, T> {
}
}
impl<T> DerefMut for MutexGuard<'_, T> {
impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.0.value.get() }
}

@ -49,15 +49,15 @@ const READ_COUNT_MASK: usize = !(ONE_READ - 1);
/// #
/// # })
/// ```
pub struct RwLock<T> {
pub struct RwLock<T: ?Sized> {
state: AtomicUsize,
read_wakers: WakerSet,
write_wakers: WakerSet,
value: UnsafeCell<T>,
}
unsafe impl<T: Send> Send for RwLock<T> {}
unsafe impl<T: Send + Sync> Sync for RwLock<T> {}
unsafe impl<T: ?Sized + Send> Send for RwLock<T> {}
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
impl<T> RwLock<T> {
/// Creates a new reader-writer lock.
@ -77,7 +77,9 @@ impl<T> RwLock<T> {
value: UnsafeCell::new(t),
}
}
}
impl<T: ?Sized> RwLock<T> {
/// Acquires a read lock.
///
/// Returns a guard that releases the lock when dropped.
@ -99,12 +101,12 @@ impl<T> RwLock<T> {
/// # })
/// ```
pub async fn read(&self) -> RwLockReadGuard<'_, T> {
pub struct ReadFuture<'a, T> {
pub struct ReadFuture<'a, T: ?Sized> {
lock: &'a RwLock<T>,
opt_key: Option<usize>,
}
impl<'a, T> Future for ReadFuture<'a, T> {
impl<'a, T: ?Sized> Future for ReadFuture<'a, T> {
type Output = RwLockReadGuard<'a, T>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
@ -131,7 +133,7 @@ impl<T> RwLock<T> {
}
}
impl<T> Drop for ReadFuture<'_, T> {
impl<T: ?Sized> Drop for ReadFuture<'_, T> {
fn drop(&mut self) {
// If the current task is still in the set, that means it is being cancelled now.
if let Some(key) = self.opt_key {
@ -224,12 +226,12 @@ impl<T> RwLock<T> {
/// # })
/// ```
pub async fn write(&self) -> RwLockWriteGuard<'_, T> {
pub struct WriteFuture<'a, T> {
pub struct WriteFuture<'a, T: ?Sized> {
lock: &'a RwLock<T>,
opt_key: Option<usize>,
}
impl<'a, T> Future for WriteFuture<'a, T> {
impl<'a, T: ?Sized> Future for WriteFuture<'a, T> {
type Output = RwLockWriteGuard<'a, T>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
@ -256,7 +258,7 @@ impl<T> RwLock<T> {
}
}
impl<T> Drop for WriteFuture<'_, T> {
impl<T: ?Sized> Drop for WriteFuture<'_, T> {
fn drop(&mut self) {
// If the current task is still in the set, that means it is being cancelled now.
if let Some(key) = self.opt_key {
@ -316,7 +318,7 @@ impl<T> RwLock<T> {
/// let lock = RwLock::new(10);
/// assert_eq!(lock.into_inner(), 10);
/// ```
pub fn into_inner(self) -> T {
pub fn into_inner(self) -> T where T: Sized {
self.value.into_inner()
}
@ -343,7 +345,7 @@ impl<T> RwLock<T> {
}
}
impl<T: fmt::Debug> fmt::Debug for RwLock<T> {
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct Locked;
impl fmt::Debug for Locked {
@ -365,19 +367,19 @@ impl<T> From<T> for RwLock<T> {
}
}
impl<T: Default> Default for RwLock<T> {
impl<T: ?Sized + Default> Default for RwLock<T> {
fn default() -> RwLock<T> {
RwLock::new(Default::default())
}
}
/// A guard that releases the read lock when dropped.
pub struct RwLockReadGuard<'a, T>(&'a RwLock<T>);
pub struct RwLockReadGuard<'a, T: ?Sized>(&'a RwLock<T>);
unsafe impl<T: Send> Send for RwLockReadGuard<'_, T> {}
unsafe impl<T: Sync> Sync for RwLockReadGuard<'_, T> {}
unsafe impl<T: ?Sized + Send> Send for RwLockReadGuard<'_, T> {}
unsafe impl<T: ?Sized + Sync> Sync for RwLockReadGuard<'_, T> {}
impl<T> Drop for RwLockReadGuard<'_, T> {
impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
fn drop(&mut self) {
let state = self.0.state.fetch_sub(ONE_READ, Ordering::SeqCst);
@ -388,19 +390,19 @@ impl<T> Drop for RwLockReadGuard<'_, T> {
}
}
impl<T: fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: fmt::Display> fmt::Display for RwLockReadGuard<'_, T> {
impl<T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T> Deref for RwLockReadGuard<'_, T> {
impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
@ -409,12 +411,12 @@ impl<T> Deref for RwLockReadGuard<'_, T> {
}
/// A guard that releases the write lock when dropped.
pub struct RwLockWriteGuard<'a, T>(&'a RwLock<T>);
pub struct RwLockWriteGuard<'a, T: ?Sized>(&'a RwLock<T>);
unsafe impl<T: Send> Send for RwLockWriteGuard<'_, T> {}
unsafe impl<T: Sync> Sync for RwLockWriteGuard<'_, T> {}
unsafe impl<T: ?Sized + Send> Send for RwLockWriteGuard<'_, T> {}
unsafe impl<T: ?Sized + Sync> Sync for RwLockWriteGuard<'_, T> {}
impl<T> Drop for RwLockWriteGuard<'_, T> {
impl<T: ?Sized> Drop for RwLockWriteGuard<'_, T> {
fn drop(&mut self) {
self.0.state.store(0, Ordering::SeqCst);
@ -427,19 +429,19 @@ impl<T> Drop for RwLockWriteGuard<'_, T> {
}
}
impl<T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: fmt::Display> fmt::Display for RwLockWriteGuard<'_, T> {
impl<T: ?Sized + fmt::Display> fmt::Display for RwLockWriteGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T> Deref for RwLockWriteGuard<'_, T> {
impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
@ -447,7 +449,7 @@ impl<T> Deref for RwLockWriteGuard<'_, T> {
}
}
impl<T> DerefMut for RwLockWriteGuard<'_, T> {
impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.0.value.get() }
}

Loading…
Cancel
Save