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 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
//! A functional reactive stream library for rust. //! //! * Small (~20 operations) //! * Synchronous //! * No dependencies //! * Is FRP (ha!) //! //! Modelled on André Staltz' javascript library [xstream][xstrem] which nicely distills //! the ideas of [reactive extensions (Rx)][reactx] down to the essential minimum. //! //! This library is not FRP (Functional Reactive Programming) in the way it was //! defined by Conal Elliot, but as a paradigm that is both functional and reactive. //! [Why I cannot say FRP but I just did][notfrp]. //! //! [xstrem]: https://github.com/staltz/xstream //! [reactx]: http://reactivex.io //! [notfrp]: https://medium.com/@andrestaltz/why-i-cannot-say-frp-but-i-just-did-d5ffaa23973b //! //! ## Example //! //! ``` //! use froop::{Sink, Stream}; //! //! // A sink is an originator of events that form a stream. //! let sink: Sink<u32> = Stream::sink(); //! //! // Map the even numbers to their square. //! let stream: Stream<u32> = sink.stream() //! .filter(|i| i % 2 == 0) //! .map(|i| i * i); //! //! // Print the result //! stream.subscribe(|i| if let Some(i) = i { //! println!("{}", i) //! }); //! //! // Send numbers into the sink. //! for i in 0..10 { //! sink.update(i); //! } //! sink.end(); //! ``` //! //! # Idea //! //! Functional Reactive Programming is a good foundation for functional programming (FP). //! The step-by-step approach of composing interlocked operations, is a relatively //! easy way to make an FP structure to a piece of software. //! //! ## Synchronous //! //! Libraries that deals with streams as values-over-time (or events) often conflate the //! idea of moving data from point A to B, with the operators that transform the data. The //! result is that the library must deal with queues of data, queue lengths and backpressure. //! //! _Froop has no queues_ //! //! Every [`Sink::update()`](struct.Sink.html#method.update) of data into the tree of //! operations executes synchronously. Froop has no operators that dispatches "later", //! i.e. no `delay()` or other time shifting operations. //! //! That also means froop also has no internal threads, futures or otherwise. //! //! ## Thread safe //! //! Every part of the froop tree is thread safe. You can move a `Sink` into another thread, //! or subscribe and propagate on a UI main thread. The thread that calls `Sink::update()` is //! the thread executing the entire tree. //! //! That safety comes at a cost, froop is not a zero cost abstraction library. Every part of //! the tree is protected by a mutex lock. This is fine for most applications since a lock //! without contention is not much overhead in the execution. But if you plan on having //! lots of threads simultaneously updating many values into the tree, you might //! experience a performance hit due to lock contention. //! //! ## Be out of your way //! //! Froop tries to impose a minimum of cognitive load when using it. //! //! * Every operator is an `FnMut(&T)` to make it the most usable possible. //! * Not require `Sync` and/or `Send` on operator functions. //! * Froop stream instances themselves are `Sync` and `Send`. //! * Impose a minimum of constraints the event value `T`. //! //! ## Subscription lifetimes //! //! See [`Subscription`](struct.Subscription.html#subscription-lifetimes) #![warn(clippy::all)] #![allow(clippy::new_without_default)] use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Condvar, Mutex}; mod imit; mod inner; mod peg; mod sub; pub use crate::imit::Imitator; use crate::inner::{MemoryMode, SafeInner, IMITATORS}; use crate::peg::Peg; pub use crate::sub::Subscription; /// A stream of events, values in time. /// /// Streams have combinators to build "execution trees" working over events. /// /// ## Memory /// /// Some streams have "memory". Streams with memory keeps a copy of the last value they /// produced so that any new subscriber will syncronously receive the value. /// /// Streams with memory are explicitly created using /// [`.remember()`](struct.Stream.html#method.remember), but also by other combinators /// such as [`.fold()`](struct.Stream.html#method.fold) and /// [`.start_with()`](struct.Stream.html#method.start_with). pub struct Stream<T: 'static> { #[allow(dead_code)] peg: Peg, inner: SafeInner<T>, } impl<T> Stream<T> { // /// Create a sink that is used to push values into a stream. /// /// ``` /// let sink = froop::Stream::sink(); /// /// // collect values going into the sink /// let coll = sink.stream().collect(); /// /// sink.update(0); /// sink.update(1); /// sink.update(2); /// sink.end(); /// /// assert_eq!(coll.wait(), vec![0, 1, 2]); /// ``` pub fn sink() -> Sink<T> { Sink::new() } /// Create a stream with memory that only emits one single value to anyone subscribing. /// /// ``` /// let value = froop::Stream::of(42); /// /// // both collectors will receive the value /// let coll1 = value.collect(); /// let coll2 = value.collect(); /// /// // use .take() since stream doesn't end /// assert_eq!(coll1.take(), [42]); /// assert_eq!(coll2.take(), [42]); /// ``` pub fn of(value: T) -> Stream<T> where T: Clone, { let inner = SafeInner::new(MemoryMode::KeepUntilEnd, Some(value)); Stream { peg: Peg::new_fake(), inner, } } /// Create a stream that never emits any value and never ends. /// /// ``` /// use froop::Stream; /// /// let never: Stream<u32> = Stream::never(); /// let coll = never.collect(); /// assert_eq!(coll.take(), vec![]); /// ``` pub fn never() -> Stream<T> { let inner = SafeInner::new(MemoryMode::NoMemory, None); Stream { peg: Peg::new_fake(), inner, } } /// Check if this stream has "memory". /// /// Streams with memory keeps a copy of the last value they produced so that any /// new subscriber will syncronously receive the value. /// /// Streams with memory are explicitly created using `.remember()`, but also by /// other combinators such as `.fold()` and `.start_with()`. /// /// The memory is not inherited to child combinators. I.e. /// /// ``` /// let sink = froop::Stream::sink(); /// sink.update(0); /// /// // This stream has memory. /// let rem = sink.stream().remember(); /// /// // This filtered stream has _NO_ memory. /// let filt = rem.filter(|t| *t > 10); /// /// assert!(rem.has_memory()); /// assert!(!filt.has_memory()); /// ``` pub fn has_memory(&self) -> bool { self.inner.lock().memory_mode().is_memory() } /// Creates an imitator. Imitators are used to make cyclic streams. /// /// pub fn imitator() -> Imitator<T> where T: Clone, { Imitator::new() } /// Subscribe to events from this stream. The returned subscription can be used to /// unsubscribe at a future time. /// /// Each value is wrapped in an `Option`, there will be exactly one None event when /// the stream ends. /// /// ``` /// let sink = froop::Stream::sink(); /// let stream = sink.stream(); /// /// let handle = std::thread::spawn(move || { /// /// // values are Some(0), Some(1), Some(2), None /// stream.subscribe(|v| if let Some(v) = v { /// println!("Got value: {}", v); /// }); /// /// // stall thread until stream ends. /// stream.wait(); /// }); /// /// sink.update(0); /// sink.update(1); /// sink.update(2); /// sink.end(); /// /// handle.join(); /// ``` pub fn subscribe<F>(&self, f: F) -> Subscription where F: FnMut(Option<&T>) + 'static, { let peg = self.inner.lock().add(f); peg.keep_mode(); Subscription::new(peg) } /// Internal subscribe that stops subscribing if the subscription goes out of scope. fn internal_subscribe<F: FnMut(Option<&T>) + 'static>(&self, f: F) -> Peg { let mut peg = self.inner.lock().add(f); peg.add_related(self.peg.clone()); peg } /// Collect events into a `Collector`. This is mostly interesting for testing. /// /// ``` /// let sink = froop::Stream::sink(); /// /// // collect all values emitted into the sink /// let coll = sink.stream().collect(); /// /// std::thread::spawn(move || { /// sink.update(0); /// sink.update(1); /// sink.update(2); /// sink.end(); /// }); /// /// let result = coll.wait(); // wait for stream to end /// assert_eq!(result, vec![0, 1, 2]); /// ``` pub fn collect(&self) -> Collector<T> where T: Clone, { let state = Arc::new((Mutex::new((false, Some(vec![]))), Condvar::new())); let clone = state.clone(); let peg = self.internal_subscribe(move |t| { let mut lock = clone.0.lock().unwrap(); if let Some(t) = t { if let Some(v) = lock.1.as_mut() { v.push(t.clone()); } } else { lock.0 = true; clone.1.notify_all(); } }); Collector { peg, state } } /// Dedupe stream by the event itself. /// /// This clones every event to compare with the next. /// /// ``` /// let sink = froop::Stream::sink(); /// /// let deduped = sink.stream().dedupe(); /// /// let coll = deduped.collect(); /// /// sink.update(0); /// sink.update(0); /// sink.update(1); /// sink.update(1); /// sink.end(); /// /// assert_eq!(coll.wait(), vec![0, 1]); /// ``` pub fn dedupe(&self) -> Stream<T> where T: Clone + PartialEq, { self.dedupe_by(|v| v.clone()) } /// Dedupe stream by some extracted value. /// /// ``` /// use froop::{Stream, Sink}; /// /// #[derive(Clone, Debug)] /// struct Foo(&'static str, usize); /// /// let sink: Sink<Foo> = Stream::sink(); /// /// // dedupe this stream of Foo on the contained usize /// let deduped = sink.stream().dedupe_by(|v| v.1); /// /// let coll = deduped.collect(); /// /// sink.update(Foo("yo", 1)); /// sink.update(Foo("bro", 1)); /// sink.update(Foo("lo", 2)); /// sink.update(Foo("lo", 2)); /// sink.end(); /// /// assert_eq!(format!("{:?}", coll.wait()), /// "[Foo(\"yo\", 1), Foo(\"lo\", 2)]"); /// ``` pub fn dedupe_by<U, F>(&self, mut f: F) -> Stream<T> where U: PartialEq + 'static, F: FnMut(&T) -> U + 'static, { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let mut prev: Option<U> = None; let peg = self.internal_subscribe(move |t| { if let Some(t) = t { let propagate = match (prev.take(), f(t)) { (None, u) => { // no previous value, save this and propagate prev = Some(u); true } (Some(pu), u) => { if pu != u { // new value is different to previous, save and propagate prev = Some(u); true } else { // new value is same as before, don't propagate false } } }; if propagate { inner_clone.lock().update_borrowed(Some(t)); } } else { inner_clone.lock().update_borrowed(t); } }); Stream { peg, inner } } /// Drop an amount of initial values. /// /// ``` /// let sink = froop::Stream::sink(); /// /// // drop 2 initial values /// let dropped = sink.stream().drop(2); /// /// let coll = dropped.collect(); /// /// sink.update(0); /// sink.update(1); /// sink.update(2); /// sink.update(3); /// sink.end(); /// /// assert_eq!(coll.wait(), vec![2, 3]); /// ``` pub fn drop(&self, amount: usize) -> Stream<T> { let mut todo = amount + 1; self.drop_while(move |_| { if todo > 0 { todo -= 1; } todo > 0 }) } /// Don't take values while some condition holds true. Once the condition is false, /// the resulting stream emits all events. /// /// ``` /// let sink = froop::Stream::sink(); /// /// // drop initial odd values /// let dropped = sink.stream().drop_while(|v| v % 2 == 1); /// /// let coll = dropped.collect(); /// /// sink.update(1); /// sink.update(3); /// sink.update(4); /// sink.update(5); // not dropped /// sink.end(); /// /// assert_eq!(coll.wait(), vec![4, 5]); /// ``` pub fn drop_while<F>(&self, mut f: F) -> Stream<T> where F: FnMut(&T) -> bool + 'static, { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let mut dropping = true; let peg = self.internal_subscribe(move |t| { if let Some(t) = t { if dropping && !f(t) { dropping = false; } if dropping { return; } inner_clone.lock().update_borrowed(Some(t)); } else { inner_clone.lock().update_borrowed(t); } }); Stream { peg, inner } } /// Produce a stream that ends when some other stream ends. /// /// ``` /// use froop::Stream; /// /// let sink1 = Stream::sink(); /// let sink2 = Stream::sink(); /// /// // ending shows values of sink1, but ends when sink2 does. /// let ending = sink1.stream().end_when(&sink2.stream()); /// /// let coll = ending.collect(); /// /// sink1.update(0); /// sink2.update("yo"); /// sink1.update(1); /// sink2.end(); /// sink1.update(2); // collector never sees this value /// /// assert_eq!(coll.wait(), [0, 1]); /// ``` pub fn end_when<U>(&self, other: &Stream<U>) -> Stream<T> { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone1 = inner.clone(); let inner_clone2 = inner.clone(); let peg1 = other.internal_subscribe(move |o| { if o.is_none() { inner_clone1.lock().update_borrowed(None); } }); let peg2 = self.internal_subscribe(move |t| { inner_clone2.lock().update_borrowed(t); }); let peg = Peg::many(vec![peg1, peg2]); Stream { peg, inner } } /// Filter out a subset of the events in the stream. /// /// ``` /// let sink = froop::Stream::sink(); /// /// // keep even numbers /// let filtered = sink.stream().filter(|v| v % 2 == 0); /// /// let coll = filtered.collect(); /// /// sink.update(0); /// sink.update(1); /// sink.update(2); /// sink.end(); /// /// assert_eq!(coll.wait(), vec![0, 2]); /// ``` pub fn filter<F>(&self, mut f: F) -> Stream<T> where F: FnMut(&T) -> bool + 'static, { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let peg = self.internal_subscribe(move |t| { if let Some(t) = t { if f(t) { inner_clone.lock().update_borrowed(Some(t)); } } else { inner_clone.lock().update_borrowed(t); } }); Stream { peg, inner } } /// Combine events from the past, with new events to produce an output. /// /// This is roughly equivalent to a "fold" or "reduce" over an array. For each event we /// emit the latest state out. The seed value is emitted straight away. /// /// The result is always a "memory" stream. /// /// ``` /// let sink = froop::Stream::sink(); /// /// let folded = sink.stream() /// .fold(40.5, |prev, next| prev + (*next as f32) / 2.0); /// /// let coll = folded.collect(); /// /// sink.update(0); /// sink.update(1); /// sink.update(2); /// sink.end(); /// /// assert_eq!(coll.wait(), vec![40.5, 40.5, 41.0, 42.0]); /// ``` pub fn fold<U, F>(&self, seed: U, mut f: F) -> Stream<U> where U: 'static, F: FnMut(U, &T) -> U + 'static, { let inner = SafeInner::new(MemoryMode::KeepUntilEnd, Some(seed)); let inner_clone = inner.clone(); let peg = self.internal_subscribe(move |t| { if let Some(t) = t { let mut lock = inner_clone.lock(); if let Some(prev) = lock.take_memory() { let next = f(prev, t); lock.update_owned(Some(next)); } else { panic!("fold without a previous value"); } } else { inner_clone.lock().update_owned(None); } }); Stream { peg, inner } } /// Internal imitate for imitator. fn imitate(&self, imitator: SafeInner<T>) -> Peg where T: Clone, { self.internal_subscribe(move |t| { let imitator_clone = imitator.clone(); if t.is_some() { let t_clone = t.cloned(); IMITATORS.with(|imit_cell| { let mut imit = imit_cell.borrow_mut(); imit.push(Box::new(move || { // this is one clone too many. if we could use // Box<FnOnce> on stable, we would do that instead let t = t_clone.clone(); imitator_clone.lock().update_owned(t.clone()); })); }); } else { imitator_clone.lock().update_owned(None); } }) } /// Emits the last seen event when the stream closes. /// /// ``` /// let sink = froop::Stream::sink(); /// /// let coll = sink.stream().last().collect(); /// /// sink.update(0); /// sink.update(1); /// sink.update(2); /// sink.end(); /// /// assert_eq!(coll.wait(), vec![2]); /// ``` pub fn last(&self) -> Stream<T> where T: Clone, { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let last = Mutex::new(None); let peg = self.internal_subscribe(move |t| { let mut lock = last.lock().unwrap(); if t.is_some() { *lock = t.cloned(); } else { let mut ilock = inner_clone.lock(); if let Some(l) = lock.take() { ilock.update_owned(Some(l)); } ilock.update_owned(None); } }); Stream { peg, inner } } /// Transform events. /// /// ``` /// let sink = froop::Stream::sink(); /// /// let mapped = sink.stream().map(|v| format!("yo {}", v)); /// /// let coll = mapped.collect(); /// /// sink.update(41); /// sink.update(42); /// sink.end(); /// /// assert_eq!(coll.wait(), /// vec!["yo 41".to_string(), "yo 42".to_string()]); /// ``` pub fn map<U, F>(&self, mut f: F) -> Stream<U> where U: 'static, F: FnMut(&T) -> U + 'static, { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let peg = self.internal_subscribe(move |t| { if let Some(t) = t { let u = f(t); inner_clone.lock().update_owned(Some(u)); } else { inner_clone.lock().update_owned(None); } }); Stream { peg, inner } } /// For every event, emit a static value. /// /// ``` /// let sink = froop::Stream::sink(); /// /// let mapped = sink.stream().map_to(42.0); /// /// let coll = mapped.collect(); /// /// sink.update(0); /// sink.update(1); /// sink.end(); /// /// assert_eq!(coll.wait(), vec![42.0, 42.0]); /// ``` pub fn map_to<U>(&self, u: U) -> Stream<U> where U: Clone + 'static, { self.map(move |_| u.clone()) } /// Merge events from a bunch of streams to one stream. /// /// ``` /// use froop::Stream; /// /// let sink1 = Stream::sink(); /// let sink2 = Stream::sink(); /// /// let merged = Stream::merge(vec![ /// sink1.stream(), /// sink2.stream() /// ]); /// /// let coll = merged.collect(); /// /// sink1.update(0); /// sink2.update(10); /// sink1.update(1); /// sink2.update(11); /// sink1.end(); /// sink2.end(); /// /// assert_eq!(coll.wait(), vec![0, 10, 1, 11]); /// ``` pub fn merge(streams: Vec<Stream<T>>) -> Stream<T> { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let active = Arc::new(AtomicUsize::new(streams.len())); let pegs: Vec<_> = streams .into_iter() .map(|stream| { let inner_clone = inner_clone.clone(); let active = active.clone(); stream.internal_subscribe(move |t| { if t.is_some() { inner_clone.lock().update_borrowed(t); } else if active.fetch_sub(1, Ordering::SeqCst) == 1 { // all streams are ended. close the merged one inner_clone.lock().update_borrowed(None); } }) }) .collect(); let peg = Peg::many(pegs); Stream { peg, inner } } /// Make a stream in memory mode. Each value is remembered for future subscribers. /// /// ``` /// let sink = froop::Stream::sink(); /// /// let rem = sink.stream().remember(); /// /// sink.update(0); /// sink.update(1); /// /// // receives last "remembered" value /// let coll = rem.collect(); /// /// sink.update(2); /// sink.end(); /// /// assert_eq!(coll.wait(), vec![1, 2]); /// ``` pub fn remember(&self) -> Stream<T> where T: Clone, { self.remember_mode(MemoryMode::KeepUntilEnd) } /// Internal remember where we can chose "mode" fn remember_mode(&self, mode: MemoryMode) -> Stream<T> where T: Clone, { let inner = SafeInner::new(mode, None); let inner_clone = inner.clone(); let peg = self.internal_subscribe(move |t| { let t = t.cloned(); inner_clone.lock().update_owned(t); }); Stream { peg, inner } } /// On every event in this stream, combine with the last value of the other stream. /// /// ``` /// use froop::Stream; /// /// let sink1 = Stream::sink(); /// let sink2 = Stream::sink(); /// /// let comb = sink1.stream().sample_combine(&sink2.stream()); /// /// let coll = comb.collect(); /// /// sink1.update(0); // lost, because no value in sink2 /// sink2.update("foo"); // doesn't trigger combine /// sink1.update(1); /// sink1.update(2); /// sink2.update("bar"); /// sink2.end(); // sink2 is "bar" forever /// sink1.update(3); /// sink1.end(); /// /// assert_eq!(coll.wait(), /// vec![(1, "foo"), (2, "foo"), (3, "bar")]) /// ``` pub fn sample_combine<U>(&self, other: &Stream<U>) -> Stream<(T, U)> where T: Clone, U: Clone, { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let rem = other.remember_mode(MemoryMode::KeepAfterEnd); let peg = self.internal_subscribe(move |t| { if let Some(t) = t { let rlock = rem.inner.lock(); if let Some(u) = rlock.peek_memory().as_ref() { // we have both t and u let v = (t.clone(), u.clone()); inner_clone.lock().update_owned(Some(v)); } } else { inner_clone.lock().update_borrowed(None); } }); Stream { peg, inner } } /// Prepend a start value to the stream. The result is a memory stream. /// /// ``` /// let sink = froop::Stream::sink(); /// /// sink.update(0); // lost /// /// let started = sink.stream().start_with(1); /// /// let coll = started.collect(); // receives 1 and following /// /// sink.update(2); /// sink.end(); /// /// assert_eq!(coll.wait(), vec![1, 2]); /// ``` pub fn start_with(&self, start: T) -> Stream<T> { let inner = SafeInner::new(MemoryMode::KeepUntilEnd, Some(start)); let inner_clone = inner.clone(); let peg = self.internal_subscribe(move |t| { inner_clone.lock().update_borrowed(t); }); Stream { peg, inner } } /// Take a number of events, then end the stream. /// /// ``` /// let sink = froop::Stream::sink(); /// /// let take2 = sink.stream().take(2); /// /// let coll = take2.collect(); /// /// sink.update(0); /// sink.update(1); // take2 ends here /// sink.update(2); /// /// assert_eq!(coll.wait(), vec![0, 1]); /// ``` pub fn take(&self, amount: usize) -> Stream<T> { let mut todo = amount + 1; self.take_while(move |_| { if todo > 0 { todo -= 1; } todo > 0 }) } /// Take events from the stream as long as a condition holds true. /// /// ``` /// let sink = froop::Stream::sink(); /// /// // take events as long as they are even /// let take = sink.stream().take_while(|v| *v % 2 == 0); /// /// let coll = take.collect(); /// /// sink.update(0); /// sink.update(2); /// sink.update(3); // take ends here /// sink.update(4); /// /// assert_eq!(coll.wait(), vec![0, 2]); /// ``` pub fn take_while<F>(&self, mut f: F) -> Stream<T> where F: FnMut(&T) -> bool + 'static, { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let peg = self.internal_subscribe(move |t| { if let Some(t) = t { if f(t) { inner_clone.lock().update_borrowed(Some(t)); } else { inner_clone.lock().update_borrowed(None); } } else { inner_clone.lock().update_borrowed(t); } }); Stream { peg, inner } } /// Stalls calling thread until the stream ends. /// /// ``` /// let sink = froop::Stream::sink(); /// let stream = sink.stream(); /// /// std::thread::spawn(move || { /// sink.update(0); /// sink.update(1); /// sink.update(2); /// sink.end(); // this releases the wait /// }); /// /// stream.wait(); // wait for other thread /// ``` #[allow(clippy::mutex_atomic)] pub fn wait(&self) { let pair = Arc::new((Mutex::new(false), Condvar::new())); let pair2 = pair.clone(); let _sub = self.internal_subscribe(move |t| { if t.is_none() { let mut lock = pair2.0.lock().unwrap(); *lock = true; pair2.1.notify_all(); } }); let mut lock = pair.0.lock().unwrap(); while !*lock { lock = pair.1.wait(lock).unwrap(); } } } impl<T> Stream<Stream<T>> { // /// Flatten out a stream of streams, sequentially. /// /// For each new stream, unsubscribe from the previous, and subscribe to the new. The new /// stream "interrupts" the previous stream. /// /// ``` /// use froop::{Stream, Sink}; /// /// let sink1: Sink<Stream<u32>> = Stream::sink(); /// let sink2: Sink<u32> = Stream::sink(); /// let sink3: Sink<u32> = Stream::sink(); /// /// let flat = sink1.stream().flatten(); /// /// let coll = flat.collect(); /// /// sink2.update(0); // lost /// /// sink1.update(sink2.stream()); /// sink2.update(1); /// sink2.update(2); /// sink2.end(); // does not end sink1 /// /// sink3.update(10); // lost /// /// sink1.update(sink3.stream()); /// sink3.update(11); /// /// sink1.end(); /// /// assert_eq!(coll.wait(), vec![1, 2, 11]); /// ``` pub fn flatten(&self) -> Stream<T> { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let mut ipeg = None; let peg = self.internal_subscribe(move |ts| { if let Some(ts) = ts { let inner_clone = inner_clone.clone(); ipeg = Some(ts.internal_subscribe(move |tv| { if let Some(tv) = tv { inner_clone.lock().update_borrowed(Some(tv)); } else { // inner stream end does nothing to outer } })); } else { ipeg.take(); inner_clone.lock().update_borrowed(None); } }); Stream { peg, inner } } /// Flatten out a stream of streams, concurrently. /// /// For each new stream, keep the previous, and subscribe to the new. /// /// ``` /// use froop::{Stream, Sink}; /// /// let sink1: Sink<Stream<u32>> = Stream::sink(); /// let sink2: Sink<u32> = Stream::sink(); /// let sink3: Sink<u32> = Stream::sink(); /// /// let flat = sink1.stream().flatten_concurrently(); /// /// let coll = flat.collect(); /// /// sink2.update(0); // lost /// /// sink1.update(sink2.stream()); /// sink2.update(1); /// sink2.update(2); /// /// sink3.update(10); // lost /// /// sink1.update(sink3.stream()); /// sink3.update(11); /// sink2.update(3); /// sink3.update(12); /// /// sink1.end(); /// /// assert_eq!(coll.wait(), vec![1, 2, 11, 3, 12]); /// ``` pub fn flatten_concurrently(&self) -> Stream<T> { let inner = SafeInner::new(MemoryMode::NoMemory, None); let inner_clone = inner.clone(); let peg = self.internal_subscribe(move |ts| { if let Some(ts) = ts { let inner_clone = inner_clone.clone(); let ipeg = ts.internal_subscribe(move |tv| { if let Some(tv) = tv { inner_clone.lock().update_borrowed(Some(tv)); } else { // inner stream end does nothing to outer } }); ipeg.keep_mode(); // we drop ipeg, but keep listening } else { inner_clone.lock().update_borrowed(None); } }); Stream { peg, inner } } } include!("./comb.rs"); /// A sink is a producer of events. Created by [`Stream::sink()`](struct.Stream.html#method.sink). pub struct Sink<T: 'static> { inner: SafeInner<T>, } impl<T> Sink<T> { /// Create a new sink that in turn is used to stream events. fn new() -> Self { Sink { inner: SafeInner::new(MemoryMode::NoMemory, None), } } /// Get a stream of events from this sink. One stream instance is created for each call, /// and they all receive the events from the sink. /// /// ``` /// let sink = froop::Stream::sink(); /// /// let stream1 = sink.stream(); /// let stream2 = sink.stream(); /// /// let coll1 = stream1.collect(); /// let coll2 = stream1.collect(); /// /// sink.update(42); /// sink.end(); /// /// assert_eq!(coll1.wait(), vec![42]); /// assert_eq!(coll2.wait(), vec![42]); /// ``` pub fn stream(&self) -> Stream<T> { Stream { peg: Peg::new_fake(), inner: self.inner.clone(), } } /// Update a value into this sink. /// /// The execution of the combinators "hanging" off this sink is (thread safe) and /// synchronous. In other words, there is nothing in froop itself that will still be /// "to do" once the `update()` call returns. /// /// Each value is wrapped in an `Option` towards subscribers of the streams. /// /// ``` /// let sink = froop::Stream::sink(); /// let stream = sink.stream(); /// /// stream.subscribe(|v| { /// // v is Some(0), Some(1), None /// }); /// /// sink.update(0); /// sink.update(1); /// sink.end(); /// ``` pub fn update(&self, next: T) { self.inner.lock().update_and_imitate(Some(next)); } /// End the stream of events. Consumes the instance since no more values are to go into it. /// /// Subscribers will se a `None` value. /// /// Every stream hanging directly off this sink will also end. The exception is streams /// combining input from multiple source streams. pub fn end(self) { self.inner.lock().update_and_imitate(None); } } /// The collector instance collects values from a stream. Created by /// [`Stream::collect()`](struct.Stream.html#method.collect). pub struct Collector<T> { #[allow(dead_code)] peg: Peg, #[allow(clippy::type_complexity)] state: Arc<(Mutex<(bool, Option<Vec<T>>)>, Condvar)>, } impl<T> Collector<T> { /// Stall the thread and wait for the stream to end. pub fn wait(self) -> Vec<T> { let mut lock = self.state.0.lock().unwrap(); while !lock.0 { lock = self.state.1.wait(lock).unwrap(); } lock.1.take().unwrap() } /// Take whatever is there, without the stream ending, and stop collecting. pub fn take(self) -> Vec<T> { let mut lock = self.state.0.lock().unwrap(); lock.1.take().unwrap() } } impl<T> Clone for Stream<T> { fn clone(&self) -> Self { Stream { peg: self.peg.clone(), inner: self.inner.clone(), } } } #[cfg(test)] mod test { use super::*; use std::sync::mpsc::sync_channel; #[test] fn test_sink_auto_traits() { fn f<X: Sync + Send>(_: X) {} let sink: Sink<u32> = Sink::new(); f(sink); } #[test] fn test_stream_auto_traits() { fn f<X: Sync + Send + Clone>(_: X) {} struct Foo(); // not clonable, but Stream<Foo> should be let sink: Sink<Foo> = Sink::new(); f(sink.stream()); } #[test] fn test_subscription_auto_traits() { fn f<X: Sync + Send + Clone>(_: X) {} let sink: Sink<u32> = Sink::new(); let sub = sink.stream().subscribe(|_| {}); f(sub); } #[test] fn test_chained_maps() { let sink: Sink<u32> = Sink::new(); // the risk is that the intermediary map drops the subscription // and the entire chain stalls. let map = sink.stream().map(|x| x + 1).map(|x| x * 2); let coll = map.collect(); sink.update(0); sink.update(1); sink.update(2); sink.end(); assert_eq!(coll.wait(), vec![2, 4, 6]); } #[test] fn test_of() { let stream = Stream::of(42); let (tx, rx) = sync_channel(1); stream.subscribe(move |x| tx.send(*x.unwrap()).unwrap()); assert_eq!(rx.recv().unwrap(), 42); } #[test] fn test_imitate() { let sink: Sink<u32> = Sink::new(); let imit: Imitator<u32> = Imitator::new(); let map = sink.stream().map(|x| x * 2); let coll = imit.stream().collect(); imit.imitate(&map); sink.update(0); sink.update(1); sink.update(2); sink.end(); assert_eq!(coll.wait(), vec![0, 2, 4]); } #[test] fn test_fold_and_last() { let sink: Sink<u32> = Sink::new(); // this potentially creates an edge case where // last might hang on to the rc value that fold has in memory let fold = sink .stream() .fold("|".to_string(), |p, c| format!("{} {}", p, c)) .last(); let coll = fold.collect(); sink.update(42); sink.end(); assert_eq!(coll.wait(), vec!["| 42".to_string()]); } #[test] fn test_fold_and_remember() { let sink: Sink<u32> = Sink::new(); // this potentially creates an edge case where // remember might hang on to the rc value that fold has in memory let fold = sink .stream() .fold("|".to_string(), |p, c| format!("{} {}", p, c)) .remember(); let coll = fold.collect(); sink.update(42); sink.end(); assert_eq!(coll.wait(), vec!["|".to_string(), "| 42".to_string()]); } #[test] fn test_imitate_cycle() { let imitator = Stream::imitator(); let fold = imitator .stream() .fold(1, |p, c| if *c < 10 { p + c } else { p }) .dedupe(); let sink = Stream::sink(); let merge = Stream::merge(vec![fold, sink.stream()]); imitator.imitate(&merge); let coll = merge.collect(); sink.update(1); assert_eq!(coll.take(), vec![1, 2, 4, 8, 16]); } #[test] fn test_combine() { let sink1 = Stream::sink(); let sink2 = Stream::sink(); let comb = Stream::combine2(&sink1.stream(), &sink2.stream()); let coll = comb.collect(); sink1.update(0.0); sink2.update(10); sink1.update(1.0); sink1.update(2.0); sink2.update(11); sink1.update(3.0); sink1.end(); sink2.end(); assert_eq!( coll.wait(), vec![(0.0, 10), (1.0, 10), (2.0, 10), (2.0, 11), (3.0, 11)] ); } }