2014-08-27 00:07:28 -05:00
|
|
|
// 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
|
|
|
|
|
2015-03-22 15:13:15 -05:00
|
|
|
|
2014-08-27 00:07:28 -05:00
|
|
|
trait Trait {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn foo(&self) -> isize;
|
2014-08-27 00:07:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct A {
|
2015-03-25 19:06:52 -05:00
|
|
|
f: isize
|
2014-08-27 00:07:28 -05:00
|
|
|
}
|
|
|
|
impl Trait for A {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn foo(&self) -> isize {
|
2014-08-27 00:07:28 -05:00
|
|
|
self.f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 01:05:30 -06:00
|
|
|
struct Foo<T: ?Sized> {
|
2014-08-27 00:07:28 -05:00
|
|
|
f: T
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
// raw trait object
|
|
|
|
let x = A { f: 42 };
|
2014-09-01 18:25:01 -05:00
|
|
|
let z: *const Trait = &x;
|
2014-08-27 00:07:28 -05:00
|
|
|
let r = unsafe {
|
|
|
|
(&*z).foo()
|
|
|
|
};
|
|
|
|
assert!(r == 42);
|
|
|
|
|
|
|
|
// raw DST struct
|
|
|
|
let p = Foo {f: A { f: 42 }};
|
2014-09-01 18:25:01 -05:00
|
|
|
let o: *const Foo<Trait> = &p;
|
2014-08-27 00:07:28 -05:00
|
|
|
let r = unsafe {
|
|
|
|
(&*o).f.foo()
|
|
|
|
};
|
|
|
|
assert!(r == 42);
|
|
|
|
|
|
|
|
// raw slice
|
2015-01-25 15:05:03 -06:00
|
|
|
let a: *const [_] = &[1, 2, 3];
|
2014-08-27 00:07:28 -05:00
|
|
|
unsafe {
|
|
|
|
let b = (*a)[2];
|
|
|
|
assert!(b == 3);
|
2014-09-01 18:25:01 -05:00
|
|
|
let len = (*a).len();
|
|
|
|
assert!(len == 3);
|
2014-08-27 00:07:28 -05:00
|
|
|
}
|
|
|
|
|
2014-09-02 21:09:07 -05:00
|
|
|
// raw slice with explicit cast
|
2015-03-24 14:45:11 -05:00
|
|
|
let a = &[1, 2, 3] as *const [i32];
|
2014-09-02 21:09:07 -05:00
|
|
|
unsafe {
|
|
|
|
let b = (*a)[2];
|
|
|
|
assert!(b == 3);
|
|
|
|
let len = (*a).len();
|
|
|
|
assert!(len == 3);
|
|
|
|
}
|
|
|
|
|
2014-08-27 00:07:28 -05:00
|
|
|
// raw DST struct with slice
|
2015-01-25 15:05:03 -06:00
|
|
|
let c: *const Foo<[_]> = &Foo {f: [1, 2, 3]};
|
2014-08-27 00:07:28 -05:00
|
|
|
unsafe {
|
|
|
|
let b = (&*c).f[0];
|
|
|
|
assert!(b == 1);
|
2014-09-01 18:25:01 -05:00
|
|
|
let len = (&*c).f.len();
|
|
|
|
assert!(len == 3);
|
2014-08-27 00:07:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// all of the above with *mut
|
|
|
|
let mut x = A { f: 42 };
|
2014-09-01 18:25:01 -05:00
|
|
|
let z: *mut Trait = &mut x;
|
2014-08-27 00:07:28 -05:00
|
|
|
let r = unsafe {
|
|
|
|
(&*z).foo()
|
|
|
|
};
|
|
|
|
assert!(r == 42);
|
|
|
|
|
|
|
|
let mut p = Foo {f: A { f: 42 }};
|
2014-09-01 18:25:01 -05:00
|
|
|
let o: *mut Foo<Trait> = &mut p;
|
2014-08-27 00:07:28 -05:00
|
|
|
let r = unsafe {
|
|
|
|
(&*o).f.foo()
|
|
|
|
};
|
|
|
|
assert!(r == 42);
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let a: *mut [_] = &mut [1, 2, 3];
|
2014-08-27 00:07:28 -05:00
|
|
|
unsafe {
|
|
|
|
let b = (*a)[2];
|
|
|
|
assert!(b == 3);
|
2014-09-01 18:25:01 -05:00
|
|
|
let len = (*a).len();
|
|
|
|
assert!(len == 3);
|
2014-08-27 00:07:28 -05:00
|
|
|
}
|
|
|
|
|
2015-03-24 14:45:11 -05:00
|
|
|
let a = &mut [1, 2, 3] as *mut [i32];
|
2014-09-02 21:09:07 -05:00
|
|
|
unsafe {
|
|
|
|
let b = (*a)[2];
|
|
|
|
assert!(b == 3);
|
|
|
|
let len = (*a).len();
|
|
|
|
assert!(len == 3);
|
|
|
|
}
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let c: *mut Foo<[_]> = &mut Foo {f: [1, 2, 3]};
|
2014-08-27 00:07:28 -05:00
|
|
|
unsafe {
|
|
|
|
let b = (&*c).f[0];
|
|
|
|
assert!(b == 1);
|
2014-09-01 18:25:01 -05:00
|
|
|
let len = (&*c).f.len();
|
|
|
|
assert!(len == 3);
|
2014-08-27 00:07:28 -05:00
|
|
|
}
|
2014-09-14 22:27:36 -05:00
|
|
|
}
|