Debian Codegraph @

src/ext/async_stream.rs

raw symbols SCIP
1 //! A minimalist clone of the `async-stream` crate in 100% safe code, without proc macros.
2 //!
3 //! This was created initially to get around some weird compiler errors we were getting with
4 //! `async-stream`, and now it'd just be more work to replace.
5
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::futurerust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/::Futurerust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#;
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/::Pinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/Pin#;
8 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::{Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#, Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#};
9 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::taskrust-analyzer cargo std https://github.com/rust-lang/rust/library/std task/::{Contextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/wake/Context#, Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#};
10
11 use futures_corerust-analyzer cargo futures-core 0.3.34 crate/::futurerust-analyzer cargo futures-core 0.3.34 future/::BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#;
12 use futures_corerust-analyzer cargo futures-core 0.3.34 crate/::streamrust-analyzer cargo futures-core 0.3.34 stream/::Streamrust-analyzer cargo futures-core 0.3.34 stream/Stream#;
13 use futures_corerust-analyzer cargo futures-core 0.3.34 crate/::FusedFuturerust-analyzer cargo futures-core 0.3.34 future/FusedFuture#;
14 use futures_utilrust-analyzer cargo futures-util 0.3.34 crate/::futurerust-analyzer cargo futures-util 0.3.34 future/::Fuserust-analyzer cargo futures-util 0.3.34 future/future/fuse/Fuse#;
15 use futures_utilrust-analyzer cargo futures-util 0.3.34 crate/::FutureExtrust-analyzer cargo futures-util 0.3.34 future/future/FutureExt#;
16
17 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::errorrust-analyzer cargo sqlx-core 0.8.3 error/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#;
18
19 pub struct TryAsyncStreamrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#<'a, T> {
20 yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#yielder.: Yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#<T>,
21 futurerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#future.: Fuserust-analyzer cargo futures-util 0.3.34 future/future/fuse/Fuse#<BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'a, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>>>,
22 }
23
24 impl<'a, T> TryAsyncStreamrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#<'a, T> {
25 pub fn newrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`TryAsyncStream<'a, T>`]new().<F, Fut>(f: F) -> Selfrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`TryAsyncStream<'a, T>`]
26 where
27 F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(Yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#<T>) -> Fut + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
28 Fut: 'a + Futurerust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#Output# = Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
29 T: 'a + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
30 {
31 let yielder = Yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#::newrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`Yielder<T>`]new().();
32
33 let future = f(yielder.duplicaterust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`Yielder<T>`]duplicate().()).boxedrust-analyzer cargo futures-util 0.3.34 future/future/FutureExt#boxed().().fuserust-analyzer cargo futures-util 0.3.34 future/future/FutureExt#fuse().();
34
35 Selfrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`TryAsyncStream<'a, T>`] { futurerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#future., yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#yielder. }
36 }
37 }
38
39 pub struct Yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#<T> {
40 // This mutex should never have any contention in normal operation.
41 // We're just using it because `Rc<Cell<Option<T>>>` would not be `Send`.
42 valuerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#value.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>>>,
43 }
44
45 impl<T> Yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#<T> {
46 fn newrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`Yielder<T>`]new().() -> Selfrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`Yielder<T>`] {
47 Yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder# {
48 valuerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#value.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`]new().(Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)),
49 }
50 }
51
52 // Don't want to expose a `Clone` impl
53 fn duplicaterust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`Yielder<T>`]duplicate().(&self) -> Selfrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`Yielder<T>`] {
54 Yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder# {
55 valuerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#value.: self.valuerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#value..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(),
56 }
57 }
58
59 /// NOTE: may deadlock the task if called from outside the future passed to `TryAsyncStream`.
60 pub async fn r#yieldrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`Yielder<T>`]`r#yield`().(&self, val: T) {
61 let replaced = self
62 .valuerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#value.
63 .lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().()
64 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("BUG: panicked while holding a lock")
65 .replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]replace().(val);
66
67 debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(
68 replaced.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().(),
69 "BUG: previously yielded value not taken"
70 );
71
72 let mut yielded = false;
73
74 // Allows the generating future to suspend its execution without changing the task priority,
75 // which would happen with `tokio::task::yield_now()`.
76 //
77 // Note that because this has no way to schedule a wakeup, this could deadlock the task
78 // if called in the wrong place.
79 futures_utilrust-analyzer cargo futures-util 0.3.34 crate/::futurerust-analyzer cargo futures-util 0.3.34 future/::poll_fnrust-analyzer cargo futures-util 0.3.34 future/poll_fn/poll_fn().(|_cx| {
80 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().yielded {
81 yielded = true;
82 Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Pendingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Pending#
83 } else {
84 Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(())
85 }
86 })
87 .rust-analyzer cargo futures-util 0.3.34 future/poll_fn/impl#[`PollFn<F>`][Future]poll().awaitrust-analyzer cargo futures-util 0.3.34 future/poll_fn/impl#[`PollFn<F>`][Future]poll().
88 }
89
90 fn takerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`Yielder<T>`]take().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T> {
91 self.valuerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/Yielder#value.
92 .lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().()
93 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("BUG: panicked while holding a lock")
94 .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().()
95 }
96 }
97
98 impl<'a, T> Streamrust-analyzer cargo futures-core 0.3.34 stream/Stream# for TryAsyncStreamrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#<'a, T> {
99 type Itemrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`TryAsyncStream<'a, T>`][Stream]Item# = Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>;
100
101 fn poll_nextrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`TryAsyncStream<'a, T>`][Stream]poll_next().(mut self: Pinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/Pin#<&mut Selfrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`TryAsyncStream<'a, T>`][Stream]>, cx: &mut Contextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/wake/Context#<'_>) -> Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`TryAsyncStream<'a, T>`][Stream]::Itemrust-analyzer cargo futures-core 0.3.34 stream/Stream#Item#>> {
102 if self.futurerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#future..is_terminatedrust-analyzer cargo futures-util 0.3.34 future/future/fuse/impl#[`Fuse<Fut>`][FusedFuture]is_terminated().() {
103 return Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
104 }
105
106 match self.futurerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#future..poll_unpinrust-analyzer cargo futures-util 0.3.34 future/future/FutureExt#poll_unpin().(cx) {
107 Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())) => {
108 // Future returned without yielding another value,
109 // or else it would have returned `Pending` instead.
110 Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
111 }
112 Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e)) => Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e))),
113 Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Pendingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Pending# => self
114 .yielderrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/TryAsyncStream#yielder.
115 .takerust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/impl#[`Yielder<T>`]take().()
116 .map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Pendingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Pending#, |val| Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val)))),
117 }
118 }
119 }
120
121 #[macro_export]
122 macro_rules! try_streamrust-analyzer cargo sqlx-core 0.8.3 ext/async_stream/try_stream! {
123 ($($block:tt)*) => {
124 $crate::ext::async_stream::TryAsyncStream::new(move |yielder| async move {
125 // Anti-footgun: effectively pins `yielder` to this future to prevent any accidental
126 // move to another task, which could deadlock.
127 let yielder = &yielder;
128
129 macro_rules! r#yield {
130 ($v:expr) => {{
131 yielder.r#yield($v).await;
132 }}
133 }
134
135 $($block)*
136 })
137 }
138 }