Skip to content

Commit ede12aa

Browse files
mikeyhewMichael Hewson
authored and
Michael Hewson
committed
Add a test with pointers and wrappers
This test shows what will ultimately be the goal of this PR: a receiver is object-safe as long as it can be coerced from a fat pointer to a thin pointer. The requirement is that if `Self: Unsize<U>`, the receiver type will implement `CoerceUnsized<FatReceiver>` where `FatReceiver` is the same as the receiver type, but with `U` substituted in for `Self`.
1 parent 4725417 commit ede12aa

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(arbitrary_self_types, unsize, coerce_unsized, box_syntax)]
11+
12+
use std::{
13+
ops::{Deref, CoerceUnsized},
14+
marker::Unsize,
15+
fmt::Debug,
16+
};
17+
18+
struct Ptr<T: ?Sized>(Box<T>);
19+
20+
impl<T: ?Sized> Deref for Ptr<T> {
21+
type Target = T;
22+
23+
fn deref(&self) -> &T {
24+
&*self.0
25+
}
26+
}
27+
28+
impl<T: Unsize<U> + ?Sized, U: ?Sized> CoerceUnsized<Ptr<U>> for Ptr<T> {}
29+
30+
struct Wrapper<T: ?Sized>(T);
31+
32+
impl<T: ?Sized> Deref for Wrapper<T> {
33+
type Target = T;
34+
35+
fn deref(&self) -> &T {
36+
&self.0
37+
}
38+
}
39+
40+
impl<T: CoerceUnsized<U>, U> CoerceUnsized<Wrapper<U>> for Wrapper<T> {}
41+
42+
trait Trait {
43+
// This method can't be called on trait objects, since the receiver would be unsized,
44+
// but should not cause an object safety error
45+
fn wrapper(self: Wrapper<Self>) -> i32;
46+
fn ptr_wrapper(self: Ptr<Wrapper<Self>>) -> i32;
47+
fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32;
48+
}
49+
50+
impl Trait for i32 {
51+
fn wrapper(self: Wrapper<Self>) -> i32 {
52+
*self
53+
}
54+
fn ptr_wrapper(self: Ptr<Wrapper<Self>>) -> i32 {
55+
**self
56+
}
57+
fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32 {
58+
***self
59+
}
60+
}
61+
62+
fn main() {
63+
let pw = Ptr(box Wrapper(5)) as Ptr<Wrapper<Debug>>;
64+
assert_eq!(pw.ptr_wrapper(), 5);
65+
66+
let wpw = Wrapper(Ptr(box Wrapper(6))) as Wrapper<Ptr<Wrapper<Debug>>>;
67+
assert_eq!(wpw.wrapper_ptr_wrapper(), 6);
68+
}

0 commit comments

Comments
 (0)