File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -434,3 +434,23 @@ fn test_array_from_slice() {
434434 let a: Result < Rc < [ u32 ; 2 ] > , _ > = r. clone ( ) . try_into ( ) ;
435435 assert ! ( a. is_err( ) ) ;
436436}
437+
438+ #[ test]
439+ fn test_fn ( ) {
440+ fn apply_fn_once < T > ( v : T , f : impl FnOnce ( T ) ) {
441+ f ( v)
442+ }
443+ fn apply_fn_mut < T > ( v : T , mut f : impl FnMut ( T ) ) {
444+ f ( v)
445+ }
446+ fn apply_fn < T > ( v : T , f : impl Fn ( T ) ) {
447+ f ( v)
448+ }
449+
450+ let x = RefCell :: new ( 0 ) ;
451+ let f = Rc :: new ( |v : i32 | * x. borrow_mut ( ) += v) ;
452+ apply_fn_once ( 1 , f. clone ( ) ) ;
453+ apply_fn_mut ( 2 , f. clone ( ) ) ;
454+ apply_fn ( 4 , f. clone ( ) ) ;
455+ assert_eq ! ( * x. borrow( ) , 7 ) ;
456+ }
Original file line number Diff line number Diff line change @@ -490,3 +490,23 @@ fn test_array_from_slice() {
490490 let a: Result < Arc < [ u32 ; 2 ] > , _ > = r. clone ( ) . try_into ( ) ;
491491 assert ! ( a. is_err( ) ) ;
492492}
493+
494+ #[ test]
495+ fn test_fn ( ) {
496+ fn apply_fn_once < T > ( v : T , f : impl FnOnce ( T ) ) {
497+ f ( v)
498+ }
499+ fn apply_fn_mut < T > ( v : T , mut f : impl FnMut ( T ) ) {
500+ f ( v)
501+ }
502+ fn apply_fn < T > ( v : T , f : impl Fn ( T ) ) {
503+ f ( v)
504+ }
505+
506+ let x = Mutex :: new ( 0 ) ;
507+ let f = Arc :: new ( |v : i32 | * x. lock ( ) . unwrap ( ) += v) ;
508+ apply_fn_once ( 1 , f. clone ( ) ) ;
509+ apply_fn_mut ( 2 , f. clone ( ) ) ;
510+ apply_fn ( 4 , f. clone ( ) ) ;
511+ assert_eq ! ( * x. lock( ) . unwrap( ) , 7 ) ;
512+ }
You can’t perform that action at this time.
0 commit comments