@@ -336,7 +336,7 @@ macro_rules! clone {
336
336
// clone!(|| {});
337
337
compile_error!( "If you have nothing to clone, no need to use this macro!" ) ;
338
338
) ;
339
- ( $( move) ? | $( $pattern : pat ) ,* | $( $_: tt) * ) => (
339
+ ( $( move) ? | $( $arg : tt $ ( : $typ : ty ) ? ) ,* | $( $_: tt) * ) => (
340
340
// In case we have:
341
341
// clone!(|a, b| {});
342
342
compile_error!( "If you have nothing to clone, no need to use this macro!" )
@@ -372,37 +372,37 @@ macro_rules! clone {
372
372
( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => $( @default -return $return_value: expr, ) ? move || $body: expr ) => (
373
373
clone!( $( $( @ $strength) ? $( $variables) .+ $( as $rename) ?) ,* => $( @default -return $return_value, ) ? move || { $body } )
374
374
) ;
375
- ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => @default -panic, move | $( $pattern : pat ) ,* | $body: block ) => (
375
+ ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => @default -panic, move | $( $arg : tt $ ( : $typ : ty ) ? ) ,* | $body: block ) => (
376
376
{
377
377
$( $crate:: to_type_before!( $( @ $strength) ? $( $variables) .+ $( as $rename) ?) ; ) *
378
- move |$( $pattern ) ,* | {
378
+ move |$( $arg $ ( : $typ ) ? ) ,* | {
379
379
$( $crate:: to_type_after!( $( as $rename) ? @default -panic, $( @ $strength) ? $( $variables) .+) ; ) *
380
380
$body
381
381
}
382
382
}
383
383
) ;
384
- ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => @default -panic, move | $( $pattern : pat ) ,* | $body: expr ) => (
385
- clone!( $( $( @ $strength) ? $( $variables) .+ $( as $rename) ?) ,* => @default -panic, move |$( $pattern ) ,* | { $body } )
384
+ ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => @default -panic, move | $( $arg : tt $ ( : $typ : ty ) ? ) ,* | $body: expr ) => (
385
+ clone!( $( $( @ $strength) ? $( $variables) .+ $( as $rename) ?) ,* => @default -panic, move |$( $arg $ ( : $typ ) ? ) ,* | { $body } )
386
386
) ;
387
- ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => $( @default -return $return_value: expr, ) ? move | $( $pattern : pat ) ,* | $body: block ) => (
387
+ ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => $( @default -return $return_value: expr, ) ? move | $( $arg : tt $ ( : $typ : ty ) ? ) ,* | $body: block ) => (
388
388
{
389
389
$( $crate:: to_type_before!( $( @ $strength) ? $( $variables) .+ $( as $rename) ?) ; ) *
390
- move |$( $pattern ) , * | {
390
+ move | $( $arg $ ( : $typ ) ? ) , * | {
391
391
let _return_value = || $crate:: to_return_value!( $( $return_value) ?) ;
392
392
$( $crate:: to_type_after!( $( as $rename) ? $( @ $strength) ? $( $variables) .+, _return_value) ; ) *
393
393
$body
394
394
}
395
395
}
396
396
) ;
397
- ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => $( @default -return $return_value: expr, ) ? move | $( $pattern : pat ) ,* | $body: expr ) => (
398
- clone!( $( $( @ $strength) ? $( $variables) .+ $( as $rename) ?) ,+ => $( @default -return $return_value, ) ? move |$( $pattern ) ,* | { $body } )
397
+ ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => $( @default -return $return_value: expr, ) ? move | $( $arg : tt $ ( : $typ : ty ) ? ) ,* | $body: expr ) => (
398
+ clone!( $( $( @ $strength) ? $( $variables) .+ $( as $rename) ?) ,+ => $( @default -return $return_value, ) ? move |$( $arg $ ( : $typ ) ? ) ,* | { $body } )
399
399
) ;
400
400
( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => @default -return $return_value: expr, || $body: block ) => (
401
401
// In case we have:
402
402
// clone!(@weak foo => @default-return false, || {});
403
403
compile_error!( "Closure needs to be \" moved\" so please add `move` before closure" ) ;
404
404
) ;
405
- ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => @default -return $return_value: expr, | $( $pattern : pat ) ,* | $body: block ) => (
405
+ ( $( $( @ $strength: ident) ? $( $variables: ident) .+ $( as $rename: ident) ?) ,+ => @default -return $return_value: expr, | $( $arg : tt $ ( : $typ : ty ) ? ) ,* | $body: block ) => (
406
406
// In case we have:
407
407
// clone!(@weak foo => @default-return false, |bla| {});
408
408
compile_error!( "Closure needs to be \" moved\" so please add `move` before closure" ) ;
@@ -669,4 +669,39 @@ mod tests {
669
669
closure ( 0i8 ) ; // to prevent compiler error for unknown `x` type.
670
670
let _ = clone ! ( @strong v, @strong w as _x => @default -return true , move || false ) ;
671
671
}
672
+
673
+ #[ test]
674
+ fn test_clone_macro_typed_args ( ) {
675
+ let v = Rc :: new ( 1 ) ;
676
+ let w = Rc :: new ( 2 ) ;
677
+
678
+ let closure = clone ! ( @weak v as x, @weak w => @default -panic, move |_x: i8 | {
679
+ println!( "v: {}, w: {}" , x, w) ;
680
+ } ) ;
681
+
682
+ let closure = clone ! ( @weak v, @weak w as x => @default -panic, move |_x: i8 | {
683
+ println!( "v: {}, w: {}" , v, x) ;
684
+ } ) ;
685
+
686
+ let closure = clone ! ( @strong v as x, @strong w => @default -panic, move |_x: i8 | {
687
+ println!( "v: {}, w: {}" , x, w) ;
688
+ } ) ;
689
+
690
+ let closure = clone ! ( @strong v, @strong w as x => @default -panic, move |_x: i8 | {
691
+ println!( "v: {}, w: {}" , v, x) ;
692
+ } ) ;
693
+
694
+ let closure = clone ! ( @weak v as x, @weak w => move |_x: i8 | {
695
+ println!( "v: {}, w: {}" , x, w) ;
696
+ } ) ;
697
+
698
+ let closure = clone ! ( @weak v, @weak w as x => move |_x: i8 | {
699
+ println!( "v: {}, w: {}" , v, x) ;
700
+ } ) ;
701
+
702
+ let closure = clone ! ( @weak v, @weak w as x => move |_: i8 , _| {
703
+ println!( "v: {}, w: {}" , v, x) ;
704
+ } ) ;
705
+ closure ( 0 , 'a' ) ;
706
+ }
672
707
}
0 commit comments