1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
//! Macros //! //! Note that macros have been designed so that there is some //! punctuation and structure to the arguments, not merely a flat list //! of anonymous values. That makes it easier to remember what each //! part is. They have also been designed so that `rustfmt` will //! accept the code within the macro and format it. So the code must //! parse as valid Rust, even though the interpretation is different. //! //! Arguments are evaluated early where possible. This means that //! many borrowing problems common in Rust can be avoided, for example //! where argument expressions reference something already borrowed //! earlier in the arg-list, especially `Cx` references. So the code //! can look more natural. //! //! Also, argument types are checked early where possible, to give //! easier to understand error messages. /// Shorthand for context argument type /// /// Usually (for Rust 2018 edition) the context argument must be /// written `cx: &mut Cx<'_, Self>`. Using this macro it can instead /// be written `cx: CX![]`. This reduces the boilerplate, but keeps /// everything else as plain Rust. (The alternative would be to try /// to wrap the whole method in a macro or use procedural macros.) /// /// Note that sometimes you'll need a context with a different type /// than `Self`, in which case `cx: CX![OtherType]` may be used, /// equivalent to `cx: &mut Cx<'_, OtherType>`. #[macro_export] macro_rules! CX { () => { &mut $crate::Cx<'_, Self> }; ($other:ty) => { &mut $crate::Cx<'_, $other> }; } // Generate lists of indices from lists of `tt` AST objects. This is // used to convert arguments lists into indices so that a tuple can be // generated and then indexed using `tup.3`-style syntax. // // Lists of `tt` items must be enclosed in `[]` and put at the start // of the arg-list. Each list is then converted to a list of indices // contained in `[]` and placed at the end of the arg-list. More than // one `[]` list may be included and will be processed. Finally the // first identifier is taken to be the name of a macro and is invoked // with the processed arg-list. #[doc(hidden)] #[macro_export] macro_rules! indices { ( $cb:ident $( $args:tt )* ) => { $crate::$cb!( $($args)* ) }; ( [ ] $($rest:tt)* ) => { $crate::indices!($($rest)* []) }; ( [ $a:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 ]) }; ( [ $a:tt $b:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 ]) }; ( [ $a:tt $b:tt $c:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 7 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt $i:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 7 8 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt $i:tt $j:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 7 8 9 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt $i:tt $j:tt $k:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 7 8 9 10 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt $i:tt $j:tt $k:tt $l:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 7 8 9 10 11 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt $i:tt $j:tt $k:tt $l:tt $m:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 7 8 9 10 11 12 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt $i:tt $j:tt $k:tt $l:tt $m:tt $n:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt $i:tt $j:tt $k:tt $l:tt $m:tt $n:tt $o:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ]) }; ( [ $a:tt $b:tt $c:tt $d:tt $e:tt $f:tt $g:tt $h:tt $i:tt $j:tt $k:tt $l:tt $m:tt $n:tt $o:tt $p:tt ] $($rest:tt)* ) => { $crate::indices!($($rest)* [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ]) }; ( $($rest:tt)* ) => { std::compile_error!("Too many arguments in call"); } } // Used to insert empty function calls in test mode which let us test // coverage of the macro branches #[cfg(test)] #[doc(hidden)] #[macro_export] macro_rules! COVERAGE { ($name:ident) => { $crate::test::macro_coverage::$name(); }; } #[cfg(not(test))] #[doc(hidden)] #[macro_export] macro_rules! COVERAGE { ($name:ident) => {}; } /// Create a new actor and initialise it /// /// ```ignore /// let actor = actor!(core, Type::init(args...), notify); /// let actor = actor!(core, <path::Type>::init(args...), notify); /// ``` /// /// This may be used when creation and initialisation of the actor can /// be done together. Otherwise see [`actor_new!`]. The actor is /// created and then the given initialisation function is called /// asynchronously. The `notify` argument is a `Ret<StopCause>` /// instance to call if the actor is terminated. An [`ActorOwn`] /// reference is returned. /// /// Implemented using [`ActorOwn::new`]. /// /// [`ActorOwn::new`]: struct.ActorOwn.html#method.new /// [`ActorOwn`]: struct.ActorOwn.html /// [`actor_new!`]: macro.actor_new.html #[macro_export] macro_rules! actor { ($core:expr, $type:ident :: $init:ident($($x:expr),* $(,)? ), $notify:expr) => {{ $crate::COVERAGE!(actor_0); let notify = $notify; let core = $core.access_core(); let actor = $crate::ActorOwn::<$type>::new(core, notify); $crate::call!([actor], <$type>::$init($($x),*)); actor }}; ($core:expr, <$type:ty> :: $init:ident($($x:expr),* $(,)? ), $notify:expr) => {{ $crate::COVERAGE!(actor_1); let notify = $notify; let core = $core.access_core(); let actor = $crate::ActorOwn::<$type>::new(core, notify); $crate::call!([actor], <$type>::$init($($x),*)); actor }}; } /// Create a new actor /// /// This may be used when creation and initialisation of the actor /// must be done separately, for example when two actors need to be /// initialised with [`Fwd`] instances pointing to each other. /// Otherwise see [`actor!`]. /// /// ```ignore /// let actor = actor_new!(core, Type, notify); /// call!([actor, core], Type::init(arg1, arg2...)); /// ``` /// /// An [`ActorOwn`] reference is returned. Implemented using /// [`ActorOwn::new`]. /// /// [`ActorOwn::new`]: struct.ActorOwn.html#method.new /// [`ActorOwn`]: struct.ActorOwn.html /// [`Fwd`]: struct.Fwd.html /// [`actor!`]: macro.actor.html #[macro_export] macro_rules! actor_new { ($core:expr, $type:ty, $notify:expr) => {{ $crate::COVERAGE!(actor_new); let notify = $notify; $crate::ActorOwn::<$type>::new($core, notify) // Expecting Cx, Core or Stakker ref }}; } /// Create a new actor that implements a trait and initialise it /// /// ```ignore /// let actor = actor_of_trait!(core, BoxedTrait, Type::init(args...), notify); /// let actor = actor_of_trait!(core, BoxedTrait, <path::Type>::init(args...), notify); /// ``` /// /// This allows treating a set of actors that all implement a trait /// equally in the calling code. The actors have to be defined /// slightly differently to make this work. Here's a short example: /// /// ``` /// # use stakker::*; /// # use std::time::Instant; /// // Trait definition /// type Animal = Box<dyn AnimalTrait>; /// trait AnimalTrait { /// fn sound(&mut self, cx: CX![Animal]); /// } /// /// struct Cat; /// impl Cat { /// fn init(_: CX![Animal]) -> Option<Animal> { /// Some(Box::new(Self)) /// } /// } /// impl AnimalTrait for Cat { /// fn sound(&mut self, _: CX![Animal]) { /// println!("Miaow"); /// } /// } /// /// struct Dog; /// impl Dog { /// fn init(_: CX![Animal]) -> Option<Animal> { /// Some(Box::new(Self)) /// } /// } /// impl AnimalTrait for Dog { /// fn sound(&mut self, _: CX![Animal]) { /// println!("Woof"); /// } /// } /// /// let mut stakker = Stakker::new(Instant::now()); /// let s = &mut stakker; /// /// // This variable can hold any kind of animal /// let mut animal: ActorOwn<Animal>; /// animal = actor_of_trait!(s, Animal, Cat::init(), ret_nop!()); /// call!([animal], sound()); /// animal = actor_of_trait!(s, Animal, Dog::init(), ret_nop!()); /// call!([animal], sound()); /// /// // To separate creation and initialisation, do it this way: /// animal = actor_new!(s, Animal, ret_nop!()); /// call!([animal], Cat::init()); /// call!([animal], sound()); /// /// s.run(Instant::now(), false); /// ``` /// /// Implemented using [`ActorOwn::new`]. /// /// [`ActorOwn::new`]: struct.ActorOwn.html#method.new #[macro_export] macro_rules! actor_of_trait { ($core:expr, $trait:ident, $type:ident :: $init:ident($($x:expr),* $(,)? ), $notify:expr) => {{ $crate::COVERAGE!(actor_2); let notify = $notify; let core = $core.access_core(); let actor = $crate::ActorOwn::<$trait>::new(core, notify); $crate::call!([actor], <$type>::$init($($x),*)); actor }}; ($core:expr, $trait:ident, <$type:ty> :: $init:ident($($x:expr),* $(,)? ), $notify:expr) => {{ $crate::COVERAGE!(actor_3); let notify = $notify; let core = $core.access_core(); let actor = $crate::ActorOwn::<$trait>::new(core, notify); $crate::call!([actor], <$type>::$init($($x),*)); actor }}; } // Common code for `call!` etc #[doc(hidden)] #[macro_export] macro_rules! generic_call { // Closures ($handler:ident $hargs:tt $access:ident; [$cx:expr], |$this:ident, $cxid:ident| $body:expr) => {{ $crate::COVERAGE!(generic_call_0); let cb = move |$this: &mut Self, $cxid: &mut $crate::Cx<'_, Self>| $body; let cx: &mut $crate::Cx<'_, Self> = $cx; // Expecting Cx<Self> ref let this = cx.this().clone(); let core = $cx.access_core(); $crate::$handler!($hargs core; move |s| this.apply(s, cb)); }}; ($handler:ident $hargs:tt $access:ident; [$core:expr], |$stakker:ident| $body:expr) => {{ $crate::COVERAGE!(generic_call_1); let core = $core.access_core(); // Expecting Core, Cx or Stakker ref let cb = move |$stakker : &mut $crate::Stakker| $body; $crate::$handler!($hargs core; cb); }}; ($handler:ident $hargs:tt $access:ident; [$cx:expr], |_ $($x:tt)*) => {{ std::compile_error!("Do not use '_' as a closure argument in this macro; use a '_' prefix instead"); }}; ($handler:ident $hargs:tt $access:ident; [$cx:expr], |$t:ident, _ $($x:tt)*) => {{ std::compile_error!("Do not use '_' as a closure argument in this macro; use a '_' prefix instead"); }}; ($handler:ident $hargs:tt $access:ident; [$cx:expr], move | $($x:tt)*) => {{ std::compile_error!("Do not add `move` to closures as they get an implicit `move` anyway"); }}; // All remaining [actor] turned to [actor, actor] ($handler:ident $hargs:tt $access:ident; [$actor_or_cx:expr], $($x:tt)+) => {{ // Can't do `let` for actor_or_cx here because that would move it and drop it $crate::generic_call!($handler $hargs $access; [$actor_or_cx, $actor_or_cx], $($x)+) }}; ($handler:ident $hargs:tt $access:ident; [$actor:expr, $core:expr], $method:ident ( $($x:expr),* $(,)? )) => {{ $crate::COVERAGE!(generic_call_2); let actor = $actor.access_actor().clone(); // Expecting Actor or Cx ref let _args = ( $($x,)* ); // This must be before access borrow let access = $core.$access(); $crate::indices!([$(($x))*] generic_call_ready $handler $hargs access; actor _args $method) }}; ($handler:ident $hargs:tt $access:ident; [$actor:expr, $core:expr], $type:ident :: $method:ident ( $($x:expr),* $(,)? )) => {{ $crate::COVERAGE!(generic_call_3); let actor = $actor.access_actor().clone(); // Expecting Actor or Cx ref let _args = ( $($x,)* ); // This must be before access borrow let access = $core.$access(); $crate::indices!([$(($x))*] generic_call_prep $handler $hargs access; actor _args <$type> $method) }}; ($handler:ident $hargs:tt $access:ident; [$actor:expr, $core:expr], < $type:ty > :: $method:ident ( $($x:expr),* $(,)? )) => {{ $crate::COVERAGE!(generic_call_4); let actor = $actor.access_actor().clone(); // Expecting Actor or Cx ref let _args = ( $($x,)* ); // This must be before access borrow let access = $core.$access(); $crate::indices!([$(($x))*] generic_call_prep $handler $hargs access; actor _args <$type> $method) }}; } #[doc(hidden)] #[macro_export] macro_rules! generic_call_ready { ($handler:ident $hargs:tt $core:ident; $actor:ident $args:ident $method:ident [$($xi:tt)*]) => { $crate::$handler!($hargs $core; move |s| $actor.apply(s, move |o, c| o.$method(c $(, $args.$xi)*))) } } #[doc(hidden)] #[macro_export] macro_rules! generic_call_prep { ($handler:ident $hargs:tt $core:ident; $actor:ident $args:ident <$atyp:ty> $method:ident [$($xi:tt)*]) => { $crate::$handler!($hargs $core; move |s| $actor.apply_prep(s, move |c| <$atyp>::$method(c $(, $args.$xi)*))) } } /// Queue an actor call or inline code for execution soon /// /// The call is deferred to the main defer queue, which will execute /// as soon as possible. /// /// Note that in the examples below, in general there can be any /// number of arguments, including zero. The number of arguments /// depends on the signature of the called method. All of these /// values may be full Rust expressions, which are evaluated at the /// call-site before queuing the call. The exceptions are method /// names, types/paths and argument names for closures which may be /// any valid identifier. /// /// Note that the part in square brackets gives the context of the /// call. For calls to the same actor, this is normally just `cx`. /// For calls to another actor, normally the actor and a [`Core`] /// reference should be given (e.g. `cx`), but if the [`Core`] /// reference is omitted, the actor's built-in [`Deferrer`] is used /// instead. There is no difference in functionality (apart from /// borrowing issues), just that [`Core`] is more likely to be in /// cache. /// /// ```ignore /// // Call a method in this actor or in another actor /// call!([cx], method(arg1, arg2...)); /// call!([actorxx, core], method(arg1, arg2...)); /// /// // Call a method whilst the actor is in the 'Prep' state, before it /// // has a `Self` instance. `Type` here in the first line may be `Self`. /// call!([cx], Type::method(arg1, arg2...)); /// call!([cx], <path::Type>::method(arg1, arg2...)); /// call!([actoryy, core], Type::method(arg1, arg2...)); /// call!([actorzz, core], <path::Type>::method(arg1, arg2...)); /// /// // Use the actor's built-in Deferrer; doesn't require Core /// call!([actorxx], method(arg1, arg2...)); /// call!([actoryy], Type::method(arg1, arg2...)); /// call!([actorzz], <path::Type>::method(arg1, arg2...)); /// /// // Defer a call to inline code. Closure is always treated as a `move` closure /// call!([cx], |this, cx| ...code...); // Inline code which refers to this actor /// call!([core], |stakker| ...code...); // Generic inline code (`&mut Stakker` arg) /// ``` /// /// Implemented using [`Core::defer`], [`Actor::defer`], /// [`Actor::apply`] and [`Actor::apply_prep`]. /// /// [`Actor::apply_prep`]: struct.Actor.html#method.apply_prep /// [`Actor::apply`]: struct.Actor.html#method.apply /// [`Actor::defer`]: struct.Actor.html#method.defer /// [`Core::defer`]: struct.Core.html#method.defer /// [`Core`]: struct.Core.html /// [`Deferrer`]: struct.Deferrer.html #[macro_export] macro_rules! call { ( $($x:tt)+ ) => {{ $crate::COVERAGE!(call_0); $crate::generic_call!(call_aux () access_deferrer; $($x)+); }}; } #[doc(hidden)] #[macro_export] macro_rules! call_aux { (() $defer:ident; $cb:expr) => {{ $crate::COVERAGE!(call_1); $defer.defer($cb); }}; } /// Lazily perform an actor call or inline code /// /// The call syntax accepted is identical to the [`call!`] macro. /// However the plain `[actor], ...` form is not accepted because a /// [`Core`] reference is always needed. This queues calls to the /// lazy queue which is run only after the normal defer queue has been /// completely exhausted. This can be used to run something at the /// end of this batch of processing, for example to flush buffers /// after accumulating data. /// /// Implemented using [`Core::lazy`], [`Actor::apply`] and /// [`Actor::apply_prep`]. /// /// [`Actor::apply_prep`]: struct.Actor.html#method.apply_prep /// [`Actor::apply`]: struct.Actor.html#method.apply /// [`Core::lazy`]: struct.Core.html#method.lazy /// [`Core`]: struct.Core.html /// [`call!`]: macro.call.html #[macro_export] macro_rules! lazy { ( $($x:tt)+ ) => {{ $crate::COVERAGE!(lazy_0); $crate::generic_call!(lazy_aux () access_core; $($x)+); // Error? Try [actor, core] form }}; } #[doc(hidden)] #[macro_export] macro_rules! lazy_aux { (() $core:ident; $cb:expr) => {{ $crate::COVERAGE!(lazy_1); $core.lazy($cb); }}; } /// Perform an actor call or inline code when the process becomes idle /// /// The call syntax accepted is identical to the [`call!`] macro. /// However the `[actor], ...` form is not accepted because a [`Core`] /// reference is always needed. This queues calls to the idle queue /// which is run only when there is nothing left to run in the normal /// and lazy queues, and there is no I/O pending. This can be used to /// create backpressure in the case of processing overload, i.e. fetch /// more data only when all current data has been fully processed. /// /// Implemented using [`Core::idle`], [`Actor::apply`] and /// [`Actor::apply_prep`]. /// /// [`Actor::apply_prep`]: struct.Actor.html#method.apply_prep /// [`Actor::apply`]: struct.Actor.html#method.apply /// [`Core::idle`]: struct.Core.html#method.idle /// [`Core`]: struct.Core.html /// [`call!`]: macro.call.html #[macro_export] macro_rules! idle { ( $($x:tt)+ ) => {{ $crate::COVERAGE!(idle_0); $crate::generic_call!(idle_aux () access_core; $($x)+); // Error? Try [actor, core] form }}; } #[doc(hidden)] #[macro_export] macro_rules! idle_aux { (() $core:ident; $cb:expr) => {{ $crate::COVERAGE!(idle_1); $core.idle($cb); }}; } /// After a delay, perform an actor call or inline code /// /// The syntax of the calls is identical to [`call!`], but with a /// `Duration` argument first. However the `[actor], ...` form is not /// accepted because a [`Core`] reference is always needed. Returns a /// [`FixedTimerKey`] which can be used to delete the timer if /// necessary. See also [`at!`]. /// /// ```ignore /// after!(dur, ...args-as-for-call-macro...); /// ``` /// /// Implemented using [`Core::after`], [`Actor::apply`] and /// [`Actor::apply_prep`]. /// /// [`Actor::apply_prep`]: struct.Actor.html#method.apply_prep /// [`Actor::apply`]: struct.Actor.html#method.apply /// [`Core::after`]: struct.Core.html#method.after /// [`Core`]: struct.Core.html /// [`FixedTimerKey`]: struct.FixedTimerKey.html /// [`at!`]: macro.at.html /// [`call!`]: macro.call.html #[macro_export] macro_rules! after { ( $dur:expr, $($x:tt)+ ) => {{ $crate::COVERAGE!(after_0); let dur: Duration = $dur; $crate::generic_call!(after_aux (dur) access_core; $($x)+) // Error? Try [actor, core] form }}; } #[doc(hidden)] #[macro_export] macro_rules! after_aux { (($dur:ident) $core:ident; $cb:expr) => {{ $crate::COVERAGE!(after_1); $core.after($dur, $cb); }}; } /// At the given `Instant`, perform an actor call or inline code /// /// The syntax of the calls is identical to [`call!`], but with an /// `Instant` argument first. However the `[actor], ...` form is not /// accepted because a [`Core`] reference is always needed. Returns a /// [`FixedTimerKey`] which can be used to delete the timer if /// necessary. See also [`after!`]. /// /// ```ignore /// at!(instant, ...args-as-for-call-macro...); /// ``` /// /// Implemented using [`Core::timer_add`], [`Actor::apply`] and /// [`Actor::apply_prep`]. /// /// [`Actor::apply_prep`]: struct.Actor.html#method.apply_prep /// [`Actor::apply`]: struct.Actor.html#method.apply /// [`Core::timer_add`]: struct.Core.html#method.timer_add /// [`Core`]: struct.Core.html /// [`FixedTimerKey`]: struct.FixedTimerKey.html /// [`after!`]: macro.after.html /// [`call!`]: macro.call.html #[macro_export] macro_rules! at { ( $inst:expr, $($x:tt)+ ) => {{ $crate::COVERAGE!(at_0); let inst: std::time::Instant = $inst; $crate::generic_call!(at_aux (inst) access_core; $($x)+) // Error? Try [actor, core] form }}; } #[doc(hidden)] #[macro_export] macro_rules! at_aux { (($inst:ident) $core:ident; $cb:expr) => {{ $crate::COVERAGE!(at_1); $core.timer_add($inst, $cb) }}; } /// Create or update a "Max" timer /// /// A "Max" timer expires at the latest (greatest) expiry time /// provided. Modifies a [`MaxTimerKey`] variable or structure member /// provided by the caller, which should be initialised with /// `Default::default()` (which is an invalid timer key). If the /// timer key currently in the variable is invalid or expired, then a /// new timer is created using the call-args following, and the key /// stored in the variable. Otherwise the timer expiry time is /// updated with the maximum of the current and provided expiry times, /// and the call-args are ignored. The timer may be deleted using /// [`Core::timer_max_del`]. /// /// The syntax of the calls is identical to [`call!`], but with a /// variable reference and `Instant` argument first. However the /// plain `[actor], ...` form is not accepted because a [`Core`] /// reference is always needed. /// /// ```ignore /// let mut var = Default::default(); /// ::: /// timer_max!(&mut var, instant, ...args-as-for-call-macro...); /// ``` /// /// Implemented using [`Core::timer_max_upd`], /// [`Core::timer_max_add`], [`Actor::apply`] and /// [`Actor::apply_prep`]. /// /// [`Actor::apply_prep`]: struct.Actor.html#method.apply_prep /// [`Actor::apply`]: struct.Actor.html#method.apply /// [`Core::timer_max_add`]: struct.Core.html#method.timer_max_add /// [`Core::timer_max_del`]: struct.Core.html#method.timer_max_del /// [`Core::timer_max_upd`]: struct.Core.html#method.timer_max_upd /// [`Core`]: struct.Core.html /// [`MaxTimerKey`]: struct.MaxTimerKey.html /// [`call!`]: macro.call.html #[macro_export] macro_rules! timer_max { ( $var:expr, $inst:expr, $($x:tt)+ ) => {{ $crate::COVERAGE!(timer_max_0); let var: &mut $crate::MaxTimerKey = $var; let inst: std::time::Instant = $inst; $crate::generic_call!(timer_max_aux (var, inst) access_core; $($x)+) // Error? Try [actor, core] form }}; } #[doc(hidden)] #[macro_export] macro_rules! timer_max_aux { (($var:ident, $inst:ident) $core:ident; $cb:expr) => {{ $crate::COVERAGE!(timer_max_1); if !$core.timer_max_upd(*$var, $inst) { *$var = $core.timer_max_add($inst, $cb); } }}; } /// Create or update a "Min" timer /// /// A "Min" timer expires at the smallest (earliest) expiry time /// provided. Modifies a [`MinTimerKey`] variable or structure member /// provided by the caller, which should be initialised with /// `Default::default()` (which is an invalid timer key). If the /// timer key currently in the variable is invalid or expired, then a /// new timer is created using the call-args following, and the key /// stored in the variable. Otherwise the timer expiry time is /// updated with the minimum of the current and provided expiry times, /// and the call-args are ignored. The timer may be deleted using /// [`Core::timer_min_del`]. /// /// The syntax of the calls is identical to [`call!`], but with a /// variable reference and `Instant` argument first. However the /// plain `[actor], ...` form is not accepted because a [`Core`] /// reference is always needed. /// /// ```ignore /// let mut var = Default::default(); /// ::: /// timer_min!(&mut var, instant, ...args-as-for-call-macro...); /// ``` /// /// Implemented using [`Core::timer_min_upd`], /// [`Core::timer_min_add`], [`Actor::apply`] and /// [`Actor::apply_prep`]. /// /// [`Actor::apply_prep`]: struct.Actor.html#method.apply_prep /// [`Actor::apply`]: struct.Actor.html#method.apply /// [`Core::timer_min_add`]: struct.Core.html#method.timer_min_add /// [`Core::timer_min_del`]: struct.Core.html#method.timer_min_del /// [`Core::timer_min_upd`]: struct.Core.html#method.timer_min_upd /// [`Core`]: struct.Core.html /// [`MinTimerKey`]: struct.MinTimerKey.html /// [`call!`]: macro.call.html #[macro_export] macro_rules! timer_min { ( $var:expr, $inst:expr, $($x:tt)+ ) => {{ $crate::COVERAGE!(timer_min_0); let var: &mut $crate::MinTimerKey = $var; let inst: std::time::Instant = $inst; $crate::generic_call!(timer_min_aux (var, inst) access_core; $($x)+) // Error? Try [actor, core] form }}; } #[doc(hidden)] #[macro_export] macro_rules! timer_min_aux { (($var:ident, $inst:ident) $core:ident; $cb:expr) => {{ $crate::COVERAGE!(timer_min_1); if !$core.timer_min_upd(*$var, $inst) { *$var = $core.timer_min_add($inst, $cb); } }}; } /// Forward data via a [`Fwd`] instance /// /// ```ignore /// fwd!([fwd2zz], arg1, arg2...); /// ``` /// /// There may be zero or more arguments, and they must match the /// message type. Implemented using [`Fwd::fwd`] /// /// [`Fwd::fwd`]: struct.Fwd.html#method.fwd /// [`Fwd`]: struct.Fwd.html #[macro_export] macro_rules! fwd { // A single argument isn't passed as a tuple, so has special // handling. ([ $fwd:expr ], $arg:expr) => {{ $crate::COVERAGE!(fwd_0); $fwd.fwd($arg); }}; ([ $fwd:expr ] $(, $arg:expr)*) => {{ $crate::COVERAGE!(fwd_1); $fwd.fwd(( $($arg ,)* )); }}; } /// Return data via a [`Ret`] instance /// /// ```ignore /// ret!([ret2zz], arg1, arg2...); /// ``` /// /// This consumes the [`Ret`] instance, which means that it cannot be /// used again. There may be zero or more arguments, and they must /// match the message type. Implemented using [`Ret::ret`]. /// /// [`Ret::ret`]: struct.Ret.html#method.ret /// [`Ret`]: struct.Ret.html #[macro_export] macro_rules! ret { // A single argument isn't passed as a tuple, so has special // handling. ([ $ret:expr ], $arg:expr) => {{ $crate::COVERAGE!(ret_0); $ret.ret($arg); }}; ([ $ret:expr ] $(, $arg:expr)*) => {{ $crate::COVERAGE!(ret_1); $ret.ret(( $($arg ,)* )); }}; } // Common code for `fwd_*!` #[doc(hidden)] #[macro_export] macro_rules! generic_fwd { // Calling actors ($handler:ident; [$actor:expr], $method:ident ( $($x:expr),* ) as ( $($t:ty),* )) => {{ $crate::COVERAGE!(generic_fwd_0); let actor = $actor.access_actor().clone(); // Expecting Actor or Cx ref let _args = ( $($x,)* ); $crate::indices!([$(($x))*] [$(($t))*] generic_fwd_ready $handler actor _args ($($t,)*) $method) }}; ($handler:ident; [$actor:expr], $type:ident::$method:ident ( $($x:expr),* ) as ( $($t:ty),* )) => {{ $crate::COVERAGE!(generic_fwd_1); let actor = $actor.access_actor().clone(); // Expecting Actor or Cx ref let _args = ( $($x,)* ); $crate::indices!([$(($x))*] [$(($t))*] generic_fwd_prep $handler actor _args ($($t,)*) <$type> $method) }}; ($handler:ident; [$actor:expr], <$type:ty>::$method:ident ( $($x:expr),* ) as ( $($t:ty),* )) => {{ $crate::COVERAGE!(generic_fwd_2); let actor = $actor.access_actor().clone(); // Expecting Actor or Cx ref let _args = ( $($x,)* ); $crate::indices!([$(($x))*] [$(($t))*] generic_fwd_prep $handler actor _args ($($t,)*) <$type> $method) }}; // Calling closures ($handler:ident; [$cx:expr], |$this:ident, $cxid:ident, $arg:ident : $t:ty| $($body:tt)+) => {{ $crate::COVERAGE!(generic_fwd_3); let cx: &mut $crate::Cx<'_, _> = $cx; // Expecting Cx ref let actor = cx.this().clone(); $crate::$handler!(ready actor; move |$this, $cxid, $arg: $t| $($body)*; std::compile_error!("`ret_to!` with a closure requires a single Option argument")) }}; ($handler:ident; [$cx:expr], |$this:ident, $cxid:ident $(, $arg:ident : $t:ty)*| $($body:tt)+) => {{ $crate::COVERAGE!(generic_fwd_4); let cx: &mut $crate::Cx<'_, _> = $cx; // Expecting Cx ref let actor = cx.this().clone(); $crate::$handler!(ready actor; move |$this, $cxid, ($($arg),*): ($($t),*)| $($body)*; std::compile_error!("`ret_to!` with a closure requires a single Option argument")) }}; ($handler:ident; [$cx:expr], |_ $($x:tt)+) => {{ std::compile_error!("Do not use '_' as a closure argument in this macro; use a '_' prefix instead"); }}; ($handler:ident; [$cx:expr], |$t:ident, _ $($x:tt)+) => {{ std::compile_error!("Do not use '_' as a closure argument in this macro; use a '_' prefix instead"); }}; ($handler:ident; [$cx:expr], move | $($x:tt)*) => {{ std::compile_error!("Do not add `move` to closures as they get an implicit `move` anyway"); }}; } #[doc(hidden)] #[macro_export] macro_rules! generic_fwd_ready { ($handler:ident $actor:ident $args:ident ($t:ty,) $method:ident [$($xi:tt)*] [$($ti:tt)*]) => {{ $crate::COVERAGE!(generic_fwd_5); $crate::$handler!(ready $actor; move |a, cx, m: $t| a.$method(cx $(, $args.$xi)* , m); move |a, cx, m: Option<$t>| a.$method(cx $(, $args.$xi)* , m)) }}; ($handler:ident $actor:ident $args:ident ($($t:ty,)*) $method:ident [$($xi:tt)*] [$($ti:tt)*]) => {{ $crate::COVERAGE!(generic_fwd_6); $crate::$handler!(ready $actor; move |a, cx, _m: ($($t,)*)| a.$method(cx $(, $args.$xi)* $(, _m.$ti)*); move |a, cx, m: Option<($($t,)*)>| a.$method(cx $(, $args.$xi)*, m)) }}; } #[doc(hidden)] #[macro_export] macro_rules! generic_fwd_prep { ($handler:ident $actor:ident $args:ident ($t:ty,) <$atyp:ty> $method:ident [$($xi:tt)*] [$($ti:tt)*]) => {{ $crate::COVERAGE!(generic_fwd_7); $crate::$handler!(prep $actor; move |cx, m: $t| <$atyp>::$method(cx $(, $args.$xi)* , m); move |cx, m: Option<$t>| <$atyp>::$method(cx $(, $args.$xi)* , m)) }}; ($handler:ident $actor:ident $args:ident ($($t:ty,)*) <$atyp:ty> $method:ident [$($xi:tt)*] [$($ti:tt)*]) => {{ $crate::COVERAGE!(generic_fwd_8); $crate::$handler!(prep $actor; move |cx, _m: ($($t,)*)| <$atyp>::$method(cx $(, $args.$xi)* $(, _m.$ti)*); move |cx, m: Option<($($t,)*)>| <$atyp>::$method(cx $(, $args.$xi)*, m)) }}; } /// Create a [`Fwd`] instance for actor calls /// /// The syntax is similar to that used for [`call!`], except that the /// call is followed by `as` and a tuple of argument types (which may /// be empty). These types are the types of the arguments accepted by /// the [`Fwd`] instance when it is called, and which are appended to /// the argument list of the method call. So each call to a method is /// made up of first the fixed arguments (if any) provided at the time /// the [`Fwd`] instance was created, followed by the variable arguments /// (if any) provided when the [`Fwd`] instance was called. This must /// match the signature of the method itself. /// /// `as` is used here because this is a standard Rust token that can /// introduce a tuple and so `rustfmt` can format the code, although /// something like `with` would make more sense. /// /// ```ignore /// // Forward to a method in this actor or in another actor /// fwd_to!([cx], method(arg1, arg2...) as (type1, type2...)); /// fwd_to!([actorxx], method(arg1, arg2...) as (type1, type2...)); /// /// // Forward to a method whilst in the 'Prep' state /// fwd_to!([cx], Self::method(arg1, arg2...) as (type1, type2...)); /// fwd_to!([cx], <path::Type>::method(arg1, arg2...) as (type1, type2...)); /// fwd_to!([actoryy], Type::method(arg1, arg2...) as (type1, type2...)); /// fwd_to!([actorzz], <path::Type>::method(arg1, arg2...) as (type1, type2...)); /// /// // Forward a call to inline code which refers to this actor. In /// // this case the `Fwd` argument list is extracted from the closure /// // argument list and no `as` section is required. Closure is /// // always treated as a `move` closure. /// fwd_to!([cx], |this, cx, arg1: type1, arg2: type2...| ...code...); /// ``` /// /// Implemented using [`Fwd::to_actor`] or [`Fwd::to_actor_prep`]. /// /// [`Fwd::to_actor_prep`]: struct.Fwd.html#method.to_actor_prep /// [`Fwd::to_actor`]: struct.Fwd.html#method.to_actor /// [`Fwd`]: struct.Fwd.html /// [`call!`]: macro.call.html #[macro_export] macro_rules! fwd_to { ($($x:tt)*) => {{ $crate::COVERAGE!(fwd_to_0); $crate::generic_fwd!(fwd_to_aux; $($x)*) }} } #[doc(hidden)] #[macro_export] macro_rules! fwd_to_aux { (ready $actor:ident; $cb:expr; $cb2:expr) => {{ $crate::COVERAGE!(fwd_to_1); $crate::Fwd::to_actor($actor, $cb) }}; (prep $actor:ident; $cb:expr; $cb2:expr) => {{ $crate::COVERAGE!(fwd_to_2); $crate::Fwd::to_actor_prep($actor, $cb) }}; } /// Create a [`Fwd`] instance which panics when called /// /// ```ignore /// fwd_panic!(panic_msg) /// ``` /// /// Argument will typically be a `String` or `&str`. Note that this /// will receive and ignore any message type. Implemented using /// [`Fwd::panic`]. /// /// [`Fwd::panic`]: struct.Fwd.html#method.panic /// [`Fwd`]: struct.Fwd.html #[macro_export] macro_rules! fwd_panic { ($arg:expr) => {{ $crate::COVERAGE!(fwd_panic_0); $crate::Fwd::panic($arg) }}; } /// Create a [`Fwd`] instance which performs an arbitrary action /// /// The action is performed immediately at the point in the code where /// the message is forwarded. So this is executed synchronously /// rather than asynchronously. However it will normally be used to /// defer a call, since it doesn't have access to any actor, just the /// message data. If it doesn't have an actor reference available, it /// will probably need to capture a [`Deferrer`] in the closure. /// /// ```ignore /// fwd_do!(|msg| ...); /// ``` /// /// Implemented using [`Fwd::new`]. /// /// [`Deferrer`]: struct.Deferrer.html /// [`Fwd::new`]: struct.Fwd.html#method.new /// [`Fwd`]: struct.Fwd.html #[macro_export] macro_rules! fwd_do { ($cb:expr) => {{ $crate::COVERAGE!(fwd_do_0); $crate::Fwd::new($cb) }}; } /// Create a [`Fwd`] instance which does nothing at all /// /// ```ignore /// fwd_nop!(); /// ``` /// /// NOP means "no operation". Implemented using [`Fwd::new`]. /// /// [`Fwd::new`]: struct.Fwd.html#method.new /// [`Fwd`]: struct.Fwd.html #[macro_export] macro_rules! fwd_nop { () => {{ $crate::COVERAGE!(fwd_nop_0); $crate::Fwd::new(|_| {}) }}; } /// Create a [`Ret`] instance for actor calls /// /// This is guaranteed to be called **exactly once**, even if dropped. /// The message is passed as `Some(msg)` if called normally, or as /// `None` if the [`Ret`] instance was dropped (e.g. if it couldn't be /// delivered somewhere). The underlying closure is a `FnOnce`, so /// non-Copy types can be passed. The syntax is the same as for /// [`fwd_to!`], and the message types are specified as normal. /// However the message is received in a single argument on the /// receiving method, either `Option<type>` for a single type, or else /// `Option<(type1, type2...)>`. /// /// ```ignore /// ret_to!(...arguments-as-for-fwd_to-macro...); /// ``` /// /// The closure form must use a single `Option` as above as the /// argument type, containing all the types passed from the [`Ret`]. /// /// Implemented using [`Ret::to_actor`] or [`Ret::to_actor_prep`]. /// /// [`Ret::to_actor_prep`]: struct.Ret.html#method.to_actor_prep /// [`Ret::to_actor`]: struct.Ret.html#method.to_actor /// [`Ret`]: struct.Ret.html /// [`fwd_to!`]: macro.fwd_to.html #[macro_export] macro_rules! ret_to { ([$cx:expr], |$this:ident, $cxid:ident, $arg:ident : Option<$t:ty>| $($body:tt)+) => {{ $crate::COVERAGE!(ret_to_0); let cx: &mut $crate::Cx<'_, _> = $cx; // Expecting Cx ref let actor = cx.this().clone(); $crate::Ret::to_actor(actor, move |$this, $cxid, $arg: Option<$t>| $($body)*) }}; ([$cx:expr], move | $($x:tt)*) => {{ std::compile_error!("Do not add `move` to closures as they get an implicit `move` anyway"); }}; // Closures not matching above will get caught below, giving a // compilation error ($($x:tt)*) => {{ $crate::COVERAGE!(ret_to_1); $crate::generic_fwd!(ret_to_aux; $($x)*) }} } #[doc(hidden)] #[macro_export] macro_rules! ret_to_aux { (ready $actor:ident; $cb:expr; $cb2:expr) => {{ $crate::COVERAGE!(ret_to_2); $crate::Ret::to_actor($actor, $cb2) }}; (prep $actor:ident; $cb:expr; $cb2:expr) => {{ $crate::COVERAGE!(ret_to_3); $crate::Ret::to_actor_prep($actor, $cb2) }}; } /// Create a [`Ret`] instance for actor calls, ignoring drops /// /// This is guaranteed to be called **at most once**. Dropping the /// [`Ret`] instance is ignored, unlike [`ret_to!`], so the message is /// passed through without an `Option` wrapper, just like [`fwd_to!`]. /// The underlying closure is a `FnOnce`, so non-Copy types can be /// passed. The syntax is the same as for [`fwd_to!`], and messages /// are received in exactly the same way in the target actor method. /// /// ```ignore /// ret_some_to!(...arguments-as-for-fwd_to-macro...); /// ``` /// /// Implemented using [`Ret::some_to_actor`] or [`Ret::some_to_actor_prep`]. /// /// [`Ret::some_to_actor_prep`]: struct.Ret.html#method.some_to_actor_prep /// [`Ret::some_to_actor`]: struct.Ret.html#method.some_to_actor /// [`Ret`]: struct.Ret.html /// [`fwd_to!`]: macro.fwd_to.html /// [`ret_to!`]: macro.ret_to.html #[macro_export] macro_rules! ret_some_to { ($($x:tt)*) => {{ $crate::COVERAGE!(ret_some_to_0); $crate::generic_fwd!(ret_some_to_aux; $($x)*) }} } #[doc(hidden)] #[macro_export] macro_rules! ret_some_to_aux { (ready $actor:ident; $cb:expr; $cb2:expr) => {{ $crate::COVERAGE!(ret_some_to_1); $crate::Ret::some_to_actor($actor, $cb) }}; (prep $actor:ident; $cb:expr; $cb2:expr) => {{ $crate::COVERAGE!(ret_some_to_2); $crate::Ret::some_to_actor_prep($actor, $cb) }}; } /// Create a [`Ret`] instance which performs an arbitrary action /// /// The action is performed immediately at the point in the code where /// the message is returned. So this is executed synchronously rather /// than asynchronously. However it will normally be used to defer a /// call, since it doesn't have access to any actor, just the message /// data. If it doesn't have an actor reference available, it will /// probably need to capture a [`Deferrer`] in the closure. /// /// ```ignore /// ret_do!(|msg| ...); /// ``` /// /// Implemented using [`Ret::new`]. /// /// [`Deferrer`]: struct.Deferrer.html /// [`Ret::new`]: struct.Ret.html#method.new /// [`Ret`]: struct.Ret.html #[macro_export] macro_rules! ret_do { ($cb:expr) => {{ $crate::COVERAGE!(ret_do_0); $crate::Ret::new($cb) }}; } /// Create a [`Ret`] instance which performs an arbitrary action, ignoring drops /// /// Like [`ret_some_to!`], this ignores the case of the [`Ret`] instance /// being dropped, so the message is received without the wrapping /// `Option`. The action is performed immediately at the point in the /// code where the message is returned. So this is executed /// synchronously rather than asynchronously. However it will /// normally be used to defer a call, since it doesn't have access to /// any actor, just the message data. If it doesn't have an actor /// reference available, it will probably need to capture a /// [`Deferrer`] in the closure. /// /// ```ignore /// ret_some_do!(|msg| ...); /// ``` /// /// Implemented using [`Ret::new`]. /// /// [`Deferrer`]: struct.Deferrer.html /// [`Ret::new`]: struct.Ret.html#method.new /// [`Ret`]: struct.Ret.html /// [`ret_some_to!`]: macro.ret_some_to.html #[macro_export] macro_rules! ret_some_do { ($cb:expr) => {{ $crate::COVERAGE!(ret_some_do_0); $crate::Ret::new(move |m| { if let Some(m) = m { ($cb)(m); } }) }}; } /// Create a [`Ret`] instance which panics when called /// /// ```ignore /// ret_panic!(panic_msg) /// ``` /// /// Ignores the case where the [`Ret`] instance is dropped. Argument /// will typically be a `String` or `&str`. Note that this will /// receive and ignore any message type. Implemented using /// [`Ret::panic`]. /// /// [`Ret::panic`]: struct.Ret.html#method.panic /// [`Ret`]: struct.Ret.html #[macro_export] macro_rules! ret_panic { ($arg:expr) => {{ $crate::COVERAGE!(ret_panic_0); $crate::Ret::panic($arg) }}; } /// Create a [`Ret`] instance which does nothing at all /// /// ```ignore /// ret_nop!(); /// ``` /// /// NOP means "no operation". Implemented using [`Ret::new`]. /// /// [`Ret::new`]: struct.Ret.html#method.new /// [`Ret`]: struct.Ret.html #[macro_export] macro_rules! ret_nop { () => {{ $crate::COVERAGE!(ret_nop_0); $crate::Ret::new(|_| {}) }}; } /// Create a [`Ret`] instance which shuts down the event loop /// /// ```ignore /// ret_shutdown!(core); /// ``` /// /// This can be used as the notify handler on an actor to shut down /// the event loop once that actor terminates. The reason for the /// actor's failure is passed through, and can be recovered after loop /// termination using [`Core::shutdown_reason`]. See also /// [`Ret::new`] and [`Core::shutdown`]. /// /// [`Core::shutdown_reason`]: struct.Core.html#method.shutdown_reason /// [`Core::shutdown`]: struct.Core.html#method.shutdown /// [`Ret::new`]: struct.Ret.html#method.new /// [`Ret`]: struct.Ret.html #[macro_export] macro_rules! ret_shutdown { ($core:expr) => {{ $crate::COVERAGE!(ret_shutdown_0); let core = $core.access_core(); let deferrer = core.deferrer(); $crate::Ret::new(move |m| { if let Some(cause) = m { deferrer.defer(|s| s.shutdown(cause)); } else { deferrer.defer(|s| s.shutdown($crate::StopCause::Dropped)); } }) }}; }