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 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
//! Streams
//!
//! This module contains a number of functions for working with `Stream`s,
//! including the `StreamExt` trait which adds methods to `Stream` types.
use crate::future::{assert_future, Either};
use crate::stream::assert_stream;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::pin::Pin;
#[cfg(feature = "sink")]
use futures_core::stream::TryStream;
#[cfg(feature = "alloc")]
use futures_core::stream::{BoxStream, LocalBoxStream};
use futures_core::{
future::Future,
stream::{FusedStream, Stream},
task::{Context, Poll},
};
#[cfg(feature = "sink")]
use futures_sink::Sink;
use crate::fns::{inspect_fn, InspectFn};
mod chain;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::chain::Chain;
mod collect;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::collect::Collect;
mod unzip;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::unzip::Unzip;
mod concat;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::concat::Concat;
mod cycle;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::cycle::Cycle;
mod enumerate;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::enumerate::Enumerate;
mod filter;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::filter::Filter;
mod filter_map;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::filter_map::FilterMap;
mod flatten;
delegate_all!(
/// Stream for the [`flatten`](StreamExt::flatten) method.
Flatten<St>(
flatten::Flatten<St, St::Item>
): Debug + Sink + Stream + FusedStream + AccessInner[St, (.)] + New[|x: St| flatten::Flatten::new(x)]
where St: Stream
);
mod fold;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::fold::Fold;
mod any;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::any::Any;
mod all;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::all::All;
#[cfg(feature = "sink")]
mod forward;
#[cfg(feature = "sink")]
delegate_all!(
/// Future for the [`forward`](super::StreamExt::forward) method.
#[cfg_attr(docsrs, doc(cfg(feature = "sink")))]
Forward<St, Si>(
forward::Forward<St, Si, St::Ok>
): Debug + Future + FusedFuture + New[|x: St, y: Si| forward::Forward::new(x, y)]
where St: TryStream
);
mod for_each;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::for_each::ForEach;
mod fuse;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::fuse::Fuse;
mod into_future;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::into_future::StreamFuture;
delegate_all!(
/// Stream for the [`inspect`](StreamExt::inspect) method.
Inspect<St, F>(
map::Map<St, InspectFn<F>>
): Debug + Sink + Stream + FusedStream + AccessInner[St, (.)] + New[|x: St, f: F| map::Map::new(x, inspect_fn(f))]
);
mod map;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::map::Map;
delegate_all!(
/// Stream for the [`flat_map`](StreamExt::flat_map) method.
FlatMap<St, U, F>(
flatten::Flatten<Map<St, F>, U>
): Debug + Sink + Stream + FusedStream + AccessInner[St, (. .)] + New[|x: St, f: F| flatten::Flatten::new(Map::new(x, f))]
);
mod next;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::next::Next;
mod select_next_some;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::select_next_some::SelectNextSome;
mod peek;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::peek::{NextIf, NextIfEq, Peek, Peekable};
mod skip;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::skip::Skip;
mod skip_while;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::skip_while::SkipWhile;
mod take;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::take::Take;
mod take_while;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::take_while::TakeWhile;
mod take_until;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::take_until::TakeUntil;
mod then;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::then::Then;
mod zip;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::zip::Zip;
#[cfg(feature = "alloc")]
mod chunks;
#[cfg(feature = "alloc")]
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::chunks::Chunks;
#[cfg(feature = "alloc")]
mod ready_chunks;
#[cfg(feature = "alloc")]
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::ready_chunks::ReadyChunks;
mod scan;
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::scan::Scan;
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
mod buffer_unordered;
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::buffer_unordered::BufferUnordered;
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
mod buffered;
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::buffered::Buffered;
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
mod for_each_concurrent;
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::for_each_concurrent::ForEachConcurrent;
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "sink")]
#[cfg_attr(docsrs, doc(cfg(feature = "sink")))]
#[cfg(feature = "alloc")]
mod split;
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "sink")]
#[cfg_attr(docsrs, doc(cfg(feature = "sink")))]
#[cfg(feature = "alloc")]
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::split::{ReuniteError, SplitSink, SplitStream};
#[cfg(feature = "std")]
mod catch_unwind;
#[cfg(feature = "std")]
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
pub use self::catch_unwind::CatchUnwind;
impl<T: ?Sized> StreamExt for T where T: Stream {}
/// An extension trait for `Stream`s that provides a variety of convenient
/// combinator functions.
pub trait StreamExt: Stream {
/// Creates a future that resolves to the next item in the stream.
///
/// Note that because `next` doesn't take ownership over the stream,
/// the [`Stream`] type must be [`Unpin`]. If you want to use `next` with a
/// [`!Unpin`](Unpin) stream, you'll first have to pin the stream. This can
/// be done by boxing the stream using [`Box::pin`] or
/// pinning it to the stack using the `pin_mut!` macro from the `pin_utils`
/// crate.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let mut stream = stream::iter(1..=3);
///
/// assert_eq!(stream.next().await, Some(1));
/// assert_eq!(stream.next().await, Some(2));
/// assert_eq!(stream.next().await, Some(3));
/// assert_eq!(stream.next().await, None);
/// # });
/// ```
fn next(&mut self) -> Next<'_, Self>
where
Self: Unpin,
{
assert_future::<Option<Self::Item>, _>(Next::new(self))
}
/// Converts this stream into a future of `(next_item, tail_of_stream)`.
/// If the stream terminates, then the next item is [`None`].
///
/// The returned future can be used to compose streams and futures together
/// by placing everything into the "world of futures".
///
/// Note that because `into_future` moves the stream, the [`Stream`] type
/// must be [`Unpin`]. If you want to use `into_future` with a
/// [`!Unpin`](Unpin) stream, you'll first have to pin the stream. This can
/// be done by boxing the stream using [`Box::pin`] or
/// pinning it to the stack using the `pin_mut!` macro from the `pin_utils`
/// crate.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=3);
///
/// let (item, stream) = stream.into_future().await;
/// assert_eq!(Some(1), item);
///
/// let (item, stream) = stream.into_future().await;
/// assert_eq!(Some(2), item);
/// # });
/// ```
fn into_future(self) -> StreamFuture<Self>
where
Self: Sized + Unpin,
{
assert_future::<(Option<Self::Item>, Self), _>(StreamFuture::new(self))
}
/// Maps this stream's items to a different type, returning a new stream of
/// the resulting type.
///
/// The provided closure is executed over all elements of this stream as
/// they are made available. It is executed inline with calls to
/// [`poll_next`](Stream::poll_next).
///
/// Note that this function consumes the stream passed into it and returns a
/// wrapped version of it, similar to the existing `map` methods in the
/// standard library.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=3);
/// let stream = stream.map(|x| x + 3);
///
/// assert_eq!(vec![4, 5, 6], stream.collect::<Vec<_>>().await);
/// # });
/// ```
fn map<T, F>(self, f: F) -> Map<Self, F>
where
F: FnMut(Self::Item) -> T,
Self: Sized,
{
assert_stream::<T, _>(Map::new(self, f))
}
/// Creates a stream which gives the current iteration count as well as
/// the next value.
///
/// The stream returned yields pairs `(i, val)`, where `i` is the
/// current index of iteration and `val` is the value returned by the
/// stream.
///
/// `enumerate()` keeps its count as a [`usize`]. If you want to count by a
/// different sized integer, the [`zip`](StreamExt::zip) function provides similar
/// functionality.
///
/// # Overflow Behavior
///
/// The method does no guarding against overflows, so enumerating more than
/// [`prim@usize::max_value()`] elements either produces the wrong result or panics. If
/// debug assertions are enabled, a panic is guaranteed.
///
/// # Panics
///
/// The returned stream might panic if the to-be-returned index would
/// overflow a [`usize`].
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(vec!['a', 'b', 'c']);
///
/// let mut stream = stream.enumerate();
///
/// assert_eq!(stream.next().await, Some((0, 'a')));
/// assert_eq!(stream.next().await, Some((1, 'b')));
/// assert_eq!(stream.next().await, Some((2, 'c')));
/// assert_eq!(stream.next().await, None);
/// # });
/// ```
fn enumerate(self) -> Enumerate<Self>
where
Self: Sized,
{
assert_stream::<(usize, Self::Item), _>(Enumerate::new(self))
}
/// Filters the values produced by this stream according to the provided
/// asynchronous predicate.
///
/// As values of this stream are made available, the provided predicate `f`
/// will be run against them. If the predicate returns a `Future` which
/// resolves to `true`, then the stream will yield the value, but if the
/// predicate returns a `Future` which resolves to `false`, then the value
/// will be discarded and the next value will be produced.
///
/// Note that this function consumes the stream passed into it and returns a
/// wrapped version of it, similar to the existing `filter` methods in the
/// standard library.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::future;
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=10);
/// let evens = stream.filter(|x| future::ready(x % 2 == 0));
///
/// assert_eq!(vec![2, 4, 6, 8, 10], evens.collect::<Vec<_>>().await);
/// # });
/// ```
fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
where
F: FnMut(&Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
{
assert_stream::<Self::Item, _>(Filter::new(self, f))
}
/// Filters the values produced by this stream while simultaneously mapping
/// them to a different type according to the provided asynchronous closure.
///
/// As values of this stream are made available, the provided function will
/// be run on them. If the future returned by the predicate `f` resolves to
/// [`Some(item)`](Some) then the stream will yield the value `item`, but if
/// it resolves to [`None`] then the next value will be produced.
///
/// Note that this function consumes the stream passed into it and returns a
/// wrapped version of it, similar to the existing `filter_map` methods in
/// the standard library.
///
/// # Examples
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=10);
/// let evens = stream.filter_map(|x| async move {
/// if x % 2 == 0 { Some(x + 1) } else { None }
/// });
///
/// assert_eq!(vec![3, 5, 7, 9, 11], evens.collect::<Vec<_>>().await);
/// # });
/// ```
fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
where
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = Option<T>>,
Self: Sized,
{
assert_stream::<T, _>(FilterMap::new(self, f))
}
/// Computes from this stream's items new items of a different type using
/// an asynchronous closure.
///
/// The provided closure `f` will be called with an `Item` once a value is
/// ready, it returns a future which will then be run to completion
/// to produce the next value on this stream.
///
/// Note that this function consumes the stream passed into it and returns a
/// wrapped version of it.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=3);
/// let stream = stream.then(|x| async move { x + 3 });
///
/// assert_eq!(vec![4, 5, 6], stream.collect::<Vec<_>>().await);
/// # });
/// ```
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
where
F: FnMut(Self::Item) -> Fut,
Fut: Future,
Self: Sized,
{
assert_stream::<Fut::Output, _>(Then::new(self, f))
}
/// Transforms a stream into a collection, returning a
/// future representing the result of that computation.
///
/// The returned future will be resolved when the stream terminates.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::channel::mpsc;
/// use futures::stream::StreamExt;
/// use std::thread;
///
/// let (tx, rx) = mpsc::unbounded();
///
/// thread::spawn(move || {
/// for i in 1..=5 {
/// tx.unbounded_send(i).unwrap();
/// }
/// });
///
/// let output = rx.collect::<Vec<i32>>().await;
/// assert_eq!(output, vec![1, 2, 3, 4, 5]);
/// # });
/// ```
fn collect<C: Default + Extend<Self::Item>>(self) -> Collect<Self, C>
where
Self: Sized,
{
assert_future::<C, _>(Collect::new(self))
}
/// Converts a stream of pairs into a future, which
/// resolves to pair of containers.
///
/// `unzip()` produces a future, which resolves to two
/// collections: one from the left elements of the pairs,
/// and one from the right elements.
///
/// The returned future will be resolved when the stream terminates.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::channel::mpsc;
/// use futures::stream::StreamExt;
/// use std::thread;
///
/// let (tx, rx) = mpsc::unbounded();
///
/// thread::spawn(move || {
/// tx.unbounded_send((1, 2)).unwrap();
/// tx.unbounded_send((3, 4)).unwrap();
/// tx.unbounded_send((5, 6)).unwrap();
/// });
///
/// let (o1, o2): (Vec<_>, Vec<_>) = rx.unzip().await;
/// assert_eq!(o1, vec![1, 3, 5]);
/// assert_eq!(o2, vec![2, 4, 6]);
/// # });
/// ```
fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB>
where
FromA: Default + Extend<A>,
FromB: Default + Extend<B>,
Self: Sized + Stream<Item = (A, B)>,
{
assert_future::<(FromA, FromB), _>(Unzip::new(self))
}
/// Concatenate all items of a stream into a single extendable
/// destination, returning a future representing the end result.
///
/// This combinator will extend the first item with the contents
/// of all the subsequent results of the stream. If the stream is
/// empty, the default value will be returned.
///
/// Works with all collections that implement the
/// [`Extend`](std::iter::Extend) trait.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::channel::mpsc;
/// use futures::stream::StreamExt;
/// use std::thread;
///
/// let (tx, rx) = mpsc::unbounded();
///
/// thread::spawn(move || {
/// for i in (0..3).rev() {
/// let n = i * 3;
/// tx.unbounded_send(vec![n + 1, n + 2, n + 3]).unwrap();
/// }
/// });
///
/// let result = rx.concat().await;
///
/// assert_eq!(result, vec![7, 8, 9, 4, 5, 6, 1, 2, 3]);
/// # });
/// ```
fn concat(self) -> Concat<Self>
where
Self: Sized,
Self::Item: Extend<<<Self as Stream>::Item as IntoIterator>::Item> + IntoIterator + Default,
{
assert_future::<Self::Item, _>(Concat::new(self))
}
/// Repeats a stream endlessly.
///
/// The stream never terminates. Note that you likely want to avoid
/// usage of `collect` or such on the returned stream as it will exhaust
/// available memory as it tries to just fill up all RAM.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
/// let a = [1, 2, 3];
/// let mut s = stream::iter(a.iter()).cycle();
///
/// assert_eq!(s.next().await, Some(&1));
/// assert_eq!(s.next().await, Some(&2));
/// assert_eq!(s.next().await, Some(&3));
/// assert_eq!(s.next().await, Some(&1));
/// assert_eq!(s.next().await, Some(&2));
/// assert_eq!(s.next().await, Some(&3));
/// assert_eq!(s.next().await, Some(&1));
/// # });
/// ```
fn cycle(self) -> Cycle<Self>
where
Self: Sized + Clone,
{
assert_stream::<Self::Item, _>(Cycle::new(self))
}
/// Execute an accumulating asynchronous computation over a stream,
/// collecting all the values into one final result.
///
/// This combinator will accumulate all values returned by this stream
/// according to the closure provided. The initial state is also provided to
/// this method and then is returned again by each execution of the closure.
/// Once the entire stream has been exhausted the returned future will
/// resolve to this value.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let number_stream = stream::iter(0..6);
/// let sum = number_stream.fold(0, |acc, x| async move { acc + x });
/// assert_eq!(sum.await, 15);
/// # });
/// ```
fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F>
where
F: FnMut(T, Self::Item) -> Fut,
Fut: Future<Output = T>,
Self: Sized,
{
assert_future::<T, _>(Fold::new(self, f, init))
}
/// Execute predicate over asynchronous stream, and return `true` if any element in stream satisfied a predicate.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let number_stream = stream::iter(0..10);
/// let contain_three = number_stream.any(|i| async move { i == 3 });
/// assert_eq!(contain_three.await, true);
/// # });
/// ```
fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F>
where
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
{
assert_future::<bool, _>(Any::new(self, f))
}
/// Execute predicate over asynchronous stream, and return `true` if all element in stream satisfied a predicate.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let number_stream = stream::iter(0..10);
/// let less_then_twenty = number_stream.all(|i| async move { i < 20 });
/// assert_eq!(less_then_twenty.await, true);
/// # });
/// ```
fn all<Fut, F>(self, f: F) -> All<Self, Fut, F>
where
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
{
assert_future::<bool, _>(All::new(self, f))
}
/// Flattens a stream of streams into just one continuous stream.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::channel::mpsc;
/// use futures::stream::StreamExt;
/// use std::thread;
///
/// let (tx1, rx1) = mpsc::unbounded();
/// let (tx2, rx2) = mpsc::unbounded();
/// let (tx3, rx3) = mpsc::unbounded();
///
/// thread::spawn(move || {
/// tx1.unbounded_send(1).unwrap();
/// tx1.unbounded_send(2).unwrap();
/// });
/// thread::spawn(move || {
/// tx2.unbounded_send(3).unwrap();
/// tx2.unbounded_send(4).unwrap();
/// });
/// thread::spawn(move || {
/// tx3.unbounded_send(rx1).unwrap();
/// tx3.unbounded_send(rx2).unwrap();
/// });
///
/// let output = rx3.flatten().collect::<Vec<i32>>().await;
/// assert_eq!(output, vec![1, 2, 3, 4]);
/// # });
/// ```
fn flatten(self) -> Flatten<Self>
where
Self::Item: Stream,
Self: Sized,
{
assert_stream::<<Self::Item as Stream>::Item, _>(Flatten::new(self))
}
/// Maps a stream like [`StreamExt::map`] but flattens nested `Stream`s.
///
/// [`StreamExt::map`] is very useful, but if it produces a `Stream` instead,
/// you would have to chain combinators like `.map(f).flatten()` while this
/// combinator provides ability to write `.flat_map(f)` instead of chaining.
///
/// The provided closure which produce inner streams is executed over all elements
/// of stream as last inner stream is terminated and next stream item is available.
///
/// Note that this function consumes the stream passed into it and returns a
/// wrapped version of it, similar to the existing `flat_map` methods in the
/// standard library.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=3);
/// let stream = stream.flat_map(|x| stream::iter(vec![x + 3; x]));
///
/// assert_eq!(vec![4, 5, 5, 6, 6, 6], stream.collect::<Vec<_>>().await);
/// # });
/// ```
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
where
F: FnMut(Self::Item) -> U,
U: Stream,
Self: Sized,
{
assert_stream::<U::Item, _>(FlatMap::new(self, f))
}
/// Combinator similar to [`StreamExt::fold`] that holds internal state
/// and produces a new stream.
///
/// Accepts initial state and closure which will be applied to each element
/// of the stream until provided closure returns `None`. Once `None` is
/// returned, stream will be terminated.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::future;
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=10);
///
/// let stream = stream.scan(0, |state, x| {
/// *state += x;
/// future::ready(if *state < 10 { Some(x) } else { None })
/// });
///
/// assert_eq!(vec![1, 2, 3], stream.collect::<Vec<_>>().await);
/// # });
/// ```
fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
where
F: FnMut(&mut S, Self::Item) -> Fut,
Fut: Future<Output = Option<B>>,
Self: Sized,
{
assert_stream::<B, _>(Scan::new(self, initial_state, f))
}
/// Skip elements on this stream while the provided asynchronous predicate
/// resolves to `true`.
///
/// This function, like `Iterator::skip_while`, will skip elements on the
/// stream until the predicate `f` resolves to `false`. Once one element
/// returns `false`, all future elements will be returned from the underlying
/// stream.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::future;
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=10);
///
/// let stream = stream.skip_while(|x| future::ready(*x <= 5));
///
/// assert_eq!(vec![6, 7, 8, 9, 10], stream.collect::<Vec<_>>().await);
/// # });
/// ```
fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
where
F: FnMut(&Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
{
assert_stream::<Self::Item, _>(SkipWhile::new(self, f))
}
/// Take elements from this stream while the provided asynchronous predicate
/// resolves to `true`.
///
/// This function, like `Iterator::take_while`, will take elements from the
/// stream until the predicate `f` resolves to `false`. Once one element
/// returns `false`, it will always return that the stream is done.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::future;
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=10);
///
/// let stream = stream.take_while(|x| future::ready(*x <= 5));
///
/// assert_eq!(vec![1, 2, 3, 4, 5], stream.collect::<Vec<_>>().await);
/// # });
/// ```
fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
where
F: FnMut(&Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
{
assert_stream::<Self::Item, _>(TakeWhile::new(self, f))
}
/// Take elements from this stream until the provided future resolves.
///
/// This function will take elements from the stream until the provided
/// stopping future `fut` resolves. Once the `fut` future becomes ready,
/// this stream combinator will always return that the stream is done.
///
/// The stopping future may return any type. Once the stream is stopped
/// the result of the stopping future may be accessed with `TakeUntil::take_result()`.
/// The stream may also be resumed with `TakeUntil::take_future()`.
/// See the documentation of [`TakeUntil`] for more information.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::future;
/// use futures::stream::{self, StreamExt};
/// use futures::task::Poll;
///
/// let stream = stream::iter(1..=10);
///
/// let mut i = 0;
/// let stop_fut = future::poll_fn(|_cx| {
/// i += 1;
/// if i <= 5 {
/// Poll::Pending
/// } else {
/// Poll::Ready(())
/// }
/// });
///
/// let stream = stream.take_until(stop_fut);
///
/// assert_eq!(vec![1, 2, 3, 4, 5], stream.collect::<Vec<_>>().await);
/// # });
/// ```
fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
where
Fut: Future,
Self: Sized,
{
assert_stream::<Self::Item, _>(TakeUntil::new(self, fut))
}
/// Runs this stream to completion, executing the provided asynchronous
/// closure for each element on the stream.
///
/// The closure provided will be called for each item this stream produces,
/// yielding a future. That future will then be executed to completion
/// before moving on to the next item.
///
/// The returned value is a `Future` where the `Output` type is `()`; it is
/// executed entirely for its side effects.
///
/// To process each item in the stream and produce another stream instead
/// of a single future, use `then` instead.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::future;
/// use futures::stream::{self, StreamExt};
///
/// let mut x = 0;
///
/// {
/// let fut = stream::repeat(1).take(3).for_each(|item| {
/// x += item;
/// future::ready(())
/// });
/// fut.await;
/// }
///
/// assert_eq!(x, 3);
/// # });
/// ```
fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F>
where
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = ()>,
Self: Sized,
{
assert_future::<(), _>(ForEach::new(self, f))
}
/// Runs this stream to completion, executing the provided asynchronous
/// closure for each element on the stream concurrently as elements become
/// available.
///
/// This is similar to [`StreamExt::for_each`], but the futures
/// produced by the closure are run concurrently (but not in parallel--
/// this combinator does not introduce any threads).
///
/// The closure provided will be called for each item this stream produces,
/// yielding a future. That future will then be executed to completion
/// concurrently with the other futures produced by the closure.
///
/// The first argument is an optional limit on the number of concurrent
/// futures. If this limit is not `None`, no more than `limit` futures
/// will be run concurrently. The `limit` argument is of type
/// `Into<Option<usize>>`, and so can be provided as either `None`,
/// `Some(10)`, or just `10`. Note: a limit of zero is interpreted as
/// no limit at all, and will have the same result as passing in `None`.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::channel::oneshot;
/// use futures::stream::{self, StreamExt};
///
/// let (tx1, rx1) = oneshot::channel();
/// let (tx2, rx2) = oneshot::channel();
/// let (tx3, rx3) = oneshot::channel();
///
/// let fut = stream::iter(vec![rx1, rx2, rx3]).for_each_concurrent(
/// /* limit */ 2,
/// |rx| async move {
/// rx.await.unwrap();
/// }
/// );
/// tx1.send(()).unwrap();
/// tx2.send(()).unwrap();
/// tx3.send(()).unwrap();
/// fut.await;
/// # })
/// ```
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
fn for_each_concurrent<Fut, F>(
self,
limit: impl Into<Option<usize>>,
f: F,
) -> ForEachConcurrent<Self, Fut, F>
where
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = ()>,
Self: Sized,
{
assert_future::<(), _>(ForEachConcurrent::new(self, limit.into(), f))
}
/// Creates a new stream of at most `n` items of the underlying stream.
///
/// Once `n` items have been yielded from this stream then it will always
/// return that the stream is done.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=10).take(3);
///
/// assert_eq!(vec![1, 2, 3], stream.collect::<Vec<_>>().await);
/// # });
/// ```
fn take(self, n: usize) -> Take<Self>
where
Self: Sized,
{
assert_stream::<Self::Item, _>(Take::new(self, n))
}
/// Creates a new stream which skips `n` items of the underlying stream.
///
/// Once `n` items have been skipped from this stream then it will always
/// return the remaining items on this stream.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(1..=10).skip(5);
///
/// assert_eq!(vec![6, 7, 8, 9, 10], stream.collect::<Vec<_>>().await);
/// # });
/// ```
fn skip(self, n: usize) -> Skip<Self>
where
Self: Sized,
{
assert_stream::<Self::Item, _>(Skip::new(self, n))
}
/// Fuse a stream such that [`poll_next`](Stream::poll_next) will never
/// again be called once it has finished. This method can be used to turn
/// any `Stream` into a `FusedStream`.
///
/// Normally, once a stream has returned [`None`] from
/// [`poll_next`](Stream::poll_next) any further calls could exhibit bad
/// behavior such as block forever, panic, never return, etc. If it is known
/// that [`poll_next`](Stream::poll_next) may be called after stream
/// has already finished, then this method can be used to ensure that it has
/// defined semantics.
///
/// The [`poll_next`](Stream::poll_next) method of a `fuse`d stream
/// is guaranteed to return [`None`] after the underlying stream has
/// finished.
///
/// # Examples
///
/// ```
/// use futures::executor::block_on_stream;
/// use futures::stream::{self, StreamExt};
/// use futures::task::Poll;
///
/// let mut x = 0;
/// let stream = stream::poll_fn(|_| {
/// x += 1;
/// match x {
/// 0..=2 => Poll::Ready(Some(x)),
/// 3 => Poll::Ready(None),
/// _ => panic!("should not happen")
/// }
/// }).fuse();
///
/// let mut iter = block_on_stream(stream);
/// assert_eq!(Some(1), iter.next());
/// assert_eq!(Some(2), iter.next());
/// assert_eq!(None, iter.next());
/// assert_eq!(None, iter.next());
/// // ...
/// ```
fn fuse(self) -> Fuse<Self>
where
Self: Sized,
{
assert_stream::<Self::Item, _>(Fuse::new(self))
}
/// Borrows a stream, rather than consuming it.
///
/// This is useful to allow applying stream adaptors while still retaining
/// ownership of the original stream.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let mut stream = stream::iter(1..5);
///
/// let sum = stream.by_ref()
/// .take(2)
/// .fold(0, |a, b| async move { a + b })
/// .await;
/// assert_eq!(sum, 3);
///
/// // You can use the stream again
/// let sum = stream.take(2)
/// .fold(0, |a, b| async move { a + b })
/// .await;
/// assert_eq!(sum, 7);
/// # });
/// ```
fn by_ref(&mut self) -> &mut Self {
self
}
/// Catches unwinding panics while polling the stream.
///
/// Caught panic (if any) will be the last element of the resulting stream.
///
/// In general, panics within a stream can propagate all the way out to the
/// task level. This combinator makes it possible to halt unwinding within
/// the stream itself. It's most commonly used within task executors. This
/// method should not be used for error handling.
///
/// Note that this method requires the `UnwindSafe` bound from the standard
/// library. This isn't always applied automatically, and the standard
/// library provides an `AssertUnwindSafe` wrapper type to apply it
/// after-the fact. To assist using this method, the [`Stream`] trait is
/// also implemented for `AssertUnwindSafe<St>` where `St` implements
/// [`Stream`].
///
/// This method is only available when the `std` feature of this
/// library is activated, and it is activated by default.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream = stream::iter(vec![Some(10), None, Some(11)]);
/// // Panic on second element
/// let stream_panicking = stream.map(|o| o.unwrap());
/// // Collect all the results
/// let stream = stream_panicking.catch_unwind();
///
/// let results: Vec<Result<i32, _>> = stream.collect().await;
/// match results[0] {
/// Ok(10) => {}
/// _ => panic!("unexpected result!"),
/// }
/// assert!(results[1].is_err());
/// assert_eq!(results.len(), 2);
/// # });
/// ```
#[cfg(feature = "std")]
fn catch_unwind(self) -> CatchUnwind<Self>
where
Self: Sized + std::panic::UnwindSafe,
{
assert_stream(CatchUnwind::new(self))
}
/// Wrap the stream in a Box, pinning it.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
#[cfg(feature = "alloc")]
fn boxed<'a>(self) -> BoxStream<'a, Self::Item>
where
Self: Sized + Send + 'a,
{
assert_stream::<Self::Item, _>(Box::pin(self))
}
/// Wrap the stream in a Box, pinning it.
///
/// Similar to `boxed`, but without the `Send` requirement.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
#[cfg(feature = "alloc")]
fn boxed_local<'a>(self) -> LocalBoxStream<'a, Self::Item>
where
Self: Sized + 'a,
{
assert_stream::<Self::Item, _>(Box::pin(self))
}
/// An adaptor for creating a buffered list of pending futures.
///
/// If this stream's item can be converted into a future, then this adaptor
/// will buffer up to at most `n` futures and then return the outputs in the
/// same order as the underlying stream. No more than `n` futures will be
/// buffered at any point in time, and less than `n` may also be buffered
/// depending on the state of each future.
///
/// The returned stream will be a stream of each future's output.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
fn buffered(self, n: usize) -> Buffered<Self>
where
Self::Item: Future,
Self: Sized,
{
assert_stream::<<Self::Item as Future>::Output, _>(Buffered::new(self, n))
}
/// An adaptor for creating a buffered list of pending futures (unordered).
///
/// If this stream's item can be converted into a future, then this adaptor
/// will buffer up to `n` futures and then return the outputs in the order
/// in which they complete. No more than `n` futures will be buffered at
/// any point in time, and less than `n` may also be buffered depending on
/// the state of each future.
///
/// The returned stream will be a stream of each future's output.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::channel::oneshot;
/// use futures::stream::{self, StreamExt};
///
/// let (send_one, recv_one) = oneshot::channel();
/// let (send_two, recv_two) = oneshot::channel();
///
/// let stream_of_futures = stream::iter(vec![recv_one, recv_two]);
/// let mut buffered = stream_of_futures.buffer_unordered(10);
///
/// send_two.send(2i32)?;
/// assert_eq!(buffered.next().await, Some(Ok(2i32)));
///
/// send_one.send(1i32)?;
/// assert_eq!(buffered.next().await, Some(Ok(1i32)));
///
/// assert_eq!(buffered.next().await, None);
/// # Ok::<(), i32>(()) }).unwrap();
/// ```
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
fn buffer_unordered(self, n: usize) -> BufferUnordered<Self>
where
Self::Item: Future,
Self: Sized,
{
assert_stream::<<Self::Item as Future>::Output, _>(BufferUnordered::new(self, n))
}
/// An adapter for zipping two streams together.
///
/// The zipped stream waits for both streams to produce an item, and then
/// returns that pair. If either stream ends then the zipped stream will
/// also end.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream1 = stream::iter(1..=3);
/// let stream2 = stream::iter(5..=10);
///
/// let vec = stream1.zip(stream2)
/// .collect::<Vec<_>>()
/// .await;
/// assert_eq!(vec![(1, 5), (2, 6), (3, 7)], vec);
/// # });
/// ```
///
fn zip<St>(self, other: St) -> Zip<Self, St>
where
St: Stream,
Self: Sized,
{
assert_stream::<(Self::Item, St::Item), _>(Zip::new(self, other))
}
/// Adapter for chaining two streams.
///
/// The resulting stream emits elements from the first stream, and when
/// first stream reaches the end, emits the elements from the second stream.
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::stream::{self, StreamExt};
///
/// let stream1 = stream::iter(vec![Ok(10), Err(false)]);
/// let stream2 = stream::iter(vec![Err(true), Ok(20)]);
///
/// let stream = stream1.chain(stream2);
///
/// let result: Vec<_> = stream.collect().await;
/// assert_eq!(result, vec![
/// Ok(10),
/// Err(false),
/// Err(true),
/// Ok(20),
/// ]);
/// # });
/// ```
fn chain<St>(self, other: St) -> Chain<Self, St>
where
St: Stream<Item = Self::Item>,
Self: Sized,
{
assert_stream::<Self::Item, _>(Chain::new(self, other))
}
/// Creates a new stream which exposes a `peek` method.
///
/// Calling `peek` returns a reference to the next item in the stream.
fn peekable(self) -> Peekable<Self>
where
Self: Sized,
{
assert_stream::<Self::Item, _>(Peekable::new(self))
}
/// An adaptor for chunking up items of the stream inside a vector.
///
/// This combinator will attempt to pull items from this stream and buffer
/// them into a local vector. At most `capacity` items will get buffered
/// before they're yielded from the returned stream.
///
/// Note that the vectors returned from this iterator may not always have
/// `capacity` elements. If the underlying stream ended and only a partial
/// vector was created, it'll be returned. Additionally if an error happens
/// from the underlying stream then the currently buffered items will be
/// yielded.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
///
/// # Panics
///
/// This method will panic if `capacity` is zero.
#[cfg(feature = "alloc")]
fn chunks(self, capacity: usize) -> Chunks<Self>
where
Self: Sized,
{
assert_stream::<Vec<Self::Item>, _>(Chunks::new(self, capacity))
}
/// An adaptor for chunking up ready items of the stream inside a vector.
///
/// This combinator will attempt to pull ready items from this stream and
/// buffer them into a local vector. At most `capacity` items will get
/// buffered before they're yielded from the returned stream. If underlying
/// stream returns `Poll::Pending`, and collected chunk is not empty, it will
/// be immediately returned.
///
/// If the underlying stream ended and only a partial vector was created,
/// it'll be returned. Additionally if an error happens from the underlying
/// stream then the currently buffered items will be yielded.
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
///
/// # Panics
///
/// This method will panic if `capacity` is zero.
#[cfg(feature = "alloc")]
fn ready_chunks(self, capacity: usize) -> ReadyChunks<Self>
where
Self: Sized,
{
assert_stream::<Vec<Self::Item>, _>(ReadyChunks::new(self, capacity))
}
/// A future that completes after the given stream has been fully processed
/// into the sink and the sink has been flushed and closed.
///
/// This future will drive the stream to keep producing items until it is
/// exhausted, sending each item to the sink. It will complete once the
/// stream is exhausted, the sink has received and flushed all items, and
/// the sink is closed. Note that neither the original stream nor provided
/// sink will be output by this future. Pass the sink by `Pin<&mut S>`
/// (for example, via `forward(&mut sink)` inside an `async` fn/block) in
/// order to preserve access to the `Sink`. If the stream produces an error,
/// that error will be returned by this future without flushing/closing the sink.
#[cfg(feature = "sink")]
#[cfg_attr(docsrs, doc(cfg(feature = "sink")))]
fn forward<S>(self, sink: S) -> Forward<Self, S>
where
S: Sink<Self::Ok, Error = Self::Error>,
Self: TryStream + Sized,
// Self: TryStream + Sized + Stream<Item = Result<<Self as TryStream>::Ok, <Self as TryStream>::Error>>,
{
// TODO: type mismatch resolving `<Self as futures_core::Stream>::Item == std::result::Result<<Self as futures_core::TryStream>::Ok, <Self as futures_core::TryStream>::Error>`
// assert_future::<Result<(), Self::Error>, _>(Forward::new(self, sink))
Forward::new(self, sink)
}
/// Splits this `Stream + Sink` object into separate `Sink` and `Stream`
/// objects.
///
/// This can be useful when you want to split ownership between tasks, or
/// allow direct interaction between the two objects (e.g. via
/// `Sink::send_all`).
///
/// This method is only available when the `std` or `alloc` feature of this
/// library is activated, and it is activated by default.
#[cfg(feature = "sink")]
#[cfg_attr(docsrs, doc(cfg(feature = "sink")))]
#[cfg(not(futures_no_atomic_cas))]
#[cfg(feature = "alloc")]
fn split<Item>(self) -> (SplitSink<Self, Item>, SplitStream<Self>)
where
Self: Sink<Item> + Sized,
{
let (sink, stream) = split::split(self);
(
crate::sink::assert_sink::<Item, Self::Error, _>(sink),
assert_stream::<Self::Item, _>(stream),
)
}
/// Do something with each item of this stream, afterwards passing it on.
///
/// This is similar to the `Iterator::inspect` method in the standard
/// library where it allows easily inspecting each value as it passes
/// through the stream, for example to debug what's going on.
fn inspect<F>(self, f: F) -> Inspect<Self, F>
where
F: FnMut(&Self::Item),
Self: Sized,
{
assert_stream::<Self::Item, _>(Inspect::new(self, f))
}
/// Wrap this stream in an `Either` stream, making it the left-hand variant
/// of that `Either`.
///
/// This can be used in combination with the `right_stream` method to write `if`
/// statements that evaluate to different streams in different branches.
fn left_stream<B>(self) -> Either<Self, B>
where
B: Stream<Item = Self::Item>,
Self: Sized,
{
assert_stream::<Self::Item, _>(Either::Left(self))
}
/// Wrap this stream in an `Either` stream, making it the right-hand variant
/// of that `Either`.
///
/// This can be used in combination with the `left_stream` method to write `if`
/// statements that evaluate to different streams in different branches.
fn right_stream<B>(self) -> Either<B, Self>
where
B: Stream<Item = Self::Item>,
Self: Sized,
{
assert_stream::<Self::Item, _>(Either::Right(self))
}
/// A convenience method for calling [`Stream::poll_next`] on [`Unpin`]
/// stream types.
fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>
where
Self: Unpin,
{
Pin::new(self).poll_next(cx)
}
/// Returns a [`Future`] that resolves when the next item in this stream is
/// ready.
///
/// This is similar to the [`next`][StreamExt::next] method, but it won't
/// resolve to [`None`] if used on an empty [`Stream`]. Instead, the
/// returned future type will return `true` from
/// [`FusedFuture::is_terminated`][] when the [`Stream`] is empty, allowing
/// [`select_next_some`][StreamExt::select_next_some] to be easily used with
/// the [`select!`] macro.
///
/// If the future is polled after this [`Stream`] is empty it will panic.
/// Using the future with a [`FusedFuture`][]-aware primitive like the
/// [`select!`] macro will prevent this.
///
/// [`FusedFuture`]: futures_core::future::FusedFuture
/// [`FusedFuture::is_terminated`]: futures_core::future::FusedFuture::is_terminated
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::{future, select};
/// use futures::stream::{StreamExt, FuturesUnordered};
///
/// let mut fut = future::ready(1);
/// let mut async_tasks = FuturesUnordered::new();
/// let mut total = 0;
/// loop {
/// select! {
/// num = fut => {
/// // First, the `ready` future completes.
/// total += num;
/// // Then we spawn a new task onto `async_tasks`,
/// async_tasks.push(async { 5 });
/// },
/// // On the next iteration of the loop, the task we spawned
/// // completes.
/// num = async_tasks.select_next_some() => {
/// total += num;
/// }
/// // Finally, both the `ready` future and `async_tasks` have
/// // finished, so we enter the `complete` branch.
/// complete => break,
/// }
/// }
/// assert_eq!(total, 6);
/// # });
/// ```
fn select_next_some(&mut self) -> SelectNextSome<'_, Self>
where
Self: Unpin + FusedStream,
{
assert_future::<Self::Item, _>(SelectNextSome::new(self))
}
}