1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Test structs with always-unsized fields.
15 #![feature(box_syntax, unsize, raw)]
36 fn foo(&self) -> usize;
44 fn foo(&self) -> usize {
62 let _ = mem::size_of::<Box<Foo<u8>>>();
63 let _ = mem::size_of::<Box<Bar>>();
64 let _ = mem::size_of::<Box<Baz>>();
71 let data: Box<Foo_<i32>> = box Foo_{f: [1, 2, 3] };
72 let x: &Foo<i32> = mem::transmute(slice::from_raw_parts(&*data, 3));
73 assert_eq!(x.f.len(), 3);
74 assert_eq!(x.f[0], 1);
81 let data: Box<_> = box Baz_ {
82 f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
83 let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5));
85 let chs: Vec<char> = x.f2.chars().collect();
86 assert_eq!(chs.len(), 5);
87 assert_eq!(chs[0], 'a');
88 assert_eq!(chs[1], 'b');
89 assert_eq!(chs[2], 'c');
90 assert_eq!(chs[3], 'd');
91 assert_eq!(chs[4], 'e');
97 let obj: Box<St> = box St { f: 42 };
99 let obj: raw::TraitObject = mem::transmute(&*obj);
100 let data: Box<_> = box Qux_{ f: St { f: 234 } };
101 let x: &Qux = mem::transmute(raw::TraitObject { vtable: obj.vtable,
102 data: mem::transmute(&*data) });
103 assert_eq!(x.f.foo(), 234);