|
| 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 | + |
| 13 | +use std::mem; |
| 14 | +use std::raw; |
| 15 | + |
| 16 | +struct Foo<T> { |
| 17 | + f: [T], |
| 18 | +} |
| 19 | + |
| 20 | +struct Bar { |
| 21 | + f1: uint, |
| 22 | + f2: [uint], |
| 23 | +} |
| 24 | + |
| 25 | +struct Baz { |
| 26 | + f1: uint, |
| 27 | + f2: str, |
| 28 | +} |
| 29 | + |
| 30 | +trait Tr { |
| 31 | + fn foo(&self) -> uint; |
| 32 | +} |
| 33 | + |
| 34 | +struct St { |
| 35 | + f: uint |
| 36 | +} |
| 37 | + |
| 38 | +impl Tr for St { |
| 39 | + fn foo(&self) -> uint { |
| 40 | + self.f |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +struct Qux<'a> { |
| 45 | + f: Tr+'a |
| 46 | +} |
| 47 | + |
| 48 | +pub fn main() { |
| 49 | + let _: &Foo<f64>; |
| 50 | + let _: &Bar; |
| 51 | + let _: &Baz; |
| 52 | + |
| 53 | + let _: Box<Foo<i32>>; |
| 54 | + let _: Box<Bar>; |
| 55 | + let _: Box<Baz>; |
| 56 | + |
| 57 | + let _ = mem::size_of::<Box<Foo<u8>>>(); |
| 58 | + let _ = mem::size_of::<Box<Bar>>(); |
| 59 | + let _ = mem::size_of::<Box<Baz>>(); |
| 60 | + |
| 61 | + unsafe { |
| 62 | + struct Foo_<T> { |
| 63 | + f: [T, ..3] |
| 64 | + } |
| 65 | + |
| 66 | + let data = box Foo_{f: [1i32, 2, 3] }; |
| 67 | + let x: &Foo<i32> = mem::transmute(raw::Slice { len: 3, data: &*data }); |
| 68 | + assert!(x.f.len() == 3); |
| 69 | + assert!(x.f[0] == 1); |
| 70 | + assert!(x.f[1] == 2); |
| 71 | + assert!(x.f[2] == 3); |
| 72 | + |
| 73 | + struct Baz_ { |
| 74 | + f1: uint, |
| 75 | + f2: [u8, ..5], |
| 76 | + } |
| 77 | + |
| 78 | + let data = box Baz_{ f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] }; |
| 79 | + let x: &Baz = mem::transmute( raw::Slice { len: 5, data: &*data } ); |
| 80 | + assert!(x.f1 == 42); |
| 81 | + let chs: Vec<char> = x.f2.chars().collect(); |
| 82 | + assert!(chs.len() == 5); |
| 83 | + assert!(chs[0] == 'a'); |
| 84 | + assert!(chs[1] == 'b'); |
| 85 | + assert!(chs[2] == 'c'); |
| 86 | + assert!(chs[3] == 'd'); |
| 87 | + assert!(chs[4] == 'e'); |
| 88 | + |
| 89 | + struct Qux_ { |
| 90 | + f: St |
| 91 | + } |
| 92 | + |
| 93 | + let obj: Box<St> = box St { f: 42 }; |
| 94 | + let obj: &Tr = &*obj; |
| 95 | + let obj: raw::TraitObject = mem::transmute(&*obj); |
| 96 | + let data = box Qux_{ f: St { f: 234 } }; |
| 97 | + let x: &Qux = mem::transmute(raw::TraitObject { vtable: obj.vtable, |
| 98 | + data: mem::transmute(&*data) }); |
| 99 | + assert!(x.f.foo() == 234); |
| 100 | + } |
| 101 | +} |
0 commit comments