]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/mir_fat_ptr.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / test / run-pass / mir_fat_ptr.rs
1 // Copyright 2015 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 that ordinary fat pointer operations work.
12
13 #![feature(rustc_attrs)]
14
15 struct Wrapper<T: ?Sized>(u32, T);
16
17 struct FatPtrContainer<'a> {
18 ptr: &'a [u8]
19 }
20
21 #[rustc_mir]
22 fn fat_ptr_project(a: &Wrapper<[u8]>) -> &[u8] {
23 &a.1
24 }
25
26 #[rustc_mir]
27 fn fat_ptr_simple(a: &[u8]) -> &[u8] {
28 a
29 }
30
31 #[rustc_mir]
32 fn fat_ptr_via_local(a: &[u8]) -> &[u8] {
33 let x = a;
34 x
35 }
36
37 #[rustc_mir]
38 fn fat_ptr_from_struct(s: FatPtrContainer) -> &[u8] {
39 s.ptr
40 }
41
42 #[rustc_mir]
43 fn fat_ptr_to_struct(a: &[u8]) -> FatPtrContainer {
44 FatPtrContainer { ptr: a }
45 }
46
47 #[rustc_mir]
48 fn fat_ptr_store_to<'a>(a: &'a [u8], b: &mut &'a [u8]) {
49 *b = a;
50 }
51
52 fn main() {
53 let a = Wrapper(4, [7,6,5]);
54
55 let p = fat_ptr_project(&a);
56 let p = fat_ptr_simple(p);
57 let p = fat_ptr_via_local(p);
58 let p = fat_ptr_from_struct(fat_ptr_to_struct(p));
59
60 let mut target : &[u8] = &[42];
61 fat_ptr_store_to(p, &mut target);
62 assert_eq!(target, &a.1);
63 }