1 //! Futures 2 //! 3 //! This module contains a number of functions for working with `Future`s, 4 //! including the `FutureExt` trait which adds methods to `Future` types. 5 6 #[cfg(feature = "compat")] 7 use crate::compat::Compat; 8 use core::pin::Pin; 9 use futures_core::{ 10 future::TryFuture, 11 stream::TryStream, 12 task::{Context, Poll}, 13 }; 14 #[cfg(feature = "sink")] 15 use futures_sink::Sink; 16 17 use crate::fns::{ 18 inspect_err_fn, inspect_ok_fn, into_fn, map_err_fn, map_ok_fn, map_ok_or_else_fn, 19 unwrap_or_else_fn, InspectErrFn, InspectOkFn, IntoFn, MapErrFn, MapOkFn, MapOkOrElseFn, 20 UnwrapOrElseFn, 21 }; 22 use crate::future::{assert_future, Inspect, Map}; 23 use crate::stream::assert_stream; 24 25 // Combinators 26 mod into_future; 27 mod try_flatten; 28 mod try_flatten_err; 29 30 delegate_all!( 31 /// Future for the [`try_flatten`](TryFutureExt::try_flatten) method. 32 TryFlatten<Fut1, Fut2>( 33 try_flatten::TryFlatten<Fut1, Fut2> 34 ): Debug + Future + FusedFuture + New[|x: Fut1| try_flatten::TryFlatten::new(x)] 35 ); 36 37 delegate_all!( 38 /// Future for the [`try_flatten_err`](TryFutureExt::try_flatten_err) method. 39 TryFlattenErr<Fut1, Fut2>( 40 try_flatten_err::TryFlattenErr<Fut1, Fut2> 41 ): Debug + Future + FusedFuture + New[|x: Fut1| try_flatten_err::TryFlattenErr::new(x)] 42 ); 43 44 delegate_all!( 45 /// Future for the [`try_flatten_stream`](TryFutureExt::try_flatten_stream) method. 46 TryFlattenStream<Fut>( 47 try_flatten::TryFlatten<Fut, Fut::Ok> 48 ): Debug + Sink + Stream + FusedStream + New[|x: Fut| try_flatten::TryFlatten::new(x)] 49 where Fut: TryFuture 50 ); 51 52 #[cfg(feature = "sink")] 53 delegate_all!( 54 /// Sink for the [`flatten_sink`](TryFutureExt::flatten_sink) method. 55 #[cfg_attr(docsrs, doc(cfg(feature = "sink")))] 56 FlattenSink<Fut, Si>( 57 try_flatten::TryFlatten<Fut, Si> 58 ): Debug + Sink + Stream + FusedStream + New[|x: Fut| try_flatten::TryFlatten::new(x)] 59 ); 60 61 delegate_all!( 62 /// Future for the [`and_then`](TryFutureExt::and_then) method. 63 AndThen<Fut1, Fut2, F>( 64 TryFlatten<MapOk<Fut1, F>, Fut2> 65 ): Debug + Future + FusedFuture + New[|x: Fut1, f: F| TryFlatten::new(MapOk::new(x, f))] 66 ); 67 68 delegate_all!( 69 /// Future for the [`or_else`](TryFutureExt::or_else) method. 70 OrElse<Fut1, Fut2, F>( 71 TryFlattenErr<MapErr<Fut1, F>, Fut2> 72 ): Debug + Future + FusedFuture + New[|x: Fut1, f: F| TryFlattenErr::new(MapErr::new(x, f))] 73 ); 74 75 delegate_all!( 76 /// Future for the [`err_into`](TryFutureExt::err_into) method. 77 ErrInto<Fut, E>( 78 MapErr<Fut, IntoFn<E>> 79 ): Debug + Future + FusedFuture + New[|x: Fut| MapErr::new(x, into_fn())] 80 ); 81 82 delegate_all!( 83 /// Future for the [`ok_into`](TryFutureExt::ok_into) method. 84 OkInto<Fut, E>( 85 MapOk<Fut, IntoFn<E>> 86 ): Debug + Future + FusedFuture + New[|x: Fut| MapOk::new(x, into_fn())] 87 ); 88 89 delegate_all!( 90 /// Future for the [`inspect_ok`](super::TryFutureExt::inspect_ok) method. 91 InspectOk<Fut, F>( 92 Inspect<IntoFuture<Fut>, InspectOkFn<F>> 93 ): Debug + Future + FusedFuture + New[|x: Fut, f: F| Inspect::new(IntoFuture::new(x), inspect_ok_fn(f))] 94 ); 95 96 delegate_all!( 97 /// Future for the [`inspect_err`](super::TryFutureExt::inspect_err) method. 98 InspectErr<Fut, F>( 99 Inspect<IntoFuture<Fut>, InspectErrFn<F>> 100 ): Debug + Future + FusedFuture + New[|x: Fut, f: F| Inspect::new(IntoFuture::new(x), inspect_err_fn(f))] 101 ); 102 103 #[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411 104 pub use self::into_future::IntoFuture; 105 106 delegate_all!( 107 /// Future for the [`map_ok`](TryFutureExt::map_ok) method. 108 MapOk<Fut, F>( 109 Map<IntoFuture<Fut>, MapOkFn<F>> 110 ): Debug + Future + FusedFuture + New[|x: Fut, f: F| Map::new(IntoFuture::new(x), map_ok_fn(f))] 111 ); 112 113 delegate_all!( 114 /// Future for the [`map_err`](TryFutureExt::map_err) method. 115 MapErr<Fut, F>( 116 Map<IntoFuture<Fut>, MapErrFn<F>> 117 ): Debug + Future + FusedFuture + New[|x: Fut, f: F| Map::new(IntoFuture::new(x), map_err_fn(f))] 118 ); 119 120 delegate_all!( 121 /// Future for the [`map_ok_or_else`](TryFutureExt::map_ok_or_else) method. 122 MapOkOrElse<Fut, F, G>( 123 Map<IntoFuture<Fut>, MapOkOrElseFn<F, G>> 124 ): Debug + Future + FusedFuture + New[|x: Fut, f: F, g: G| Map::new(IntoFuture::new(x), map_ok_or_else_fn(f, g))] 125 ); 126 127 delegate_all!( 128 /// Future for the [`unwrap_or_else`](TryFutureExt::unwrap_or_else) method. 129 UnwrapOrElse<Fut, F>( 130 Map<IntoFuture<Fut>, UnwrapOrElseFn<F>> 131 ): Debug + Future + FusedFuture + New[|x: Fut, f: F| Map::new(IntoFuture::new(x), unwrap_or_else_fn(f))] 132 ); 133 134 impl<Fut: ?Sized + TryFuture> TryFutureExt for Fut {} 135 136 /// Adapters specific to [`Result`]-returning futures 137 pub trait TryFutureExt: TryFuture { 138 /// Flattens the execution of this future when the successful result of this 139 /// future is a [`Sink`]. 140 /// 141 /// This can be useful when sink initialization is deferred, and it is 142 /// convenient to work with that sink as if the sink was available at the 143 /// call site. 144 /// 145 /// Note that this function consumes this future and returns a wrapped 146 /// version of it. 147 /// 148 /// # Examples 149 /// 150 /// ``` 151 /// use futures::future::{Future, TryFutureExt}; 152 /// use futures::sink::Sink; 153 /// # use futures::channel::mpsc::{self, SendError}; 154 /// # type T = i32; 155 /// # type E = SendError; 156 /// 157 /// fn make_sink_async() -> impl Future<Output = Result< 158 /// impl Sink<T, Error = E>, 159 /// E, 160 /// >> { // ... } 161 /// # let (tx, _rx) = mpsc::unbounded::<i32>(); 162 /// # futures::future::ready(Ok(tx)) 163 /// # } 164 /// fn take_sink(sink: impl Sink<T, Error = E>) { /* ... */ } 165 /// 166 /// let fut = make_sink_async(); 167 /// take_sink(fut.flatten_sink()) 168 /// ``` 169 #[cfg(feature = "sink")] 170 #[cfg_attr(docsrs, doc(cfg(feature = "sink")))] flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok> where Self::Ok: Sink<Item, Error = Self::Error>, Self: Sized,171 fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok> 172 where 173 Self::Ok: Sink<Item, Error = Self::Error>, 174 Self: Sized, 175 { 176 crate::sink::assert_sink::<Item, Self::Error, _>(FlattenSink::new(self)) 177 } 178 179 /// Maps this future's success value to a different value. 180 /// 181 /// This method can be used to change the [`Ok`](TryFuture::Ok) type of the 182 /// future into a different type. It is similar to the [`Result::map`] 183 /// method. You can use this method to chain along a computation once the 184 /// future has been resolved. 185 /// 186 /// The provided closure `f` will only be called if this future is resolved 187 /// to an [`Ok`]. If it resolves to an [`Err`], panics, or is dropped, then 188 /// the provided closure will never be invoked. 189 /// 190 /// Note that this method consumes the future it is called on and returns a 191 /// wrapped version of it. 192 /// 193 /// # Examples 194 /// 195 /// ``` 196 /// use futures::future::TryFutureExt; 197 /// 198 /// # futures::executor::block_on(async { 199 /// let future = async { Ok::<i32, i32>(1) }; 200 /// let future = future.map_ok(|x| x + 3); 201 /// assert_eq!(future.await, Ok(4)); 202 /// # }); 203 /// ``` 204 /// 205 /// Calling [`map_ok`](TryFutureExt::map_ok) on an errored future has no 206 /// effect: 207 /// 208 /// ``` 209 /// use futures::future::TryFutureExt; 210 /// 211 /// # futures::executor::block_on(async { 212 /// let future = async { Err::<i32, i32>(1) }; 213 /// let future = future.map_ok(|x| x + 3); 214 /// assert_eq!(future.await, Err(1)); 215 /// # }); 216 /// ``` map_ok<T, F>(self, f: F) -> MapOk<Self, F> where F: FnOnce(Self::Ok) -> T, Self: Sized,217 fn map_ok<T, F>(self, f: F) -> MapOk<Self, F> 218 where 219 F: FnOnce(Self::Ok) -> T, 220 Self: Sized, 221 { 222 assert_future::<Result<T, Self::Error>, _>(MapOk::new(self, f)) 223 } 224 225 /// Maps this future's success value to a different value, and permits for error handling resulting in the same type. 226 /// 227 /// This method can be used to coalesce your [`Ok`](TryFuture::Ok) type and [`Error`](TryFuture::Error) into another type, 228 /// where that type is the same for both outcomes. 229 /// 230 /// The provided closure `f` will only be called if this future is resolved 231 /// to an [`Ok`]. If it resolves to an [`Err`], panics, or is dropped, then 232 /// the provided closure will never be invoked. 233 /// 234 /// The provided closure `e` will only be called if this future is resolved 235 /// to an [`Err`]. If it resolves to an [`Ok`], panics, or is dropped, then 236 /// the provided closure will never be invoked. 237 /// 238 /// Note that this method consumes the future it is called on and returns a 239 /// wrapped version of it. 240 /// 241 /// # Examples 242 /// 243 /// ``` 244 /// use futures::future::TryFutureExt; 245 /// 246 /// # futures::executor::block_on(async { 247 /// let future = async { Ok::<i32, i32>(5) }; 248 /// let future = future.map_ok_or_else(|x| x * 2, |x| x + 3); 249 /// assert_eq!(future.await, 8); 250 /// 251 /// let future = async { Err::<i32, i32>(5) }; 252 /// let future = future.map_ok_or_else(|x| x * 2, |x| x + 3); 253 /// assert_eq!(future.await, 10); 254 /// # }); 255 /// ``` 256 /// map_ok_or_else<T, E, F>(self, e: E, f: F) -> MapOkOrElse<Self, F, E> where F: FnOnce(Self::Ok) -> T, E: FnOnce(Self::Error) -> T, Self: Sized,257 fn map_ok_or_else<T, E, F>(self, e: E, f: F) -> MapOkOrElse<Self, F, E> 258 where 259 F: FnOnce(Self::Ok) -> T, 260 E: FnOnce(Self::Error) -> T, 261 Self: Sized, 262 { 263 assert_future::<T, _>(MapOkOrElse::new(self, f, e)) 264 } 265 266 /// Maps this future's error value to a different value. 267 /// 268 /// This method can be used to change the [`Error`](TryFuture::Error) type 269 /// of the future into a different type. It is similar to the 270 /// [`Result::map_err`] method. You can use this method for example to 271 /// ensure that futures have the same [`Error`](TryFuture::Error) type when 272 /// using [`select!`] or [`join!`]. 273 /// 274 /// The provided closure `f` will only be called if this future is resolved 275 /// to an [`Err`]. If it resolves to an [`Ok`], panics, or is dropped, then 276 /// the provided closure will never be invoked. 277 /// 278 /// Note that this method consumes the future it is called on and returns a 279 /// wrapped version of it. 280 /// 281 /// # Examples 282 /// 283 /// ``` 284 /// use futures::future::TryFutureExt; 285 /// 286 /// # futures::executor::block_on(async { 287 /// let future = async { Err::<i32, i32>(1) }; 288 /// let future = future.map_err(|x| x + 3); 289 /// assert_eq!(future.await, Err(4)); 290 /// # }); 291 /// ``` 292 /// 293 /// Calling [`map_err`](TryFutureExt::map_err) on a successful future has 294 /// no effect: 295 /// 296 /// ``` 297 /// use futures::future::TryFutureExt; 298 /// 299 /// # futures::executor::block_on(async { 300 /// let future = async { Ok::<i32, i32>(1) }; 301 /// let future = future.map_err(|x| x + 3); 302 /// assert_eq!(future.await, Ok(1)); 303 /// # }); 304 /// ``` map_err<E, F>(self, f: F) -> MapErr<Self, F> where F: FnOnce(Self::Error) -> E, Self: Sized,305 fn map_err<E, F>(self, f: F) -> MapErr<Self, F> 306 where 307 F: FnOnce(Self::Error) -> E, 308 Self: Sized, 309 { 310 assert_future::<Result<Self::Ok, E>, _>(MapErr::new(self, f)) 311 } 312 313 /// Maps this future's [`Error`](TryFuture::Error) to a new error type 314 /// using the [`Into`](std::convert::Into) trait. 315 /// 316 /// This method does for futures what the `?`-operator does for 317 /// [`Result`]: It lets the compiler infer the type of the resulting 318 /// error. Just as [`map_err`](TryFutureExt::map_err), this is useful for 319 /// example to ensure that futures have the same [`Error`](TryFuture::Error) 320 /// type when using [`select!`] or [`join!`]. 321 /// 322 /// Note that this method consumes the future it is called on and returns a 323 /// wrapped version of it. 324 /// 325 /// # Examples 326 /// 327 /// ``` 328 /// use futures::future::TryFutureExt; 329 /// 330 /// # futures::executor::block_on(async { 331 /// let future_err_u8 = async { Err::<(), u8>(1) }; 332 /// let future_err_i32 = future_err_u8.err_into::<i32>(); 333 /// # }); 334 /// ``` err_into<E>(self) -> ErrInto<Self, E> where Self: Sized, Self::Error: Into<E>,335 fn err_into<E>(self) -> ErrInto<Self, E> 336 where 337 Self: Sized, 338 Self::Error: Into<E>, 339 { 340 assert_future::<Result<Self::Ok, E>, _>(ErrInto::new(self)) 341 } 342 343 /// Maps this future's [`Ok`](TryFuture::Ok) to a new type 344 /// using the [`Into`](std::convert::Into) trait. ok_into<U>(self) -> OkInto<Self, U> where Self: Sized, Self::Ok: Into<U>,345 fn ok_into<U>(self) -> OkInto<Self, U> 346 where 347 Self: Sized, 348 Self::Ok: Into<U>, 349 { 350 assert_future::<Result<U, Self::Error>, _>(OkInto::new(self)) 351 } 352 353 /// Executes another future after this one resolves successfully. The 354 /// success value is passed to a closure to create this subsequent future. 355 /// 356 /// The provided closure `f` will only be called if this future is resolved 357 /// to an [`Ok`]. If this future resolves to an [`Err`], panics, or is 358 /// dropped, then the provided closure will never be invoked. The 359 /// [`Error`](TryFuture::Error) type of this future and the future 360 /// returned by `f` have to match. 361 /// 362 /// Note that this method consumes the future it is called on and returns a 363 /// wrapped version of it. 364 /// 365 /// # Examples 366 /// 367 /// ``` 368 /// use futures::future::TryFutureExt; 369 /// 370 /// # futures::executor::block_on(async { 371 /// let future = async { Ok::<i32, i32>(1) }; 372 /// let future = future.and_then(|x| async move { Ok::<i32, i32>(x + 3) }); 373 /// assert_eq!(future.await, Ok(4)); 374 /// # }); 375 /// ``` 376 /// 377 /// Calling [`and_then`](TryFutureExt::and_then) on an errored future has no 378 /// effect: 379 /// 380 /// ``` 381 /// use futures::future::TryFutureExt; 382 /// 383 /// # futures::executor::block_on(async { 384 /// let future = async { Err::<i32, i32>(1) }; 385 /// let future = future.and_then(|x| async move { Err::<i32, i32>(x + 3) }); 386 /// assert_eq!(future.await, Err(1)); 387 /// # }); 388 /// ``` and_then<Fut, F>(self, f: F) -> AndThen<Self, Fut, F> where F: FnOnce(Self::Ok) -> Fut, Fut: TryFuture<Error = Self::Error>, Self: Sized,389 fn and_then<Fut, F>(self, f: F) -> AndThen<Self, Fut, F> 390 where 391 F: FnOnce(Self::Ok) -> Fut, 392 Fut: TryFuture<Error = Self::Error>, 393 Self: Sized, 394 { 395 assert_future::<Result<Fut::Ok, Fut::Error>, _>(AndThen::new(self, f)) 396 } 397 398 /// Executes another future if this one resolves to an error. The 399 /// error value is passed to a closure to create this subsequent future. 400 /// 401 /// The provided closure `f` will only be called if this future is resolved 402 /// to an [`Err`]. If this future resolves to an [`Ok`], panics, or is 403 /// dropped, then the provided closure will never be invoked. The 404 /// [`Ok`](TryFuture::Ok) type of this future and the future returned by `f` 405 /// have to match. 406 /// 407 /// Note that this method consumes the future it is called on and returns a 408 /// wrapped version of it. 409 /// 410 /// # Examples 411 /// 412 /// ``` 413 /// use futures::future::TryFutureExt; 414 /// 415 /// # futures::executor::block_on(async { 416 /// let future = async { Err::<i32, i32>(1) }; 417 /// let future = future.or_else(|x| async move { Err::<i32, i32>(x + 3) }); 418 /// assert_eq!(future.await, Err(4)); 419 /// # }); 420 /// ``` 421 /// 422 /// Calling [`or_else`](TryFutureExt::or_else) on a successful future has 423 /// no effect: 424 /// 425 /// ``` 426 /// use futures::future::TryFutureExt; 427 /// 428 /// # futures::executor::block_on(async { 429 /// let future = async { Ok::<i32, i32>(1) }; 430 /// let future = future.or_else(|x| async move { Ok::<i32, i32>(x + 3) }); 431 /// assert_eq!(future.await, Ok(1)); 432 /// # }); 433 /// ``` or_else<Fut, F>(self, f: F) -> OrElse<Self, Fut, F> where F: FnOnce(Self::Error) -> Fut, Fut: TryFuture<Ok = Self::Ok>, Self: Sized,434 fn or_else<Fut, F>(self, f: F) -> OrElse<Self, Fut, F> 435 where 436 F: FnOnce(Self::Error) -> Fut, 437 Fut: TryFuture<Ok = Self::Ok>, 438 Self: Sized, 439 { 440 assert_future::<Result<Fut::Ok, Fut::Error>, _>(OrElse::new(self, f)) 441 } 442 443 /// Do something with the success value of a future before passing it on. 444 /// 445 /// When using futures, you'll often chain several of them together. While 446 /// working on such code, you might want to check out what's happening at 447 /// various parts in the pipeline, without consuming the intermediate 448 /// value. To do that, insert a call to `inspect_ok`. 449 /// 450 /// # Examples 451 /// 452 /// ``` 453 /// # futures::executor::block_on(async { 454 /// use futures::future::TryFutureExt; 455 /// 456 /// let future = async { Ok::<_, ()>(1) }; 457 /// let new_future = future.inspect_ok(|&x| println!("about to resolve: {}", x)); 458 /// assert_eq!(new_future.await, Ok(1)); 459 /// # }); 460 /// ``` inspect_ok<F>(self, f: F) -> InspectOk<Self, F> where F: FnOnce(&Self::Ok), Self: Sized,461 fn inspect_ok<F>(self, f: F) -> InspectOk<Self, F> 462 where 463 F: FnOnce(&Self::Ok), 464 Self: Sized, 465 { 466 assert_future::<Result<Self::Ok, Self::Error>, _>(InspectOk::new(self, f)) 467 } 468 469 /// Do something with the error value of a future before passing it on. 470 /// 471 /// When using futures, you'll often chain several of them together. While 472 /// working on such code, you might want to check out what's happening at 473 /// various parts in the pipeline, without consuming the intermediate 474 /// value. To do that, insert a call to `inspect_err`. 475 /// 476 /// # Examples 477 /// 478 /// ``` 479 /// # futures::executor::block_on(async { 480 /// use futures::future::TryFutureExt; 481 /// 482 /// let future = async { Err::<(), _>(1) }; 483 /// let new_future = future.inspect_err(|&x| println!("about to error: {}", x)); 484 /// assert_eq!(new_future.await, Err(1)); 485 /// # }); 486 /// ``` inspect_err<F>(self, f: F) -> InspectErr<Self, F> where F: FnOnce(&Self::Error), Self: Sized,487 fn inspect_err<F>(self, f: F) -> InspectErr<Self, F> 488 where 489 F: FnOnce(&Self::Error), 490 Self: Sized, 491 { 492 assert_future::<Result<Self::Ok, Self::Error>, _>(InspectErr::new(self, f)) 493 } 494 495 /// Flatten the execution of this future when the successful result of this 496 /// future is another future. 497 /// 498 /// This is equivalent to `future.and_then(|x| x)`. try_flatten(self) -> TryFlatten<Self, Self::Ok> where Self::Ok: TryFuture<Error = Self::Error>, Self: Sized,499 fn try_flatten(self) -> TryFlatten<Self, Self::Ok> 500 where 501 Self::Ok: TryFuture<Error = Self::Error>, 502 Self: Sized, 503 { 504 assert_future::<Result<<Self::Ok as TryFuture>::Ok, Self::Error>, _>(TryFlatten::new(self)) 505 } 506 507 /// Flatten the execution of this future when the successful result of this 508 /// future is a stream. 509 /// 510 /// This can be useful when stream initialization is deferred, and it is 511 /// convenient to work with that stream as if stream was available at the 512 /// call site. 513 /// 514 /// Note that this function consumes this future and returns a wrapped 515 /// version of it. 516 /// 517 /// # Examples 518 /// 519 /// ``` 520 /// # futures::executor::block_on(async { 521 /// use futures::future::TryFutureExt; 522 /// use futures::stream::{self, TryStreamExt}; 523 /// 524 /// let stream_items = vec![17, 18, 19].into_iter().map(Ok); 525 /// let future_of_a_stream = async { Ok::<_, ()>(stream::iter(stream_items)) }; 526 /// 527 /// let stream = future_of_a_stream.try_flatten_stream(); 528 /// let list = stream.try_collect::<Vec<_>>().await; 529 /// assert_eq!(list, Ok(vec![17, 18, 19])); 530 /// # }); 531 /// ``` try_flatten_stream(self) -> TryFlattenStream<Self> where Self::Ok: TryStream<Error = Self::Error>, Self: Sized,532 fn try_flatten_stream(self) -> TryFlattenStream<Self> 533 where 534 Self::Ok: TryStream<Error = Self::Error>, 535 Self: Sized, 536 { 537 assert_stream::<Result<<Self::Ok as TryStream>::Ok, Self::Error>, _>(TryFlattenStream::new( 538 self, 539 )) 540 } 541 542 /// Unwraps this future's output, producing a future with this future's 543 /// [`Ok`](TryFuture::Ok) type as its 544 /// [`Output`](std::future::Future::Output) type. 545 /// 546 /// If this future is resolved successfully, the returned future will 547 /// contain the original future's success value as output. Otherwise, the 548 /// closure `f` is called with the error value to produce an alternate 549 /// success value. 550 /// 551 /// This method is similar to the [`Result::unwrap_or_else`] method. 552 /// 553 /// # Examples 554 /// 555 /// ``` 556 /// use futures::future::TryFutureExt; 557 /// 558 /// # futures::executor::block_on(async { 559 /// let future = async { Err::<(), &str>("Boom!") }; 560 /// let future = future.unwrap_or_else(|_| ()); 561 /// assert_eq!(future.await, ()); 562 /// # }); 563 /// ``` unwrap_or_else<F>(self, f: F) -> UnwrapOrElse<Self, F> where Self: Sized, F: FnOnce(Self::Error) -> Self::Ok,564 fn unwrap_or_else<F>(self, f: F) -> UnwrapOrElse<Self, F> 565 where 566 Self: Sized, 567 F: FnOnce(Self::Error) -> Self::Ok, 568 { 569 assert_future::<Self::Ok, _>(UnwrapOrElse::new(self, f)) 570 } 571 572 /// Wraps a [`TryFuture`] into a future compatible with libraries using 573 /// futures 0.1 future definitions. Requires the `compat` feature to enable. 574 #[cfg(feature = "compat")] 575 #[cfg_attr(docsrs, doc(cfg(feature = "compat")))] compat(self) -> Compat<Self> where Self: Sized + Unpin,576 fn compat(self) -> Compat<Self> 577 where 578 Self: Sized + Unpin, 579 { 580 Compat::new(self) 581 } 582 583 /// Wraps a [`TryFuture`] into a type that implements 584 /// [`Future`](std::future::Future). 585 /// 586 /// [`TryFuture`]s currently do not implement the 587 /// [`Future`](std::future::Future) trait due to limitations of the 588 /// compiler. 589 /// 590 /// # Examples 591 /// 592 /// ``` 593 /// use futures::future::{Future, TryFuture, TryFutureExt}; 594 /// 595 /// # type T = i32; 596 /// # type E = (); 597 /// fn make_try_future() -> impl TryFuture<Ok = T, Error = E> { // ... } 598 /// # async { Ok::<i32, ()>(1) } 599 /// # } 600 /// fn take_future(future: impl Future<Output = Result<T, E>>) { /* ... */ } 601 /// 602 /// take_future(make_try_future().into_future()); 603 /// ``` into_future(self) -> IntoFuture<Self> where Self: Sized,604 fn into_future(self) -> IntoFuture<Self> 605 where 606 Self: Sized, 607 { 608 assert_future::<Result<Self::Ok, Self::Error>, _>(IntoFuture::new(self)) 609 } 610 611 /// A convenience method for calling [`TryFuture::try_poll`] on [`Unpin`] 612 /// future types. try_poll_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Result<Self::Ok, Self::Error>> where Self: Unpin,613 fn try_poll_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Result<Self::Ok, Self::Error>> 614 where 615 Self: Unpin, 616 { 617 Pin::new(self).try_poll(cx) 618 } 619 } 620