Skip to content

Commit a2d1cb2

Browse files
committed
impl DispatchFromDyn for Cell and UnsafeCell
1 parent c8e6a9e commit a2d1cb2

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

library/core/src/cell.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ use crate::cmp::Ordering;
196196
use crate::fmt::{self, Debug, Display};
197197
use crate::marker::{PhantomData, Unsize};
198198
use crate::mem;
199-
use crate::ops::{CoerceUnsized, Deref, DerefMut};
199+
use crate::ops::{CoerceUnsized, Deref, DerefMut, DispatchFromDyn};
200200
use crate::ptr::{self, NonNull};
201201

202202
mod lazy;
@@ -571,6 +571,16 @@ impl<T: Default> Cell<T> {
571571
#[unstable(feature = "coerce_unsized", issue = "18598")]
572572
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}
573573

574+
// Allow types that wrap `Cell` to also implement `DispatchFromDyn`
575+
// and become object safe method receivers.
576+
// Note that currently `Cell` itself cannot be a method receiver
577+
// because it does not implement Deref.
578+
// In other words:
579+
// `self: Cell<&Self>` won't work
580+
// `self: CellWrapper<Self>` becomes possible
581+
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
582+
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<Cell<U>> for Cell<T> {}
583+
574584
impl<T> Cell<[T]> {
575585
/// Returns a `&[Cell<T>]` from a `&Cell<[T]>`
576586
///
@@ -2078,6 +2088,16 @@ impl<T> const From<T> for UnsafeCell<T> {
20782088
#[unstable(feature = "coerce_unsized", issue = "18598")]
20792089
impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T> {}
20802090

2091+
// Allow types that wrap `UnsafeCell` to also implement `DispatchFromDyn`
2092+
// and become object safe method receivers.
2093+
// Note that currently `UnsafeCell` itself cannot be a method receiver
2094+
// because it does not implement Deref.
2095+
// In other words:
2096+
// `self: UnsafeCell<&Self>` won't work
2097+
// `self: UnsafeCellWrapper<Self>` becomes possible
2098+
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
2099+
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T> {}
2100+
20812101
/// [`UnsafeCell`], but [`Sync`].
20822102
///
20832103
/// This is just an `UnsafeCell`, except it implements `Sync`
@@ -2169,6 +2189,17 @@ impl<T> const From<T> for SyncUnsafeCell<T> {
21692189
//#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
21702190
impl<T: CoerceUnsized<U>, U> CoerceUnsized<SyncUnsafeCell<U>> for SyncUnsafeCell<T> {}
21712191

2192+
// Allow types that wrap `SyncUnsafeCell` to also implement `DispatchFromDyn`
2193+
// and become object safe method receivers.
2194+
// Note that currently `SyncUnsafeCell` itself cannot be a method receiver
2195+
// because it does not implement Deref.
2196+
// In other words:
2197+
// `self: SyncUnsafeCell<&Self>` won't work
2198+
// `self: SyncUnsafeCellWrapper<Self>` becomes possible
2199+
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
2200+
//#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
2201+
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<SyncUnsafeCell<U>> for SyncUnsafeCell<T> {}
2202+
21722203
#[allow(unused)]
21732204
fn assert_coerce_unsized(
21742205
a: UnsafeCell<&i32>,

tests/ui/self/arbitrary_self_types_pointers_and_wrappers.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(rustc_attrs)]
44

55
use std::{
6+
cell::Cell,
67
ops::{Deref, CoerceUnsized, DispatchFromDyn},
78
marker::Unsize,
89
};
@@ -20,6 +21,20 @@ impl<T: ?Sized> Deref for Ptr<T> {
2021
impl<T: Unsize<U> + ?Sized, U: ?Sized> CoerceUnsized<Ptr<U>> for Ptr<T> {}
2122
impl<T: Unsize<U> + ?Sized, U: ?Sized> DispatchFromDyn<Ptr<U>> for Ptr<T> {}
2223

24+
25+
struct CellPtr<'a, T: ?Sized>(Cell<&'a T>);
26+
27+
impl<'a, T: ?Sized> Deref for CellPtr<'a, T> {
28+
type Target = T;
29+
30+
fn deref(&self) -> &T {
31+
self.0.get()
32+
}
33+
}
34+
35+
impl<'a, T: Unsize<U> + ?Sized, U: ?Sized> CoerceUnsized<CellPtr<'a, U>> for CellPtr<'a, T> {}
36+
impl<'a, T: Unsize<U> + ?Sized, U: ?Sized> DispatchFromDyn<CellPtr<'a, U>> for CellPtr<'a, T> {}
37+
2338
struct Wrapper<T: ?Sized>(T);
2439

2540
impl<T: ?Sized> Deref for Wrapper<T> {
@@ -42,6 +57,7 @@ trait Trait {
4257
fn ptr_wrapper(self: Ptr<Wrapper<Self>>) -> i32;
4358
fn wrapper_ptr(self: Wrapper<Ptr<Self>>) -> i32;
4459
fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32;
60+
fn cell(self: CellPtr<Self>) -> i32;
4561
}
4662

4763
impl Trait for i32 {
@@ -54,6 +70,9 @@ impl Trait for i32 {
5470
fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32 {
5571
***self
5672
}
73+
fn cell(self: CellPtr<Self>) -> i32 {
74+
*self
75+
}
5776
}
5877

5978
fn main() {
@@ -65,4 +84,7 @@ fn main() {
6584

6685
let wpw = Wrapper(Ptr(Box::new(Wrapper(7)))) as Wrapper<Ptr<Wrapper<dyn Trait>>>;
6786
assert_eq!(wpw.wrapper_ptr_wrapper(), 7);
87+
88+
let c = CellPtr(Cell::new(&8)) as CellPtr<dyn Trait>;
89+
assert_eq!(c.cell(), 8);
6890
}

0 commit comments

Comments
 (0)