1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test DST raw pointers
#![feature(unsized_tuple_coercion)]
trait Trait {
fn foo(&self) -> isize;
}
struct A {
f: isize
}
impl Trait for A {
fn foo(&self) -> isize {
self.f
}
}
struct Foo<T: ?Sized> {
f: T
}
pub fn main() {
// raw trait object
let x = A { f: 42 };
let z: *const Trait = &x;
let r = unsafe {
(&*z).foo()
};
assert_eq!(r, 42);
// raw DST struct
let p = Foo {f: A { f: 42 }};
let o: *const Foo<Trait> = &p;
let r = unsafe {
(&*o).f.foo()
};
assert_eq!(r, 42);
// raw DST tuple
let p = (A { f: 42 },);
let o: *const (Trait,) = &p;
let r = unsafe {
(&*o).0.foo()
};
assert_eq!(r, 42);
// raw slice
let a: *const [_] = &[1, 2, 3];
unsafe {
let b = (*a)[2];
assert_eq!(b, 3);
let len = (*a).len();
assert_eq!(len, 3);
}
// raw slice with explicit cast
let a = &[1, 2, 3] as *const [i32];
unsafe {
let b = (*a)[2];
assert_eq!(b, 3);
let len = (*a).len();
assert_eq!(len, 3);
}
// raw DST struct with slice
let c: *const Foo<[_]> = &Foo {f: [1, 2, 3]};
unsafe {
let b = (&*c).f[0];
assert_eq!(b, 1);
let len = (&*c).f.len();
assert_eq!(len, 3);
}
// raw DST tuple with slice
let c: *const ([_],) = &([1, 2, 3],);
unsafe {
let b = (&*c).0[0];
assert_eq!(b, 1);
let len = (&*c).0.len();
assert_eq!(len, 3);
}
// all of the above with *mut
let mut x = A { f: 42 };
let z: *mut Trait = &mut x;
let r = unsafe {
(&*z).foo()
};
assert_eq!(r, 42);
let mut p = Foo {f: A { f: 42 }};
let o: *mut Foo<Trait> = &mut p;
let r = unsafe {
(&*o).f.foo()
};
assert_eq!(r, 42);
let mut p = (A { f: 42 },);
let o: *mut (Trait,) = &mut p;
let r = unsafe {
(&*o).0.foo()
};
assert_eq!(r, 42);
let a: *mut [_] = &mut [1, 2, 3];
unsafe {
let b = (*a)[2];
assert_eq!(b, 3);
let len = (*a).len();
assert_eq!(len, 3);
}
let a = &mut [1, 2, 3] as *mut [i32];
unsafe {
let b = (*a)[2];
assert_eq!(b, 3);
let len = (*a).len();
assert_eq!(len, 3);
}
let c: *mut Foo<[_]> = &mut Foo {f: [1, 2, 3]};
unsafe {
let b = (&*c).f[0];
assert_eq!(b, 1);
let len = (&*c).f.len();
assert_eq!(len, 3);
}
let c: *mut ([_],) = &mut ([1, 2, 3],);
unsafe {
let b = (&*c).0[0];
assert_eq!(b, 1);
let len = (&*c).0.len();
assert_eq!(len, 3);
}
}
|