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
//! Synchronous event system. //! //! # What is an event system # //! //! An event system is a set of signals connected to a bunch of objects. When a signal is emitted, //! the objects subscribing to said signal will have their handlers invoked to perform some useful //! processing. //! //! ## Synchronous? ## //! //! Revent is synchonous, meaning that calling `emit` will immediately call all subscribers. This //! also means that subscribers can return complex types with lifetimes referring to themselves. //! Event handlers can also emit further events synchronously. //! //! # Example # //! //! ``` //! use revent::{hub, node, Subscriber}; //! //! // Here is a simple trait for a signal. All signals are traits. //! trait A { //! fn function(&mut self); //! } //! //! // Create a new top-level hub, this contains all signals. //! hub! { //! X { //! signal_1: A, //! } //! } //! //! // Make `MyHandler` subscribe to `signal_1`. //! node! { //! X { //! signal_1: A, //! } => Node(MyHandler) { //! } //! } //! //! // Create the `MyHandler` struct. //! struct MyHandler; //! impl A for MyHandler { //! fn function(&mut self) { //! println!("Hello world"); //! } //! } //! //! // Describe how to build an instance of `MyHandler`. //! impl Subscriber for MyHandler { //! type Input = (); //! fn build(_node: Self::Node, _input: Self::Input) -> Self { //! Self //! } //! } //! //! // Create a new root hub instance. //! let mut x = X::new(); //! //! // Add an instance of `MyHandler`. //! let input = (); //! x.subscribe::<MyHandler>(input); //! //! // Emit an event on the `signal_1` channel. //! x.signal_1().emit(|subscriber| { //! subscriber.function(); //! }); //! ``` //! //! # Nested emitting # //! //! To allow for nested emitting we specify which signals we wish to be able to emit to in our //! internal node. //! //! ``` //! use revent::{hub, node, Subscriber}; //! //! trait A { //! fn function_a(&mut self); //! } //! //! trait B { //! fn function_b(&mut self); //! } //! //! hub! { //! X { //! signal_1: A, //! signal_2: B, //! } //! } //! //! node! { //! X { //! signal_1: A, //! } => Node(MyHandler) { //! signal_2: B, //! // Node holds `signal_2` and is able to emit into this. //! } //! } //! //! struct MyHandler { //! node: Node, //! } //! //! impl A for MyHandler { //! fn function_a(&mut self) { //! self.node.signal_2().emit(|subscriber| { //! subscriber.function_b(); //! }); //! } //! } //! //! // Describe how to build an instance of `MyHandler`. //! impl Subscriber for MyHandler { //! type Input = (); //! fn build(mut node: Self::Node, _input: Self::Input) -> Self { //! Self { node } //! } //! } //! ``` //! //! # Mutable borrowing # //! //! It's possible to put a single object in two or more [Signal]s. If one signal is able to emit //! into another signal then we may get a double-mutable borrow. //! //! Revent avoids the possibility of mutable borrows at emit-time by performing a graph cycle search //! every time a type subscribes. The following code panics at the subscribe stage giving us a //! useful error message about how the cycle is formed. //! //! The following prints `[AToBHandler]a -> [BToAHandler]b -> a`, meaning that `AToBHandler` //! listens to `a` and emits into `b` (which `BToAHandler` listens to), which then again emits into //! `a`, thus a cycle is formed that can cause a double mutable borrow. //! //! ```should_panic //! use revent::{hub, node, Subscriber}; //! //! pub trait A { //! fn a(&mut self); //! } //! //! pub trait B { //! fn b(&mut self); //! } //! //! hub! { //! X { //! a: A, //! b: B, //! } //! } //! //! node! { //! X { //! a: A, //! } => AToB(AToBHandler) { //! b: B, //! } //! } //! //! struct AToBHandler { //! node: AToB, //! } //! //! impl A for AToBHandler { //! fn a(&mut self) { //! self.node.b().emit(|x| { //! x.b(); //! }); //! } //! } //! //! impl Subscriber for AToBHandler { //! type Input = (); //! fn build(node: Self::Node, _: Self::Input) -> Self { //! Self { node } //! } //! } //! //! node! { //! X { //! b: B, //! } => BToA(BToAHandler) { //! a: A, //! } //! } //! //! struct BToAHandler { //! node: BToA, //! } //! //! impl B for BToAHandler { //! fn b(&mut self) { //! self.node.a().emit(|x| { //! x.a(); //! }); //! } //! } //! //! impl Subscriber for BToAHandler { //! type Input = (); //! fn build(node: Self::Node, _: Self::Input) -> Self { //! Self { node } //! } //! } //! //! let mut x = X::new(); //! x.subscribe::<AToBHandler>(()); //! x.subscribe::<BToAHandler>(()); //! ``` #![deny( missing_docs, trivial_casts, trivial_numeric_casts, unsafe_code, unused_import_braces, unused_qualifications )] #![feature(drain_filter)] mod mng; mod signal; mod traits; #[doc(hidden)] pub use mng::{Grapher, Manager}; pub use signal::Signal; pub use traits::{Nodified, Selfscriber, Subscriber}; /// Generate a top-level `hub`. /// /// A hub is struct where all signals are defined. It is the "root" object for downstream [node]s. /// /// The macro invocation /// ``` /// use revent::hub; /// /// trait X {} /// trait Y {} /// trait Z {} /// /// hub! { /// HubName { /// signal_name_1: X, /// signal_name_2: Y, /// signal_name_3: Z, /// } /// } /// ``` /// /// generates the code /// /// ```ignore /// struct HubName { ... } /// /// impl HubName { /// fn new() -> Self { ... } /// /// pub fn subscribe<T>(&mut self, input: T::Input) -> ::std::rc::Rc<::std::cell::RefCell<T>> /// where /// T: revent::Nodified + revent::Selfscriber<Self> + revent::Subscriber, /// T::Node: for<'a> From<&'a Self>, /// { ... } /// /// pub fn signal_name_1(&mut self) -> &mut revent::Signal<dyn X> { ... } /// pub fn signal_name_2(&mut self) -> &mut revent::Signal<dyn Y> { ... } /// pub fn signal_name_3(&mut self) -> &mut revent::Signal<dyn Z> { ... } /// } /// /// impl Default for HubName { /// fn default() -> Self { ... } /// } /// ``` /// /// Note that while the `subscribe` function returns a reference to the built node object, it /// should only be used for testing purposes. #[macro_export] macro_rules! hub { ($name:ident { $($channel:ident: $channel_type:path),*$(,)? }) => { $crate::node_internal! { hub $name { $($channel: $channel_type),* } } impl $name { /// Create a new hub instance. pub fn new() -> Self { let manager = ::std::rc::Rc::new(::std::cell::RefCell::new($crate::Manager::default())); Self { _private_revent_1_manager: manager.clone(), $($channel: $crate::Signal::new(stringify!($channel), manager.clone())),* } } } impl ::std::default::Default for $name { fn default() -> Self { Self::new() } } } } /// Generate an intermediate node in the signal chain. /// /// The macro invocation /// ```ignore /// use revent::node; /// /// node! { /// HubName { /// signal_name_1: X, /// signal_name_2: Y, /// } => MyNode(Handler) { /// signal_name_3: Z, /// } /// } /// ``` /// /// generates the code /// /// ```ignore /// struct MyNode { ... } /// /// impl MyNode { /// pub fn subscribe<T>(&mut self, input: T::Input) -> ::std::rc::Rc<::std::cell::RefCell<T>> /// where /// T: revent::Nodified + revent::Selfscriber<Self> + revent::Subscriber, /// T::Node: for<'a> From<&'a Self>, /// { ... } /// /// pub fn signal_name_3(&mut self) -> &mut revent::Signal<dyn Z> { ... } /// } /// /// impl From<&'_ HubName> for MyNode { /// fn from(item: &HubName) -> Self { ... } /// } /// /// impl revent::Selfscriber<HubName> for MyHandler { /// fn name() -> &'static str { ... } /// fn type_id() -> TypeId { ... } /// fn selfscribe(holder: &HubName, item: Rc<RefCell<Self>>) { ... } /// } /// /// impl revent::Nodified for MyHandler { /// type Node = MyNode; /// } /// ``` #[macro_export] macro_rules! node { ( $($source:path),+$(,)? { $($listen:ident: $listen_type:path),*$(,)? } => $hub:ident($on:path) { $($emit:ident: $emit_type:path),*$(,)? } ) => { $crate::node_internal! { hub $hub { $($emit: $emit_type),* } } $crate::node_internal! { from $hub, $($source),+ { $($emit: $emit_type),* } } $crate::node_internal! { selfscribe $on { $($source),* } { $($listen),* } } impl $crate::Nodified for $on { type Node = $hub; } }; } #[doc(hidden)] #[macro_export] macro_rules! node_internal { (hub $hub:ident { $($emit:ident: $emit_type:path),* }) => { /// Event hub. pub struct $hub { _private_revent_1_manager: ::std::rc::Rc<::std::cell::RefCell<$crate::Manager>>, $($emit: $crate::Signal<dyn $emit_type>),* } impl $hub { /// Add a new subscriber. #[allow(dead_code)] pub fn subscribe<T>(&mut self, input: T::Input) -> ::std::rc::Rc<::std::cell::RefCell<T>> where T: $crate::Nodified + $crate::Selfscriber<Self> + $crate::Subscriber, T::Node: for<'a> ::std::convert::From<&'a mut Self>, { self._private_revent_1_manager.borrow_mut().prepare_construction(T::name(), T::type_id()); let sub: T::Node = ::std::convert::From::from(&mut *self); let item = ::std::rc::Rc::new(::std::cell::RefCell::new(T::build(sub, input))); T::selfscribe(self, item.clone()); self._private_revent_1_manager.borrow_mut().finish_construction(); item } $( /// Access a signal. #[allow(dead_code)] pub fn $emit(&mut self) -> &mut $crate::Signal<dyn $emit_type> { &mut self.$emit } )* /// Generate a dependency graph in the graphviz `dot` language. #[allow(dead_code)] pub fn generate_graphviz(&self) -> String { let mng = self._private_revent_1_manager.borrow_mut(); format!("{}", $crate::Grapher::new(&*mng)) } #[doc(hidden)] pub fn _private_revent_1_manager(&self) -> ::std::rc::Rc<::std::cell::RefCell<$crate::Manager>> { self._private_revent_1_manager.clone() } } impl ::std::ops::Drop for $hub { fn drop(&mut self) {} } }; (from $hub:path, $source:path { $($emit:ident: $emit_type:path),* }) => { impl ::std::convert::From<&'_ mut $source> for $hub { fn from(item: &mut $source) -> Self { Self { _private_revent_1_manager: item._private_revent_1_manager().clone(), $($emit: item.$emit().internal_clone()),* } } } }; (from $hub:path, $source:path, $($rest:path),+ { $($emit:ident: $emit_type:path),* }) => { crate::node_internal! { from $hub, $($rest),+ { $($emit: $emit_type),* } } }; (selfscribe $on:path { $source:path } { $($listen:ident),* }) => { impl $crate::Selfscriber<$source> for $on { fn name() -> &'static str { stringify!($on) } fn type_id() -> ::std::any::TypeId { ::std::any::TypeId::of::<Self>() } #[allow(unused_variables)] fn selfscribe(holder: &mut $source, item: ::std::rc::Rc<::std::cell::RefCell<Self>>) { $(holder.$listen().insert(item.clone());)* } } }; (selfscribe $on:path { $source:path, $($rest:path),+ } { $($listen:ident),* }) => { crate::node_internal! { selfscribe $on { $source } { $($listen),* } } crate::node_internal! { selfscribe $on { $($rest),+ } { $($listen),* } } }; } #[cfg(test)] mod tests { use crate::*; #[test] fn empty_hub_creation() { hub! { X { } } X::new(); X::default(); } #[test] fn hub_with_a_trait() { pub trait A {} hub! { X { a: A, } } X::new(); X::default(); } #[test] fn hub_with_node() { pub trait A {} hub! { X { a: A, b: A, } } node! { X { a: A, } => Node(Handler) { b: A, } } struct Handler; impl A for Handler {} X::new(); X::default(); } #[test] fn accessing_the_subscriber_return() { pub trait A { fn a(&mut self); } hub! { X { a: A, } } node! { X { a: A, } => Node(Handler) { } } struct Handler { number: i32, } impl A for Handler { fn a(&mut self) { assert_eq!(self.number, 32); } } impl Subscriber for Handler { type Input = (); fn build(_: Self::Node, _: Self::Input) -> Self { Self { number: 0 } } } let mut x = X::new(); let a = x.subscribe::<Handler>(()); a.borrow_mut().number = 32; x.a().emit(|x| x.a()); } #[test] #[should_panic(expected = "[Handler]a -> a")] fn hub_recursion() { pub trait A { fn a(&mut self); } hub! { X { a: A, } } node! { X { a: A, } => Node(Handler) { a: A, } } struct Handler { node: Node, } impl A for Handler { fn a(&mut self) { self.node.a().emit(|x| { x.a(); }); } } impl Subscriber for Handler { type Input = (); fn build(node: Self::Node, _: Self::Input) -> Self { Self { node } } } let mut x = X::new(); x.subscribe::<Handler>(()); x.a().emit(|x| x.a()); } #[test] #[should_panic(expected = "[AToBHandler]a -> [BToAHandler]b -> a")] fn hub_dual_recursion() { pub trait A { fn a(&mut self); } pub trait B { fn b(&mut self); } hub! { X { a: A, b: B, } } node! { X { a: A, } => AToB(AToBHandler) { b: B, } } struct AToBHandler { node: AToB, } impl A for AToBHandler { fn a(&mut self) { self.node.b().emit(|x| { x.b(); }); } } impl Subscriber for AToBHandler { type Input = (); fn build(node: Self::Node, _: Self::Input) -> Self { Self { node } } } node! { X { b: B, } => BToA(BToAHandler) { a: A, } } struct BToAHandler { node: BToA, } impl B for BToAHandler { fn b(&mut self) { self.node.a().emit(|x| { x.a(); }); } } impl Subscriber for BToAHandler { type Input = (); fn build(node: Self::Node, _: Self::Input) -> Self { Self { node } } } let mut x = X::new(); x.subscribe::<AToBHandler>(()); x.subscribe::<BToAHandler>(()); } #[test] fn sorting() { pub trait A { fn value(&self) -> i32; } hub! { X { a: A, } } node! { X { a: A, } => Node(Handler) { } } struct Handler(i32); impl A for Handler { fn value(&self) -> i32 { self.0 } } impl Subscriber for Handler { type Input = i32; fn build(_: Self::Node, input: Self::Input) -> Self { Self(input) } } let mut x = X::new(); for value in 0..10 { x.subscribe::<Handler>(value); } x.a().sort_by(|a, b| b.value().cmp(&a.value())); let mut count = 9; x.a().emit(|item| { assert_eq!(item.value(), count); count -= 1; }); } #[test] #[should_panic(expected = "revent found a recursion during subscription: [Handler]a -> a")] fn same_name_different_module_recursion() { mod test { pub trait A {} hub! { X { a: A, } } pub mod a { use super::{A, X}; use crate::Subscriber; node! { X { a: A, } => Node(Handler) { } } pub struct Handler; impl A for Handler {} impl Subscriber for Handler { type Input = (); fn build(_: Self::Node, _: Self::Input) -> Self { Self } } } pub mod b { use super::{A, X}; use crate::Subscriber; node! { X { a: A, } => Node(Handler) { a: A, } } pub struct Handler; impl A for Handler {} impl Subscriber for Handler { type Input = (); fn build(_: Self::Node, _: Self::Input) -> Self { Self } } } } let mut x = test::X::new(); x.subscribe::<test::a::Handler>(()); x.subscribe::<test::b::Handler>(()); } #[test] #[should_panic( expected = "revent found a recursion during subscription: [Handler]a -> [Handler#1]b -> a" )] fn same_name_different_module_cooperative_recursion() { mod test { pub trait A {} pub trait B {} hub! { X { a: A, b: B, } } pub mod a { use super::{A, B, X}; use crate::Subscriber; node! { X { a: A, } => Node(Handler) { b: B, } } pub struct Handler; impl A for Handler {} impl Subscriber for Handler { type Input = (); fn build(_: Self::Node, _: Self::Input) -> Self { Self } } } pub mod b { use super::{A, B, X}; use crate::Subscriber; node! { X { b: B, } => Node(Handler) { a: A, } } pub struct Handler; impl B for Handler {} impl Subscriber for Handler { type Input = (); fn build(_: Self::Node, _: Self::Input) -> Self { Self } } } } let mut x = test::X::new(); x.subscribe::<test::a::Handler>(()); x.subscribe::<test::b::Handler>(()); } #[test] #[should_panic( expected = "revent found a recursion during subscription: [Handler]a -> [Handler#1]b -> [Handler#2]c -> a" )] fn even_deeper_same_name_recursion() { mod test { pub trait A {} pub trait B {} pub trait C {} hub! { X { a: A, b: B, c: C, } } pub mod a { use super::{A, B, X}; use crate::Subscriber; node! { X { a: A, } => Node(Handler) { b: B, } } pub struct Handler; impl A for Handler {} impl Subscriber for Handler { type Input = (); fn build(_: Self::Node, _: Self::Input) -> Self { Self } } } pub mod b { use super::{B, C, X}; use crate::Subscriber; node! { X { b: B, } => Node(Handler) { c: C, } } pub struct Handler; impl B for Handler {} impl Subscriber for Handler { type Input = (); fn build(_: Self::Node, _: Self::Input) -> Self { Self } } } pub mod c { use super::{A, C, X}; use crate::Subscriber; node! { X { c: B, } => Node(Handler) { a: A, } } pub struct Handler; impl C for Handler {} impl Subscriber for Handler { type Input = (); fn build(_: Self::Node, _: Self::Input) -> Self { Self } } } } let mut x = test::X::new(); x.subscribe::<test::a::Handler>(()); x.subscribe::<test::b::Handler>(()); x.subscribe::<test::c::Handler>(()); } #[test] #[should_panic(expected = "revent found a recursion during subscription: [Handler]b -> b")] fn secondary_self_recursion() { pub trait A {} pub trait B {} hub! { X { a: A, b: B, } } node! { X { a: A, b: B, } => Node(Handler) { b: B, } } pub struct Handler; impl A for Handler {} impl B for Handler {} impl Subscriber for Handler { type Input = (); fn build(_: Self::Node, _: Self::Input) -> Self { Self } } let mut x = X::new(); x.subscribe::<Handler>(()); } }