File tree Expand file tree Collapse file tree 2 files changed +23
-15
lines changed Expand file tree Collapse file tree 2 files changed +23
-15
lines changed Original file line number Diff line number Diff line change @@ -40,10 +40,21 @@ fn fn_once_closure_with_multiple_args() -> i64 {
4040 }
4141}
4242
43- fn boxed ( f : Box < dyn FnOnce ( ) -> i32 > ) -> i32 {
43+ fn boxed_fn_once ( f : Box < dyn FnOnce ( ) -> i32 > ) -> i32 {
4444 f ( )
4545}
4646
47+ fn box_dyn ( ) {
48+ let x: Box < dyn Fn ( i32 ) -> i32 > = Box :: new ( |x| x * 2 ) ;
49+ assert_eq ! ( x( 21 ) , 42 ) ;
50+ let mut i = 5 ;
51+ {
52+ let mut x: Box < dyn FnMut ( ) > = Box :: new ( || i *= 2 ) ;
53+ x ( ) ; x ( ) ;
54+ }
55+ assert_eq ! ( i, 20 ) ;
56+ }
57+
4758fn fn_item_as_closure_trait_object ( ) {
4859 fn foo ( ) { }
4960 let f: & dyn Fn ( ) = & foo;
@@ -96,8 +107,9 @@ fn main() {
96107 assert_eq ! ( crazy_closure( ) , ( 84 , 10 , 10 ) ) ;
97108 assert_eq ! ( closure_arg_adjustment_problem( ) , 3 ) ;
98109 assert_eq ! ( fn_once_closure_with_multiple_args( ) , 6 ) ;
99- assert_eq ! ( boxed ( Box :: new( { let x = 13 ; move || x} ) ) , 13 ) ;
110+ assert_eq ! ( boxed_fn_once ( Box :: new( { let x = 13 ; move || x} ) ) , 13 ) ;
100111
112+ box_dyn ( ) ;
101113 fn_item_as_closure_trait_object ( ) ;
102114 fn_item_with_args_as_closure_trait_object ( ) ;
103115 fn_item_with_multiple_args_as_closure_trait_object ( ) ;
Original file line number Diff line number Diff line change 11#![ feature( unsized_locals) ]
22
3- fn ref_dyn ( ) {
3+ fn ref_box_dyn ( ) {
44 struct Struct ( i32 ) ;
55
66 trait Trait {
@@ -17,22 +17,19 @@ fn ref_dyn() {
1717
1818 let y: & dyn Trait = & Struct ( 42 ) ;
1919 y. method ( ) ;
20+
2021 let x: Foo < Struct > = Foo ( Struct ( 42 ) ) ;
2122 let y: & Foo < dyn Trait > = & x;
2223 y. 0 . method ( ) ;
23- }
2424
25- fn box_dyn ( ) {
26- let x: Box < dyn Fn ( i32 ) -> i32 > = Box :: new ( |x| x * 2 ) ;
27- assert_eq ! ( x( 21 ) , 42 ) ;
28- let mut i = 5 ;
29- {
30- let mut x: Box < dyn FnMut ( ) > = Box :: new ( || i *= 2 ) ;
31- x ( ) ; x ( ) ;
32- }
33- assert_eq ! ( i, 20 ) ;
25+ let y: Box < dyn Trait > = Box :: new ( Struct ( 42 ) ) ;
26+ y. method ( ) ;
27+
28+ let y = & y;
29+ y. method ( ) ;
3430}
3531
32+
3633fn box_box_trait ( ) {
3734 struct DroppableStruct ;
3835
@@ -130,8 +127,7 @@ fn unsized_dyn_autoderef() {
130127}
131128
132129fn main ( ) {
133- ref_dyn ( ) ;
134- box_dyn ( ) ;
130+ ref_box_dyn ( ) ;
135131 box_box_trait ( ) ;
136132
137133 // "exotic" receivers
You can’t perform that action at this time.
0 commit comments