aws_credential_types/provider/
future.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Convenience `ProvideCredentials` struct that implements the `ProvideCredentials` trait.
7
8use crate::provider::token::Result as TokenResult;
9use crate::provider::Result as CredsResult;
10use aws_smithy_async::future::now_or_later::NowOrLater;
11use std::future::Future;
12use std::pin::Pin;
13use std::task::{Context, Poll};
14
15type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
16
17/// Future new-type that `ProvideCredentials::provide_credentials` must return.
18#[derive(Debug)]
19pub struct ProvideCredentials<'a>(NowOrLater<CredsResult, BoxFuture<'a, CredsResult>>);
20
21impl<'a> ProvideCredentials<'a> {
22    /// Creates a `ProvideCredentials` struct from a future.
23    pub fn new(future: impl Future<Output = CredsResult> + Send + 'a) -> Self {
24        ProvideCredentials(NowOrLater::new(Box::pin(future)))
25    }
26
27    /// Creates a `ProvideCredentials` struct from a resolved credentials value.
28    pub fn ready(credentials: CredsResult) -> Self {
29        ProvideCredentials(NowOrLater::ready(credentials))
30    }
31}
32
33impl Future for ProvideCredentials<'_> {
34    type Output = CredsResult;
35
36    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
37        Pin::new(&mut self.0).poll(cx)
38    }
39}
40
41/// Future new-type that `ProvideToken::provide_token` must return.
42#[derive(Debug)]
43pub struct ProvideToken<'a>(NowOrLater<TokenResult, BoxFuture<'a, TokenResult>>);
44
45impl<'a> ProvideToken<'a> {
46    /// Creates a `ProvideToken` struct from a future.
47    pub fn new(future: impl Future<Output = TokenResult> + Send + 'a) -> Self {
48        ProvideToken(NowOrLater::new(Box::pin(future)))
49    }
50
51    /// Creates a `ProvideToken` struct from a resolved credentials value.
52    pub fn ready(credentials: TokenResult) -> Self {
53        ProvideToken(NowOrLater::ready(credentials))
54    }
55}
56
57impl Future for ProvideToken<'_> {
58    type Output = TokenResult;
59
60    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
61        Pin::new(&mut self.0).poll(cx)
62    }
63}