]> git.lizzy.rs Git - rust.git/blob - src/test/ui/dynamically-sized-types/dst-coerce-custom.rs
Rollup merge of #89468 - FabianWolff:issue-89358, r=jackh726
[rust.git] / src / test / ui / dynamically-sized-types / dst-coerce-custom.rs
1 // run-pass
2 // Test a very simple custom DST coercion.
3
4 #![feature(unsize, coerce_unsized)]
5
6 use std::ops::CoerceUnsized;
7 use std::marker::Unsize;
8
9 struct Bar<T: ?Sized> {
10     x: *const T,
11 }
12
13 impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Bar<U>> for Bar<T> {}
14
15 trait Baz {
16     fn get(&self) -> i32;
17 }
18
19 impl Baz for i32 {
20     fn get(&self) -> i32 {
21         *self
22     }
23 }
24
25 fn main() {
26     // Arrays.
27     let a: Bar<[i32; 3]> = Bar { x: &[1, 2, 3] };
28     // This is the actual coercion.
29     let b: Bar<[i32]> = a;
30
31     unsafe {
32         assert_eq!((*b.x)[0], 1);
33         assert_eq!((*b.x)[1], 2);
34         assert_eq!((*b.x)[2], 3);
35     }
36
37     // Trait objects.
38     let a: Bar<i32> = Bar { x: &42 };
39     let b: Bar<dyn Baz> = a;
40     unsafe {
41         assert_eq!((*b.x).get(), 42);
42     }
43 }