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