/
unsized3.rs
104 lines (85 loc) · 2.39 KB
/
unsized3.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test structs with always-unsized fields.
#![allow(unknown_features)]
#![feature(box_syntax)]
use std::mem;
use std::raw;
use std::slice;
struct Foo<T> {
f: [T],
}
struct Bar {
f1: usize,
f2: [usize],
}
struct Baz {
f1: usize,
f2: str,
}
trait Tr {
fn foo(&self) -> usize;
}
struct St {
f: usize
}
impl Tr for St {
fn foo(&self) -> usize {
self.f
}
}
struct Qux<'a> {
f: Tr+'a
}
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> {
f: [T; 3]
}
let data: Box<Foo_<i32>> = box Foo_{f: [1, 2, 3] };
let x: &Foo<i32> = mem::transmute(slice::from_raw_parts(&*data, 3));
assert!(x.f.len() == 3);
assert!(x.f[0] == 1);
struct Baz_ {
f1: usize,
f2: [u8; 5],
}
let data: Box<_> = box Baz_ {
f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5));
assert!(x.f1 == 42);
let chs: Vec<char> = x.f2.chars().collect();
assert!(chs.len() == 5);
assert!(chs[0] == 'a');
assert!(chs[1] == 'b');
assert!(chs[2] == 'c');
assert!(chs[3] == 'd');
assert!(chs[4] == 'e');
struct Qux_ {
f: St
}
let obj: Box<St> = box St { f: 42 };
let obj: &Tr = &*obj;
let obj: raw::TraitObject = mem::transmute(&*obj);
let data: Box<_> = box Qux_{ f: St { f: 234 } };
let x: &Qux = mem::transmute(raw::TraitObject { vtable: obj.vtable,
data: mem::transmute(&*data) });
assert!(x.f.foo() == 234);
}
}