]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/dst-coerce-custom.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / test / run-pass / dst-coerce-custom.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 a very simple custom DST coercion.
12
13 #![feature(unsize, coerce_unsized)]
14
15 use std::ops::CoerceUnsized;
16 use std::marker::Unsize;
17
18 struct Bar<T: ?Sized> {
19 x: *const T,
20 }
21
22 impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Bar<U>> for Bar<T> {}
23
24 trait Baz {
25 fn get(&self) -> i32;
26 }
27
28 impl Baz for i32 {
29 fn get(&self) -> i32 {
30 *self
31 }
32 }
33
34 fn main() {
35 // Arrays.
36 let a: Bar<[i32; 3]> = Bar { x: &[1, 2, 3] };
37 // This is the actual coercion.
38 let b: Bar<[i32]> = a;
39
40 unsafe {
41 assert_eq!((*b.x)[0], 1);
42 assert_eq!((*b.x)[1], 2);
43 assert_eq!((*b.x)[2], 3);
44 }
45
46 // Trait objects.
47 let a: Bar<i32> = Bar { x: &42 };
48 let b: Bar<Baz> = a;
49 unsafe {
50 assert_eq!((*b.x).get(), 42);
51 }
52 }