aws_smithy_async::future

Module now_or_later

Source
Expand description

Provides the NowOrLater future with an explicit Now variant

When a future is immediately, ready, this enables avoiding an unnecessary allocation. This is intended to be used with Pin<Box<dyn Future>> or similar as the future variant. For convenience, BoxFuture is provided for this use case.

Typically, this is used when creating a manual async trait. In this case, it’s critical that the lifetime is captured to enable interop with the async-trait macro.

§Examples

mod future {
  use aws_smithy_async::future::now_or_later::{NowOrLater, BoxFuture};
  use std::future::Future;
  pub struct ProvideRegion<'a>(NowOrLater<Option<String>, BoxFuture<'a, Option<String>>>);
  impl<'a> ProvideRegion<'a> {
      pub fn new(f: impl Future<Output = Option<String>> + Send + 'a) -> Self {
          Self(NowOrLater::new(Box::pin(f)))
      }

      pub fn ready(region: Option<String>) -> Self {
          Self(NowOrLater::ready(region))
      }
  }
}

pub trait ProvideRegion {
    fn provide_region<'a>(&'a self) -> future::ProvideRegion<'a> where Self: 'a;
}

struct AsyncRegionProvider;
impl AsyncRegionProvider {
    async fn region(&self) -> Option<String> {
        todo!()
    }
}

impl ProvideRegion for AsyncRegionProvider {
    fn provide_region<'a>(&'a self) -> future::ProvideRegion<'a> where Self: 'a {
      future::ProvideRegion::new(self.region())
    }
}

Structs§

Enums§

  • Zero sized type for using NowOrLater when no future variant exists.

Type Aliases§