1 #![feature(arbitrary_self_types, unsize, coerce_unsized, dispatch_from_dyn)]
2 #![feature(rustc_attrs)]
8 fn bar(self: Pin<&mut Self>) -> bool;
11 impl Foo for &'static str {
12 fn bar(self: Pin<&mut Self>) -> bool {
17 let mut test: Pin<Box<dyn Foo>> = Box::pin("foo");
21 fn stdlib_pointers() {
22 use std::{pin::Pin, rc::Rc, sync::Arc};
25 fn by_rc(self: Rc<Self>) -> i64;
26 fn by_arc(self: Arc<Self>) -> i64;
27 fn by_pin_mut(self: Pin<&mut Self>) -> i64;
28 fn by_pin_box(self: Pin<Box<Self>>) -> i64;
32 fn by_rc(self: Rc<Self>) -> i64 {
35 fn by_arc(self: Arc<Self>) -> i64 {
38 fn by_pin_mut(self: Pin<&mut Self>) -> i64 {
41 fn by_pin_box(self: Pin<Box<Self>>) -> i64 {
46 let rc = Rc::new(1i64) as Rc<dyn Trait>;
47 assert_eq!(1, rc.by_rc());
49 let arc = Arc::new(2i64) as Arc<dyn Trait>;
50 assert_eq!(2, arc.by_arc());
53 let pin_mut = Pin::new(&mut value) as Pin<&mut dyn Trait>;
54 assert_eq!(3, pin_mut.by_pin_mut());
56 let pin_box = Into::<Pin<Box<i64>>>::into(Box::new(4i64)) as Pin<Box<dyn Trait>>;
57 assert_eq!(4, pin_box.by_pin_box());
60 fn pointers_and_wrappers() {
63 ops::{CoerceUnsized, Deref, DispatchFromDyn},
66 struct Ptr<T: ?Sized>(Box<T>);
68 impl<T: ?Sized> Deref for Ptr<T> {
71 fn deref(&self) -> &T {
76 impl<T: Unsize<U> + ?Sized, U: ?Sized> CoerceUnsized<Ptr<U>> for Ptr<T> {}
77 impl<T: Unsize<U> + ?Sized, U: ?Sized> DispatchFromDyn<Ptr<U>> for Ptr<T> {}
79 struct Wrapper<T: ?Sized>(T);
81 impl<T: ?Sized> Deref for Wrapper<T> {
84 fn deref(&self) -> &T {
89 impl<T: CoerceUnsized<U>, U> CoerceUnsized<Wrapper<U>> for Wrapper<T> {}
90 impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<Wrapper<U>> for Wrapper<T> {}
93 // This method isn't object-safe yet. Unsized by-value `self` is object-safe (but not callable
94 // without unsized_locals), but wrappers arond `Self` currently are not.
95 // FIXME (mikeyhew) uncomment this when unsized rvalues object-safety is implemented
96 // fn wrapper(self: Wrapper<Self>) -> i32;
97 fn ptr_wrapper(self: Ptr<Wrapper<Self>>) -> i32;
98 fn wrapper_ptr(self: Wrapper<Ptr<Self>>) -> i32;
99 fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32;
103 fn ptr_wrapper(self: Ptr<Wrapper<Self>>) -> i32 {
106 fn wrapper_ptr(self: Wrapper<Ptr<Self>>) -> i32 {
109 fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32 {
114 let pw = Ptr(Box::new(Wrapper(5))) as Ptr<Wrapper<dyn Trait>>;
115 assert_eq!(pw.ptr_wrapper(), 5);
117 let wp = Wrapper(Ptr(Box::new(6))) as Wrapper<Ptr<dyn Trait>>;
118 assert_eq!(wp.wrapper_ptr(), 6);
120 let wpw = Wrapper(Ptr(Box::new(Wrapper(7)))) as Wrapper<Ptr<Wrapper<dyn Trait>>>;
121 assert_eq!(wpw.wrapper_ptr_wrapper(), 7);
127 pointers_and_wrappers();