1
- #![ feature( allocator_api) ]
1
+ #![ allow( incomplete_features) ] // for triat upcasting
2
+ #![ feature( allocator_api, trait_upcasting) ]
2
3
3
4
use std:: alloc:: { AllocError , Allocator } ;
4
5
use std:: alloc:: Layout ;
@@ -37,6 +38,10 @@ impl MyTrait for [u8; 1] {
37
38
}
38
39
}
39
40
41
+ trait TheTrait : MyTrait { }
42
+
43
+ impl TheTrait for [ u8 ; 1 ] { }
44
+
40
45
/// `Box<T, G>` is a `ScalarPair` where the 2nd component is the allocator.
41
46
fn test1 ( ) {
42
47
let mut space = vec ! [ MaybeUninit :: new( 0 ) ; 1 ] ;
@@ -46,6 +51,10 @@ fn test1() {
46
51
47
52
let boxed = Box :: new_in ( [ 42u8 ; 1 ] , & once_alloc) ;
48
53
let _val = * boxed;
54
+ let with_dyn: Box < dyn TheTrait , & OnceAlloc > = boxed;
55
+ assert_eq ! ( 42 , with_dyn. hello( ) ) ;
56
+ let with_dyn: Box < dyn MyTrait , & OnceAlloc > = with_dyn; // upcast
57
+ assert_eq ! ( 42 , with_dyn. hello( ) ) ;
49
58
}
50
59
51
60
// Make the allocator itself so big that the Box is not even a ScalarPair any more.
@@ -70,6 +79,10 @@ fn test2() {
70
79
71
80
let boxed = Box :: new_in ( [ 0u8 ; 1 ] , OnceAllocRef ( & once_alloc, 0 ) ) ;
72
81
let _val = * boxed;
82
+ let with_dyn: Box < dyn TheTrait , OnceAllocRef > = boxed;
83
+ assert_eq ! ( 42 , with_dyn. hello( ) ) ;
84
+ let with_dyn: Box < dyn MyTrait , OnceAllocRef > = with_dyn; // upcast
85
+ assert_eq ! ( 42 , with_dyn. hello( ) ) ;
73
86
}
74
87
75
88
fn main ( ) {
0 commit comments