Skip to content

Commit 16c69fd

Browse files
committed
Auto merge of #2033 - RalfJung:arbitrary-self-dyn, r=RalfJung
test arbitrary-self dyn receivers Requires rust-lang/rust#95071
2 parents a64e6fd + b066856 commit 16c69fd

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c7ce69faf2a7ea16c15d922985ca27ba70da30ee
1+
9bd53718e2537d95d8c092609618c2dcd6f05127

tests/run-pass/dyn-arbitrary-self.rs

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#![feature(arbitrary_self_types, unsize, coerce_unsized, dispatch_from_dyn)]
2+
#![feature(rustc_attrs)]
3+
4+
fn pin_box_dyn() {
5+
use std::pin::Pin;
6+
7+
trait Foo {
8+
fn bar(self: Pin<&mut Self>) -> bool;
9+
}
10+
11+
impl Foo for &'static str {
12+
fn bar(self: Pin<&mut Self>) -> bool {
13+
true
14+
}
15+
}
16+
17+
let mut test: Pin<Box<dyn Foo>> = Box::pin("foo");
18+
test.as_mut().bar();
19+
}
20+
21+
fn stdlib_pointers() {
22+
use std::{
23+
rc::Rc,
24+
sync::Arc,
25+
pin::Pin,
26+
};
27+
28+
trait Trait {
29+
fn by_rc(self: Rc<Self>) -> i64;
30+
fn by_arc(self: Arc<Self>) -> i64;
31+
fn by_pin_mut(self: Pin<&mut Self>) -> i64;
32+
fn by_pin_box(self: Pin<Box<Self>>) -> i64;
33+
}
34+
35+
impl Trait for i64 {
36+
fn by_rc(self: Rc<Self>) -> i64 {
37+
*self
38+
}
39+
fn by_arc(self: Arc<Self>) -> i64 {
40+
*self
41+
}
42+
fn by_pin_mut(self: Pin<&mut Self>) -> i64 {
43+
*self
44+
}
45+
fn by_pin_box(self: Pin<Box<Self>>) -> i64 {
46+
*self
47+
}
48+
}
49+
50+
let rc = Rc::new(1i64) as Rc<dyn Trait>;
51+
assert_eq!(1, rc.by_rc());
52+
53+
let arc = Arc::new(2i64) as Arc<dyn Trait>;
54+
assert_eq!(2, arc.by_arc());
55+
56+
let mut value = 3i64;
57+
let pin_mut = Pin::new(&mut value) as Pin<&mut dyn Trait>;
58+
assert_eq!(3, pin_mut.by_pin_mut());
59+
60+
let pin_box = Into::<Pin<Box<i64>>>::into(Box::new(4i64)) as Pin<Box<dyn Trait>>;
61+
assert_eq!(4, pin_box.by_pin_box());
62+
}
63+
64+
fn pointers_and_wrappers() {
65+
use std::{
66+
ops::{Deref, CoerceUnsized, DispatchFromDyn},
67+
marker::Unsize,
68+
};
69+
70+
struct Ptr<T: ?Sized>(Box<T>);
71+
72+
impl<T: ?Sized> Deref for Ptr<T> {
73+
type Target = T;
74+
75+
fn deref(&self) -> &T {
76+
&*self.0
77+
}
78+
}
79+
80+
impl<T: Unsize<U> + ?Sized, U: ?Sized> CoerceUnsized<Ptr<U>> for Ptr<T> {}
81+
impl<T: Unsize<U> + ?Sized, U: ?Sized> DispatchFromDyn<Ptr<U>> for Ptr<T> {}
82+
83+
struct Wrapper<T: ?Sized>(T);
84+
85+
impl<T: ?Sized> Deref for Wrapper<T> {
86+
type Target = T;
87+
88+
fn deref(&self) -> &T {
89+
&self.0
90+
}
91+
}
92+
93+
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Wrapper<U>> for Wrapper<T> {}
94+
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<Wrapper<U>> for Wrapper<T> {}
95+
96+
97+
trait Trait {
98+
// This method isn't object-safe yet. Unsized by-value `self` is object-safe (but not callable
99+
// without unsized_locals), but wrappers arond `Self` currently are not.
100+
// FIXME (mikeyhew) uncomment this when unsized rvalues object-safety is implemented
101+
// fn wrapper(self: Wrapper<Self>) -> i32;
102+
fn ptr_wrapper(self: Ptr<Wrapper<Self>>) -> i32;
103+
fn wrapper_ptr(self: Wrapper<Ptr<Self>>) -> i32;
104+
fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32;
105+
}
106+
107+
impl Trait for i32 {
108+
fn ptr_wrapper(self: Ptr<Wrapper<Self>>) -> i32 {
109+
**self
110+
}
111+
fn wrapper_ptr(self: Wrapper<Ptr<Self>>) -> i32 {
112+
**self
113+
}
114+
fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32 {
115+
***self
116+
}
117+
}
118+
119+
let pw = Ptr(Box::new(Wrapper(5))) as Ptr<Wrapper<dyn Trait>>;
120+
assert_eq!(pw.ptr_wrapper(), 5);
121+
122+
let wp = Wrapper(Ptr(Box::new(6))) as Wrapper<Ptr<dyn Trait>>;
123+
assert_eq!(wp.wrapper_ptr(), 6);
124+
125+
let wpw = Wrapper(Ptr(Box::new(Wrapper(7)))) as Wrapper<Ptr<Wrapper<dyn Trait>>>;
126+
assert_eq!(wpw.wrapper_ptr_wrapper(), 7);
127+
}
128+
129+
130+
fn main() {
131+
pin_box_dyn();
132+
stdlib_pointers();
133+
pointers_and_wrappers();
134+
}

0 commit comments

Comments
 (0)