]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/unsized3.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / test / run-pass / unsized3.rs
CommitLineData
1a4d82fc
JJ
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.
4//
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.
10
11// Test structs with always-unsized fields.
12
c34b1796
AL
13// pretty-expanded FIXME #23616
14
1a4d82fc 15#![allow(unknown_features)]
c34b1796 16#![feature(box_syntax, core)]
1a4d82fc
JJ
17
18use std::mem;
19use std::raw;
c34b1796 20use std::slice;
1a4d82fc
JJ
21
22struct Foo<T> {
23 f: [T],
24}
25
26struct Bar {
c34b1796
AL
27 f1: usize,
28 f2: [usize],
1a4d82fc
JJ
29}
30
31struct Baz {
c34b1796 32 f1: usize,
1a4d82fc
JJ
33 f2: str,
34}
35
36trait Tr {
c34b1796 37 fn foo(&self) -> usize;
1a4d82fc
JJ
38}
39
40struct St {
c34b1796 41 f: usize
1a4d82fc
JJ
42}
43
44impl Tr for St {
c34b1796 45 fn foo(&self) -> usize {
1a4d82fc
JJ
46 self.f
47 }
48}
49
50struct Qux<'a> {
51 f: Tr+'a
52}
53
54pub fn main() {
55 let _: &Foo<f64>;
56 let _: &Bar;
57 let _: &Baz;
58
59 let _: Box<Foo<i32>>;
60 let _: Box<Bar>;
61 let _: Box<Baz>;
62
63 let _ = mem::size_of::<Box<Foo<u8>>>();
64 let _ = mem::size_of::<Box<Bar>>();
65 let _ = mem::size_of::<Box<Baz>>();
66
67 unsafe {
68 struct Foo_<T> {
69 f: [T; 3]
70 }
71
c34b1796
AL
72 let data: Box<Foo_<i32>> = box Foo_{f: [1, 2, 3] };
73 let x: &Foo<i32> = mem::transmute(slice::from_raw_parts(&*data, 3));
1a4d82fc
JJ
74 assert!(x.f.len() == 3);
75 assert!(x.f[0] == 1);
1a4d82fc
JJ
76
77 struct Baz_ {
c34b1796 78 f1: usize,
1a4d82fc
JJ
79 f2: [u8; 5],
80 }
81
c34b1796
AL
82 let data: Box<_> = box Baz_ {
83 f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
84 let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5));
1a4d82fc
JJ
85 assert!(x.f1 == 42);
86 let chs: Vec<char> = x.f2.chars().collect();
87 assert!(chs.len() == 5);
88 assert!(chs[0] == 'a');
89 assert!(chs[1] == 'b');
90 assert!(chs[2] == 'c');
91 assert!(chs[3] == 'd');
92 assert!(chs[4] == 'e');
93
94 struct Qux_ {
95 f: St
96 }
97
98 let obj: Box<St> = box St { f: 42 };
99 let obj: &Tr = &*obj;
100 let obj: raw::TraitObject = mem::transmute(&*obj);
c34b1796 101 let data: Box<_> = box Qux_{ f: St { f: 234 } };
1a4d82fc
JJ
102 let x: &Qux = mem::transmute(raw::TraitObject { vtable: obj.vtable,
103 data: mem::transmute(&*data) });
104 assert!(x.f.foo() == 234);
105 }
106}