error_stack/future.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
//! Extension for convenient usage of [`Report`]s returned by [`Future`] s.
//!
//! Extends [`Future`] with the same methods as [`ResultExt`] but calls the methods on [`poll`]ing.
//!
//! [`Report`]: crate::Report
//! [`poll`]: Future::poll
use core::{
fmt::{Debug, Display},
future::Future,
pin::Pin,
task::{Context as TaskContext, Poll},
};
use crate::{Context, Result, ResultExt};
macro_rules! implement_future_adaptor {
($future:ident, $method:ident, $bound:ident $(+ $bounds:ident)* $(+ $lifetime:lifetime)*, $output:ty) => {
#[doc = concat!("Adaptor returned by [`FutureExt::", stringify!( $method ), "`].")]
pub struct $future<Fut, T> {
future: Fut,
inner: Option<T>,
}
impl<Fut, T> Future for $future<Fut, T>
where
Fut: Future,
Fut::Output: ResultExt,
T: $bound $(+ $bounds)* $(+ $lifetime)*
{
type Output = $output;
#[track_caller]
fn poll(self: Pin<&mut Self>, cx: &mut TaskContext) -> Poll<Self::Output> {
// SAFETY: The pointee of `inner` will not move. Note, that the value inside the
// `Option` will be taken, but the `Option` remains in place. Additionally,
// `Self` does not implement `Drop`, nor is it `#[repr(packed)]`
// See the `pin` module: https://doc.rust-lang.org/core/pin/index.html
let (future, inner) = unsafe {
let Self { future, inner } = self.get_unchecked_mut();
(Pin::new_unchecked(future), inner)
};
// Can't use `map` as `#[track_caller]` is unstable on closures
match future.poll(cx) {
Poll::Ready(value) => {
Poll::Ready(value.$method({
inner.take().expect("Cannot poll context after it resolves")
}))
}
Poll::Pending => Poll::Pending,
}
}
}
};
}
macro_rules! implement_lazy_future_adaptor {
($future:ident, $method:ident, $bound:ident $(+ $bounds:ident)* $(+ $lifetime:lifetime)*, $output:ty) => {
#[doc = concat!("Adaptor returned by [`FutureExt::", stringify!( $method ), "`].")]
pub struct $future<Fut, F> {
future: Fut,
inner: Option<F>,
}
impl<Fut, F, T> Future for $future<Fut, F>
where
Fut: Future,
Fut::Output: ResultExt,
F: FnOnce() -> T,
T: $bound $(+ $bounds)* $(+ $lifetime)*
{
type Output = $output;
#[track_caller]
fn poll(self: Pin<&mut Self>, cx: &mut TaskContext) -> Poll<Self::Output> {
// SAFETY: The pointee of `inner` will not move. Note, that the value inside the
// `Option` will be taken, but the `Option` remains in place. Additionally,
// `Self` does not implement `Drop`, nor is it `#[repr(packed)]`
// See the `pin` module: https://doc.rust-lang.org/core/pin/index.html
let (future, inner) = unsafe {
let Self { future, inner } = self.get_unchecked_mut();
(Pin::new_unchecked(future), inner)
};
// Can't use `map` as `#[track_caller]` is unstable on closures
match future.poll(cx) {
Poll::Ready(value) => {
Poll::Ready(value.$method({
inner.take().expect("Cannot poll context after it resolves")
}))
}
Poll::Pending => Poll::Pending,
}
}
}
};
}
implement_future_adaptor!(
FutureWithAttachment,
attach,
Send + Sync + 'static,
Result<<Fut::Output as ResultExt>::Ok, <Fut::Output as ResultExt>::Context>
);
implement_lazy_future_adaptor!(
FutureWithLazyAttachment,
attach_lazy,
Send + Sync + 'static,
Result<<Fut::Output as ResultExt>::Ok, <Fut::Output as ResultExt>::Context>
);
implement_future_adaptor!(
FutureWithPrintableAttachment,
attach_printable,
Display + Debug + Send + Sync + 'static,
Result<<Fut::Output as ResultExt>::Ok, <Fut::Output as ResultExt>::Context>
);
implement_lazy_future_adaptor!(
FutureWithLazyPrintableAttachment,
attach_printable_lazy,
Display + Debug + Send + Sync + 'static,
Result<<Fut::Output as ResultExt>::Ok, <Fut::Output as ResultExt>::Context>
);
implement_future_adaptor!(
FutureWithContext,
change_context,
Context,
Result<<Fut::Output as ResultExt>::Ok, T>
);
implement_lazy_future_adaptor!(
FutureWithLazyContext,
change_context_lazy,
Context,
Result<<Fut::Output as ResultExt>::Ok, T>
);
/// Extension trait for [`Future`] to provide contextual information on [`Report`]s.
///
/// [`Report`]: crate::Report
pub trait FutureExt: Future + Sized {
/// Adds a new attachment to the [`Report`] inside the [`Result`] when [`poll`]ing the
/// [`Future`].
///
/// Applies [`Report::attach`] on the [`Err`] variant, refer to it for more information.
///
/// [`Report`]: crate::Report
/// [`Report::attach`]: crate::Report::attach
/// [`poll`]: Future::poll
#[track_caller]
fn attach<A>(self, attachment: A) -> FutureWithAttachment<Self, A>
where
A: Send + Sync + 'static;
/// Lazily adds a new attachment to the [`Report`] inside the [`Result`] when [`poll`]ing the
/// [`Future`].
///
/// Applies [`Report::attach`] on the [`Err`] variant, refer to it for more information.
///
/// [`Report`]: crate::Report
/// [`Report::attach`]: crate::Report::attach
/// [`poll`]: Future::poll
#[track_caller]
fn attach_lazy<A, F>(self, attachment: F) -> FutureWithLazyAttachment<Self, F>
where
A: Send + Sync + 'static,
F: FnOnce() -> A;
/// Adds a new printable attachment to the [`Report`] inside the [`Result`] when [`poll`]ing the
/// [`Future`].
///
/// Applies [`Report::attach_printable`] on the [`Err`] variant, refer to it for more
/// information.
///
/// [`Report`]: crate::Report
/// [`Report::attach_printable`]: crate::Report::attach_printable
/// [`poll`]: Future::poll
#[track_caller]
fn attach_printable<A>(self, attachment: A) -> FutureWithPrintableAttachment<Self, A>
where
A: Display + Debug + Send + Sync + 'static;
/// Lazily adds a new printable attachment to the [`Report`] inside the [`Result`] when
/// [`poll`]ing the [`Future`].
///
/// Applies [`Report::attach_printable`] on the [`Err`] variant, refer to it for more
/// information.
///
/// [`Report`]: crate::Report
/// [`Report::attach_printable`]: crate::Report::attach_printable
/// [`poll`]: Future::poll
#[track_caller]
fn attach_printable_lazy<A, F>(
self,
attachment: F,
) -> FutureWithLazyPrintableAttachment<Self, F>
where
A: Display + Debug + Send + Sync + 'static,
F: FnOnce() -> A;
/// Changes the [`Context`] of the [`Report`] inside the [`Result`] when [`poll`]ing the
/// [`Future`].
///
/// Applies [`Report::change_context`] on the [`Err`] variant, refer to it for more information.
///
/// [`Report`]: crate::Report
/// [`Report::change_context`]: crate::Report::change_context
/// [`poll`]: Future::poll
#[track_caller]
fn change_context<C>(self, context: C) -> FutureWithContext<Self, C>
where
C: Context;
/// Lazily changes the [`Context`] of the [`Report`] inside the [`Result`] when [`poll`]ing the
/// [`Future`].
///
/// Applies [`Report::change_context`] on the [`Err`] variant, refer to it for more information.
///
/// [`Report`]: crate::Report
/// [`Report::change_context`]: crate::Report::change_context
/// [`poll`]: Future::poll
#[track_caller]
fn change_context_lazy<C, F>(self, context: F) -> FutureWithLazyContext<Self, F>
where
C: Context,
F: FnOnce() -> C;
}
impl<Fut: Future> FutureExt for Fut
where
Fut::Output: ResultExt,
{
fn attach<A>(self, attachment: A) -> FutureWithAttachment<Self, A>
where
A: Send + Sync + 'static,
{
FutureWithAttachment {
future: self,
inner: Some(attachment),
}
}
#[track_caller]
fn attach_lazy<A, F>(self, attachment: F) -> FutureWithLazyAttachment<Self, F>
where
A: Send + Sync + 'static,
F: FnOnce() -> A,
{
FutureWithLazyAttachment {
future: self,
inner: Some(attachment),
}
}
#[track_caller]
fn attach_printable<A>(self, attachment: A) -> FutureWithPrintableAttachment<Self, A>
where
A: Display + Debug + Send + Sync + 'static,
{
FutureWithPrintableAttachment {
future: self,
inner: Some(attachment),
}
}
#[track_caller]
fn attach_printable_lazy<A, F>(
self,
attachment: F,
) -> FutureWithLazyPrintableAttachment<Self, F>
where
A: Display + Debug + Send + Sync + 'static,
F: FnOnce() -> A,
{
FutureWithLazyPrintableAttachment {
future: self,
inner: Some(attachment),
}
}
#[track_caller]
fn change_context<C>(self, context: C) -> FutureWithContext<Self, C>
where
C: Context,
{
FutureWithContext {
future: self,
inner: Some(context),
}
}
#[track_caller]
fn change_context_lazy<C, F>(self, context: F) -> FutureWithLazyContext<Self, F>
where
C: Context,
F: FnOnce() -> C,
{
FutureWithLazyContext {
future: self,
inner: Some(context),
}
}
}