diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/futures-0.1.31/src/future/chain.rs | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/futures-0.1.31/src/future/chain.rs')
-rw-r--r-- | third_party/rust/futures-0.1.31/src/future/chain.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/third_party/rust/futures-0.1.31/src/future/chain.rs b/third_party/rust/futures-0.1.31/src/future/chain.rs new file mode 100644 index 0000000000..1bf5cd639c --- /dev/null +++ b/third_party/rust/futures-0.1.31/src/future/chain.rs @@ -0,0 +1,48 @@ +use core::mem; + +use {Future, Poll, Async}; + +#[derive(Debug)] +pub enum Chain<A, B, C> where A: Future { + First(A, C), + Second(B), + Done, +} + +impl<A, B, C> Chain<A, B, C> + where A: Future, + B: Future, +{ + pub fn new(a: A, c: C) -> Chain<A, B, C> { + Chain::First(a, c) + } + + pub fn poll<F>(&mut self, f: F) -> Poll<B::Item, B::Error> + where F: FnOnce(Result<A::Item, A::Error>, C) + -> Result<Result<B::Item, B>, B::Error>, + { + let a_result = match *self { + Chain::First(ref mut a, _) => { + match a.poll() { + Ok(Async::NotReady) => return Ok(Async::NotReady), + Ok(Async::Ready(t)) => Ok(t), + Err(e) => Err(e), + } + } + Chain::Second(ref mut b) => return b.poll(), + Chain::Done => panic!("cannot poll a chained future twice"), + }; + let data = match mem::replace(self, Chain::Done) { + Chain::First(_, c) => c, + _ => panic!(), + }; + match f(a_result, data)? { + Ok(e) => Ok(Async::Ready(e)), + Err(mut b) => { + let ret = b.poll(); + *self = Chain::Second(b); + ret + } + } + } +} |