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
/*
* Copyright (C) 2026 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tokio::sync::oneshot::{
self,
error::{RecvError, TryRecvError},
};
use crate::{Cancellation, CancellationCause, CaptureOutcome};
/// Contains the final outcome of a [`Series`].
///
/// This mostly just wraps a oneshot receiver to have nicer ergonomics. If the
/// oneshot sender disconnects, its error message gets flattened into a regular
/// cancellation.
///
/// [`Series`]: crate::Series
pub struct Outcome<T> {
/// A receiver to receive the actual result of the outcome
value: oneshot::Receiver<Result<T, Cancellation>>,
/// A receiver attached to a sender who is simply monitoring for whether
/// the outcome gets dropped.
finished: Option<oneshot::Sender<()>>,
}
impl<T> Future for Outcome<T> {
type Output = Result<T, Cancellation>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let self_mut = self.get_mut();
let r = Future::poll(Pin::new(&mut self_mut.value), cx).map(flatten_recv);
match r {
Poll::Pending => Poll::Pending,
Poll::Ready(value) => {
// If we are receiving the value then notify the finished monitor
// that we successfully received the outcome.
if let Some(finished) = self_mut.finished.take() {
let _ = finished.send(());
}
Poll::Ready(value)
}
}
}
}
impl<T> Outcome<T> {
/// Try receiving the outcome if it's available. This can be used in a
/// blocking context but does not block execution itself.
///
/// If the outcome is not available yet, this will return None.
///
/// If the outcome was previously delivered or if the sender was dropped,
/// this will give a [`CancellationCause::Undeliverable`].
pub fn try_recv(&mut self) -> Option<Result<T, Cancellation>> {
match self.value.try_recv() {
Ok(r) => {
if let Some(finished) = self.finished.take() {
let _ = finished.send(());
}
Some(r)
}
Err(err) => match err {
TryRecvError::Empty => None,
TryRecvError::Closed => {
if let Some(finished) = self.finished.take() {
let _ = finished.send(());
}
Some(Err(CancellationCause::Undeliverable.into()))
}
},
}
}
/// Check if the outcome has already been delivered. If this is true then
/// you will no longer be able to poll for the outcome.
pub fn is_terminated(&self) -> bool {
self.value.is_terminated()
}
/// Check if the outcome is available to be received.
///
/// If the outcome was previously received this will return false. Use
/// [`Self::is_terminated`] to tell pending outcomes apart from
/// already-delivered outcomes.
///
/// If you want to know if an outcome is still pending, use [`Self::is_pending`].
pub fn is_available(&self) -> bool {
!self.value.is_empty()
}
/// Check if the outcome is still being determined.
pub fn is_pending(&self) -> bool {
self.value.is_empty() && !self.is_terminated()
}
pub(crate) fn new() -> (Self, CaptureOutcome<T>) {
let (value_sender, value_receiver) = oneshot::channel();
let (finished_sender, finished_receiver) = oneshot::channel();
let outcome = Self {
value: value_receiver,
finished: Some(finished_sender),
};
let capture = CaptureOutcome::new(value_sender, finished_receiver);
(outcome, capture)
}
}
fn flatten_recv<T>(
response: Result<Result<T, Cancellation>, RecvError>,
) -> Result<T, Cancellation> {
match response {
Ok(r) => r,
Err(err) => Err(err.into()),
}
}