-
Notifications
You must be signed in to change notification settings - Fork 336
/
mod.rs
2310 lines (1888 loc) · 57.2 KB
/
mod.rs
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
//! Asynchronous iteration.
//!
//! This module is an async version of [`std::iter`].
//!
//! [`std::iter`]: https://doc.rust-lang.org/std/iter/index.html
//!
//! # Examples
//!
//! ```
//! # async_std::task::block_on(async {
//! #
//! use async_std::prelude::*;
//! use async_std::stream;
//!
//! let mut s = stream::repeat(9).take(3);
//!
//! while let Some(v) = s.next().await {
//! assert_eq!(v, 9);
//! }
//! #
//! # })
//! ```
mod all;
mod any;
mod chain;
mod cloned;
mod cmp;
mod copied;
mod cycle;
mod enumerate;
mod eq;
mod filter;
mod filter_map;
mod find;
mod find_map;
mod fold;
mod for_each;
mod fuse;
mod ge;
mod gt;
mod inspect;
mod last;
mod le;
mod lt;
mod map;
mod max;
mod max_by;
mod max_by_key;
mod min;
mod min_by;
mod min_by_key;
mod ne;
mod next;
mod nth;
mod partial_cmp;
mod position;
mod scan;
mod skip;
mod skip_while;
mod step_by;
mod take;
mod take_while;
mod try_fold;
mod try_for_each;
mod zip;
use all::AllFuture;
use any::AnyFuture;
use cmp::CmpFuture;
use cycle::Cycle;
use enumerate::Enumerate;
use eq::EqFuture;
use filter_map::FilterMap;
use find::FindFuture;
use find_map::FindMapFuture;
use fold::FoldFuture;
use for_each::ForEachFuture;
use ge::GeFuture;
use gt::GtFuture;
use last::LastFuture;
use le::LeFuture;
use lt::LtFuture;
use max::MaxFuture;
use max_by::MaxByFuture;
use max_by_key::MaxByKeyFuture;
use min::MinFuture;
use min_by::MinByFuture;
use min_by_key::MinByKeyFuture;
use ne::NeFuture;
use next::NextFuture;
use nth::NthFuture;
use partial_cmp::PartialCmpFuture;
use position::PositionFuture;
use try_fold::TryFoldFuture;
use try_for_each::TryForEachFuture;
pub use chain::Chain;
pub use cloned::Cloned;
pub use copied::Copied;
pub use filter::Filter;
pub use fuse::Fuse;
pub use inspect::Inspect;
pub use map::Map;
pub use scan::Scan;
pub use skip::Skip;
pub use skip_while::SkipWhile;
pub use step_by::StepBy;
pub use take::Take;
pub use take_while::TakeWhile;
pub use zip::Zip;
use core::cmp::Ordering;
cfg_unstable! {
use core::future::Future;
use core::pin::Pin;
use core::time::Duration;
use crate::stream::into_stream::IntoStream;
use crate::stream::{FromStream, Product, Sum};
use crate::stream::Extend;
use count::CountFuture;
use partition::PartitionFuture;
use unzip::UnzipFuture;
pub use merge::Merge;
pub use flatten::Flatten;
pub use flat_map::FlatMap;
pub use timeout::{TimeoutError, Timeout};
pub use throttle::Throttle;
pub use delay::Delay;
mod count;
mod merge;
mod flatten;
mod flat_map;
mod partition;
mod timeout;
mod throttle;
mod delay;
mod unzip;
}
pub use futures_core::stream::Stream as Stream;
#[doc = r#"
Extension methods for [`Stream`].
[`Stream`]: ../stream/trait.Stream.html
"#]
pub trait StreamExt: Stream {
#[doc = r#"
Advances the stream and returns the next value.
Returns [`None`] when iteration is finished. Individual stream implementations may
choose to resume iteration, and so calling `next()` again may or may not eventually
start returning more values.
[`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let mut s = stream::once(7);
assert_eq!(s.next().await, Some(7));
assert_eq!(s.next().await, None);
#
# }) }
```
"#]
fn next(&mut self) -> NextFuture<'_, Self>
where
Self: Unpin,
{
NextFuture { stream: self }
}
#[doc = r#"
Creates a stream that yields its first `n` elements.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let mut s = stream::repeat(9).take(3);
while let Some(v) = s.next().await {
assert_eq!(v, 9);
}
#
# }) }
```
"#]
fn take(self, n: usize) -> Take<Self>
where
Self: Sized,
{
Take::new(self, n)
}
#[doc = r#"
Creates a stream that yields elements based on a predicate.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![1, 2, 3, 4]);
let mut s = s.take_while(|x| x < &3 );
assert_eq!(s.next().await, Some(1));
assert_eq!(s.next().await, Some(2));
assert_eq!(s.next().await, None);
#
# }) }
```
"#]
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
{
TakeWhile::new(self, predicate)
}
#[doc = r#"
Limit the amount of items yielded per timeslice in a stream.
This stream does not drop any items, but will only limit the rate at which items pass through.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
use std::time::{Duration, Instant};
let start = Instant::now();
// emit value every 5 milliseconds
let s = stream::interval(Duration::from_millis(5)).take(2);
// throttle for 10 milliseconds
let mut s = s.throttle(Duration::from_millis(10));
s.next().await;
assert!(start.elapsed().as_millis() >= 5);
s.next().await;
assert!(start.elapsed().as_millis() >= 15);
s.next().await;
assert!(start.elapsed().as_millis() >= 25);
#
# }) }
```
"#]
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn throttle(self, d: Duration) -> Throttle<Self>
where
Self: Sized,
{
Throttle::new(self, d)
}
#[doc = r#"
Creates a stream that yields each `step`th element.
# Panics
This method will panic if the given step is `0`.
# Examples
Basic usage:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![0u8, 1, 2, 3, 4]);
let mut stepped = s.step_by(2);
assert_eq!(stepped.next().await, Some(0));
assert_eq!(stepped.next().await, Some(2));
assert_eq!(stepped.next().await, Some(4));
assert_eq!(stepped.next().await, None);
#
# }) }
```
"#]
fn step_by(self, step: usize) -> StepBy<Self>
where
Self: Sized,
{
StepBy::new(self, step)
}
#[doc = r#"
Takes two streams and creates a new stream over both in sequence.
# Examples
Basic usage:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let first = stream::from_iter(vec![0u8, 1]);
let second = stream::from_iter(vec![2, 3]);
let mut c = first.chain(second);
assert_eq!(c.next().await, Some(0));
assert_eq!(c.next().await, Some(1));
assert_eq!(c.next().await, Some(2));
assert_eq!(c.next().await, Some(3));
assert_eq!(c.next().await, None);
#
# }) }
```
"#]
fn chain<U>(self, other: U) -> Chain<Self, U>
where
Self: Sized,
U: Stream<Item = Self::Item> + Sized,
{
Chain::new(self, other)
}
#[doc = r#"
Creates an stream which copies all of its elements.
# Examples
Basic usage:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let v = stream::from_iter(vec![&1, &2, &3]);
let mut v_cloned = v.cloned();
assert_eq!(v_cloned.next().await, Some(1));
assert_eq!(v_cloned.next().await, Some(2));
assert_eq!(v_cloned.next().await, Some(3));
assert_eq!(v_cloned.next().await, None);
#
# }) }
```
"#]
fn cloned<'a, T>(self) -> Cloned<Self>
where
Self: Sized + Stream<Item = &'a T>,
T: Clone + 'a,
{
Cloned::new(self)
}
#[doc = r#"
Creates an stream which copies all of its elements.
# Examples
Basic usage:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![&1, &2, &3]);
let mut s_copied = s.copied();
assert_eq!(s_copied.next().await, Some(1));
assert_eq!(s_copied.next().await, Some(2));
assert_eq!(s_copied.next().await, Some(3));
assert_eq!(s_copied.next().await, None);
#
# }) }
```
"#]
fn copied<'a, T>(self) -> Copied<Self>
where
Self: Sized + Stream<Item = &'a T>,
T: Copy + 'a,
{
Copied::new(self)
}
#[doc = r#"
Creates a stream that yields the provided values infinitely and in order.
# Examples
Basic usage:
```
# async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let mut s = stream::once(7).cycle();
assert_eq!(s.next().await, Some(7));
assert_eq!(s.next().await, Some(7));
assert_eq!(s.next().await, Some(7));
assert_eq!(s.next().await, Some(7));
assert_eq!(s.next().await, Some(7));
#
# })
```
"#]
fn cycle(self) -> Cycle<Self>
where
Self: Clone + Sized,
{
Cycle::new(self)
}
#[doc = r#"
Creates a stream that gives the current element's count as well as the next value.
# Overflow behaviour.
This combinator does no guarding against overflows.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec!['a', 'b', 'c']);
let mut s = s.enumerate();
assert_eq!(s.next().await, Some((0, 'a')));
assert_eq!(s.next().await, Some((1, 'b')));
assert_eq!(s.next().await, Some((2, 'c')));
assert_eq!(s.next().await, None);
#
# }) }
```
"#]
fn enumerate(self) -> Enumerate<Self>
where
Self: Sized,
{
Enumerate::new(self)
}
#[doc = r#"
Creates a stream that is delayed before it starts yielding items.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
use std::time::{Duration, Instant};
let start = Instant::now();
let mut s = stream::from_iter(vec![0u8, 1, 2]).delay(Duration::from_millis(200));
assert_eq!(s.next().await, Some(0));
// The first time will take more than 200ms due to delay.
assert!(start.elapsed().as_millis() >= 200);
assert_eq!(s.next().await, Some(1));
// There will be no delay after the first time.
assert!(start.elapsed().as_millis() < 400);
assert_eq!(s.next().await, Some(2));
assert!(start.elapsed().as_millis() < 400);
assert_eq!(s.next().await, None);
assert!(start.elapsed().as_millis() < 400);
#
# }) }
```
"#]
#[cfg(any(feature = "unstable", feature = "docs"))]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn delay(self, dur: std::time::Duration) -> Delay<Self>
where
Self: Sized,
{
Delay::new(self, dur)
}
#[doc = r#"
Takes a closure and creates a stream that calls that closure on every element of this stream.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![1, 2, 3]);
let mut s = s.map(|x| 2 * x);
assert_eq!(s.next().await, Some(2));
assert_eq!(s.next().await, Some(4));
assert_eq!(s.next().await, Some(6));
assert_eq!(s.next().await, None);
#
# }) }
```
"#]
fn map<B, F>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: FnMut(Self::Item) -> B,
{
Map::new(self, f)
}
#[doc = r#"
A combinator that does something with each element in the stream, passing the value
on.
# Examples
Basic usage:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![1, 2, 3, 4, 5]);
let sum = s
.inspect(|x| println!("about to filter {}", x))
.filter(|x| x % 2 == 0)
.inspect(|x| println!("made it through filter: {}", x))
.fold(0, |sum, i| sum + i)
.await;
assert_eq!(sum, 6);
#
# }) }
```
"#]
fn inspect<F>(self, f: F) -> Inspect<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item),
{
Inspect::new(self, f)
}
#[doc = r#"
Returns the last element of the stream.
# Examples
Basic usage:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![1, 2, 3]);
let last = s.last().await;
assert_eq!(last, Some(3));
#
# }) }
```
An empty stream will return `None`:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::stream;
use crate::async_std::prelude::*;
let s = stream::empty::<()>();
let last = s.last().await;
assert_eq!(last, None);
#
# }) }
```
"#]
fn last(
self,
) -> LastFuture<Self, Self::Item>
where
Self: Sized,
{
LastFuture::new(self)
}
#[doc = r#"
Creates a stream which ends after the first `None`.
After a stream returns `None`, future calls may or may not yield `Some(T)` again.
`fuse()` adapts an iterator, ensuring that after a `None` is given, it will always
return `None` forever.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let mut s = stream::once(1).fuse();
assert_eq!(s.next().await, Some(1));
assert_eq!(s.next().await, None);
assert_eq!(s.next().await, None);
#
# }) }
```
"#]
fn fuse(self) -> Fuse<Self>
where
Self: Sized,
{
Fuse::new(self)
}
#[doc = r#"
Creates a stream that uses a predicate to determine if an element should be yielded.
# Examples
Basic usage:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![1, 2, 3, 4]);
let mut s = s.filter(|i| i % 2 == 0);
assert_eq!(s.next().await, Some(2));
assert_eq!(s.next().await, Some(4));
assert_eq!(s.next().await, None);
#
# }) }
```
"#]
fn filter<P>(self, predicate: P) -> Filter<Self, P>
where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
{
Filter::new(self, predicate)
}
#[doc= r#"
Creates an stream that works like map, but flattens nested structure.
# Examples
Basic usage:
```
# async_std::task::block_on(async {
use async_std::prelude::*;
use async_std::stream;
let words = stream::from_iter(&["alpha", "beta", "gamma"]);
let merged: String = words
.flat_map(|s| stream::from_iter(s.chars()))
.collect().await;
assert_eq!(merged, "alphabetagamma");
let d3 = stream::from_iter(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
let d1: Vec<_> = d3
.flat_map(|item| stream::from_iter(item))
.flat_map(|item| stream::from_iter(item))
.collect().await;
assert_eq!(d1, [&1, &2, &3, &4, &5, &6, &7, &8]);
# });
```
"#]
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
where
Self: Sized,
U: IntoStream,
F: FnMut(Self::Item) -> U,
{
FlatMap::new(self, f)
}
#[doc = r#"
Creates an stream that flattens nested structure.
# Examples
Basic usage:
```
# async_std::task::block_on(async {
use async_std::prelude::*;
use async_std::stream;
let inner1 = stream::from_iter(vec![1u8,2,3]);
let inner2 = stream::from_iter(vec![4u8,5,6]);
let s = stream::from_iter(vec![inner1, inner2]);
let v: Vec<_> = s.flatten().collect().await;
assert_eq!(v, vec![1,2,3,4,5,6]);
# });
"#]
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn flatten(self) -> Flatten<Self>
where
Self: Sized,
Self::Item: IntoStream,
{
Flatten::new(self)
}
#[doc = r#"
Both filters and maps a stream.
# Examples
Basic usage:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec!["1", "lol", "3", "NaN", "5"]);
let mut parsed = s.filter_map(|a| a.parse::<u32>().ok());
let one = parsed.next().await;
assert_eq!(one, Some(1));
let three = parsed.next().await;
assert_eq!(three, Some(3));
let five = parsed.next().await;
assert_eq!(five, Some(5));
let end = parsed.next().await;
assert_eq!(end, None);
#
# }) }
```
"#]
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
where
Self: Sized,
F: FnMut(Self::Item) -> Option<B>,
{
FilterMap::new(self, f)
}
#[doc = r#"
Returns the element that gives the minimum value with respect to the
specified key function. If several elements are equally minimum,
the first element is returned. If the stream is empty, `None` is returned.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![-1isize, 2, -3]);
let min = s.clone().min_by_key(|x| x.abs()).await;
assert_eq!(min, Some(-1));
let min = stream::empty::<isize>().min_by_key(|x| x.abs()).await;
assert_eq!(min, None);
#
# }) }
```
"#]
fn min_by_key<B, F>(
self,
key_by: F,
) -> MinByKeyFuture<Self, Self::Item, F>
where
Self: Sized,
B: Ord,
F: FnMut(&Self::Item) -> B,
{
MinByKeyFuture::new(self, key_by)
}
#[doc = r#"
Returns the element that gives the maximum value with respect to the
specified key function. If several elements are equally maximum,
the first element is returned. If the stream is empty, `None` is returned.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![-3_i32, 0, 1, 5, -10]);
let max = s.clone().max_by_key(|x| x.abs()).await;
assert_eq!(max, Some(-10));
let max = stream::empty::<isize>().max_by_key(|x| x.abs()).await;
assert_eq!(max, None);
#
# }) }
```
"#]
fn max_by_key<B, F>(
self,
key_by: F,
) -> MaxByKeyFuture<Self, Self::Item, F>
where
Self: Sized,
B: Ord,
F: FnMut(&Self::Item) -> B,
{
MaxByKeyFuture::new(self, key_by)
}
#[doc = r#"
Returns the element that gives the minimum value with respect to the
specified comparison function. If several elements are equally minimum,
the first element is returned. If the stream is empty, `None` is returned.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![1u8, 2, 3]);
let min = s.clone().min_by(|x, y| x.cmp(y)).await;
assert_eq!(min, Some(1));
let min = s.min_by(|x, y| y.cmp(x)).await;
assert_eq!(min, Some(3));
let min = stream::empty::<u8>().min_by(|x, y| x.cmp(y)).await;
assert_eq!(min, None);
#
# }) }
```
"#]
fn min_by<F>(
self,
compare: F,
) -> MinByFuture<Self, F, Self::Item>
where
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
{
MinByFuture::new(self, compare)
}
#[doc = r#"
Returns the element that gives the maximum value. If several elements are equally maximum,
the first element is returned. If the stream is empty, `None` is returned.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![1usize, 2, 3]);
let max = s.clone().max().await;
assert_eq!(max, Some(3));
let max = stream::empty::<usize>().max().await;
assert_eq!(max, None);
#
# }) }
```
"#]
fn max(
self,
) -> MaxFuture<Self, Self::Item>
where
Self: Sized,
Self::Item: Ord,
{
MaxFuture::new(self)
}
#[doc = r#"
Returns the element that gives the minimum value. If several elements are equally minimum,
the first element is returned. If the stream is empty, `None` is returned.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let s = stream::from_iter(vec![1usize, 2, 3]);
let min = s.clone().min().await;
assert_eq!(min, Some(1));
let min = stream::empty::<usize>().min().await;
assert_eq!(min, None);
#
# }) }
```
"#]
fn min(
self,
) -> MinFuture<Self, Self::Item>
where
Self: Sized,
Self::Item: Ord,
{
MinFuture::new(self)
}
#[doc = r#"
Returns the element that gives the maximum value with respect to the
specified comparison function. If several elements are equally maximum,
the first element is returned. If the stream is empty, `None` is returned.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;