2016-09-22 06:00:43 -05:00
|
|
|
// Test DST raw pointers
|
|
|
|
|
|
|
|
trait Trait {
|
|
|
|
fn foo(&self) -> isize;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct A {
|
2022-06-20 17:30:34 -05:00
|
|
|
f: isize,
|
2016-09-22 06:00:43 -05:00
|
|
|
}
|
|
|
|
impl Trait for A {
|
|
|
|
fn foo(&self) -> isize {
|
|
|
|
self.f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Foo<T: ?Sized> {
|
2022-06-20 17:30:34 -05:00
|
|
|
f: T,
|
2016-09-22 06:00:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
// raw trait object
|
|
|
|
let x = A { f: 42 };
|
2019-05-30 03:58:30 -05:00
|
|
|
let z: *const dyn Trait = &x;
|
2022-06-20 17:30:34 -05:00
|
|
|
let r = unsafe { (&*z).foo() };
|
2016-09-22 06:00:43 -05:00
|
|
|
assert_eq!(r, 42);
|
|
|
|
|
|
|
|
// raw DST struct
|
2022-06-20 17:30:34 -05:00
|
|
|
let p = Foo { f: A { f: 42 } };
|
2019-05-30 03:58:30 -05:00
|
|
|
let o: *const Foo<dyn Trait> = &p;
|
2022-06-20 17:30:34 -05:00
|
|
|
let r = unsafe { (&*o).f.foo() };
|
2016-09-22 06:00:43 -05:00
|
|
|
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
|
2022-06-20 17:30:34 -05:00
|
|
|
let c: *const Foo<[_]> = &Foo { f: [1, 2, 3] };
|
2016-09-22 06:00:43 -05:00
|
|
|
unsafe {
|
|
|
|
let b = (&*c).f[0];
|
|
|
|
assert_eq!(b, 1);
|
|
|
|
let len = (&*c).f.len();
|
|
|
|
assert_eq!(len, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// all of the above with *mut
|
|
|
|
let mut x = A { f: 42 };
|
2019-05-30 03:58:30 -05:00
|
|
|
let z: *mut dyn Trait = &mut x;
|
2022-06-20 17:30:34 -05:00
|
|
|
let r = unsafe { (&*z).foo() };
|
2016-09-22 06:00:43 -05:00
|
|
|
assert_eq!(r, 42);
|
|
|
|
|
2022-06-20 17:30:34 -05:00
|
|
|
let mut p = Foo { f: A { f: 42 } };
|
2019-05-30 03:58:30 -05:00
|
|
|
let o: *mut Foo<dyn Trait> = &mut p;
|
2022-06-20 17:30:34 -05:00
|
|
|
let r = unsafe { (&*o).f.foo() };
|
2016-09-22 06:00:43 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-06-20 17:30:34 -05:00
|
|
|
let c: *mut Foo<[_]> = &mut Foo { f: [1, 2, 3] };
|
2016-09-22 06:00:43 -05:00
|
|
|
unsafe {
|
|
|
|
let b = (&*c).f[0];
|
|
|
|
assert_eq!(b, 1);
|
|
|
|
let len = (&*c).f.len();
|
|
|
|
assert_eq!(len, 3);
|
|
|
|
}
|
|
|
|
}
|