2019-07-26 16:54:25 -05:00
|
|
|
// run-pass
|
2014-09-08 23:54:13 -05:00
|
|
|
// Test structs with always-unsized fields.
|
|
|
|
|
2015-06-10 15:33:52 -05:00
|
|
|
#![allow(warnings)]
|
2021-07-02 23:24:47 -05:00
|
|
|
#![feature(box_syntax, unsize, ptr_metadata)]
|
2015-01-07 19:25:56 -06:00
|
|
|
|
2014-09-08 23:54:13 -05:00
|
|
|
use std::mem;
|
2021-07-02 23:24:47 -05:00
|
|
|
use std::ptr;
|
2015-03-13 07:09:34 -05:00
|
|
|
use std::slice;
|
2014-09-08 23:54:13 -05:00
|
|
|
|
|
|
|
struct Foo<T> {
|
|
|
|
f: [T],
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar {
|
2015-03-13 07:09:34 -05:00
|
|
|
f1: usize,
|
|
|
|
f2: [usize],
|
2014-09-08 23:54:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Baz {
|
2015-03-13 07:09:34 -05:00
|
|
|
f1: usize,
|
2014-09-08 23:54:13 -05:00
|
|
|
f2: str,
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Tr {
|
2015-03-13 07:09:34 -05:00
|
|
|
fn foo(&self) -> usize;
|
2014-09-08 23:54:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct St {
|
2021-07-02 23:24:47 -05:00
|
|
|
f: usize,
|
2014-09-08 23:54:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Tr for St {
|
2015-03-13 07:09:34 -05:00
|
|
|
fn foo(&self) -> usize {
|
2014-09-08 23:54:13 -05:00
|
|
|
self.f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Qux<'a> {
|
2021-07-02 23:24:47 -05:00
|
|
|
f: Tr + 'a,
|
2014-09-08 23:54:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let _: &Foo<f64>;
|
|
|
|
let _: &Bar;
|
|
|
|
let _: &Baz;
|
|
|
|
|
|
|
|
let _: Box<Foo<i32>>;
|
|
|
|
let _: Box<Bar>;
|
|
|
|
let _: Box<Baz>;
|
|
|
|
|
|
|
|
let _ = mem::size_of::<Box<Foo<u8>>>();
|
|
|
|
let _ = mem::size_of::<Box<Bar>>();
|
|
|
|
let _ = mem::size_of::<Box<Baz>>();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
struct Foo_<T> {
|
2021-07-02 23:24:47 -05:00
|
|
|
f: [T; 3],
|
2014-09-08 23:54:13 -05:00
|
|
|
}
|
|
|
|
|
2021-07-02 23:24:47 -05:00
|
|
|
let data: Box<Foo_<i32>> = box Foo_ { f: [1, 2, 3] };
|
2015-03-13 07:09:34 -05:00
|
|
|
let x: &Foo<i32> = mem::transmute(slice::from_raw_parts(&*data, 3));
|
2015-06-07 13:00:38 -05:00
|
|
|
assert_eq!(x.f.len(), 3);
|
|
|
|
assert_eq!(x.f[0], 1);
|
2014-09-08 23:54:13 -05:00
|
|
|
|
|
|
|
struct Baz_ {
|
2015-03-13 07:09:34 -05:00
|
|
|
f1: usize,
|
2014-12-19 20:20:51 -06:00
|
|
|
f2: [u8; 5],
|
2014-09-08 23:54:13 -05:00
|
|
|
}
|
|
|
|
|
2021-07-02 23:24:47 -05:00
|
|
|
let data: Box<_> =
|
|
|
|
box Baz_ { f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
|
2015-03-13 07:09:34 -05:00
|
|
|
let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5));
|
2015-06-07 13:00:38 -05:00
|
|
|
assert_eq!(x.f1, 42);
|
2014-09-08 23:54:13 -05:00
|
|
|
let chs: Vec<char> = x.f2.chars().collect();
|
2015-06-07 13:00:38 -05:00
|
|
|
assert_eq!(chs.len(), 5);
|
|
|
|
assert_eq!(chs[0], 'a');
|
|
|
|
assert_eq!(chs[1], 'b');
|
|
|
|
assert_eq!(chs[2], 'c');
|
|
|
|
assert_eq!(chs[3], 'd');
|
|
|
|
assert_eq!(chs[4], 'e');
|
2014-09-08 23:54:13 -05:00
|
|
|
|
|
|
|
struct Qux_ {
|
2021-07-02 23:24:47 -05:00
|
|
|
f: St,
|
2014-09-08 23:54:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let obj: Box<St> = box St { f: 42 };
|
|
|
|
let obj: &Tr = &*obj;
|
2021-07-02 23:24:47 -05:00
|
|
|
let data: Box<_> = box Qux_ { f: St { f: 234 } };
|
|
|
|
let x: &Qux = &*ptr::from_raw_parts::<Qux>((&*data as *const _).cast(), ptr::metadata(obj));
|
2015-06-07 13:00:38 -05:00
|
|
|
assert_eq!(x.f.foo(), 234);
|
2014-09-08 23:54:13 -05:00
|
|
|
}
|
|
|
|
}
|