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
use {
::core::{
marker::Unpin,
},
::futures::{
stream::{
Next,
Stream,
StreamExt,
},
},
};
/// "Lifts" and converts an [`Unpin`] [`Stream`] into an
/// <code>impl [LendingIterator]</code> which lends futures (an `S`,
/// say).
///
/// [`Stream`]: https://docs.rs/futures/^0.3.21/futures/stream/trait.Stream.html
/// [`StreamExt`]: https://docs.rs/futures/^0.3.21/futures/stream/trait.StreamExt.html
/// [`BoxFuture`]: https://docs.rs/futures/^0.3.21/futures/future/type.BoxFuture.html
///
/** - ```rust
# ::futures::executor::block_on(async {
use {
::futures::{
prelude::*,
},
::lending_iterator::{
prelude::*,
},
};
let stream = stream::iter(1..=3);
let mut lending_iterator = lending_iterator::from_stream(stream);
loop {
if let Some(item) = lending_iterator.next().unwrap().await {
println!("Got `{}`", item);
} else {
break;
}
}
# })
``` */
///
/// This could be useful to get access to the more lifetime-powerful adapters of
/// this crate (compared to the adapters on [`StreamExt`], for instance).
///
/// - That being said, said adapters often requires the explicitly-turbofished
/// `HKT` parameter, which in turn requires **nameable types**.
///
/// Sadly, most <code>impl [Future]</code> are unnameable!. So you'll mostly
/// need to use [`BoxFuture`]s and whatnot.
///
/// For `nightly` users, however, `type_alias_impl_trait` can be leveraged
/// to avoid unnecessary [`Box`]ing. Keep it in mind!
///
/// [Future]: ::core::future::Future
/// [`Box`]: ::alloc::boxed::Box
///
/// For instance, here is what a `&mut`-based "unfold" async iterator
/// construction would look like:
///
/** - ```rust
# ::futures::executor::block_on(async {
use {
::futures::{
future::BoxFuture,
prelude::*,
},
::lending_iterator::{
prelude::*,
},
};
let mut iter =
lending_iterator::repeat_mut(0)
.map::<HKT!(BoxFuture<'_, Option<i32>>), _>(
|[], state: &mut _| async move {
if *state <= 2 {
let yielded = *state * 2;
*state += 1;
Some(yielded)
} else {
None
}
}
.boxed()
)
;
let mut result = vec![];
loop {
if let Some(item) = iter.next().unwrap().await {
result.push(item);
} else {
break;
}
}
assert_eq!(result, [0, 2, 4]);
# })
``` */
///
#[apply(cfg_futures)]
pub
fn from_stream<S : Stream + Unpin> (
stream: S,
) -> FromStream<S>
{
FromStream(stream)
}
/// The <code>impl [LendingIterator]</code> returned by [`from_stream()`].
#[apply(cfg_futures)]
pub
struct FromStream<S : Stream + Unpin>(
S,
);
#[apply(cfg_futures)]
#[gat]
impl<S : Stream + Unpin>
LendingIterator
for
FromStream<S>
{
type Item<'next>
where
Self : 'next,
=
Next<'next, S>
;
fn next (
self: &'_ mut FromStream<S>,
) -> Option<
Next<'_, S>
>
{
Some(self.0.next())
}
}