@@ -40,15 +40,17 @@ impl Error for CredentialsError {
40
40
}
41
41
42
42
pub type CredentialsResult = Result < Credentials , CredentialsError > ;
43
- type BoxFuture < T > = Pin < Box < dyn Future < Output = T > + Send > > ;
43
+ type BoxFuture < ' a , T > = Pin < Box < dyn Future < Output = T > + Send + ' a > > ;
44
44
45
45
/// An asynchronous credentials provider
46
46
///
47
47
/// If your use-case is synchronous, you should implement [`ProvideCredentials`] instead. Otherwise,
48
48
/// consider using [`async_provide_credentials_fn`] with a closure rather than directly implementing
49
49
/// this trait.
50
50
pub trait AsyncProvideCredentials : Send + Sync {
51
- fn provide_credentials ( & self ) -> BoxFuture < CredentialsResult > ;
51
+ fn provide_credentials < ' a > ( & ' a self ) -> BoxFuture < ' a , CredentialsResult >
52
+ where
53
+ Self : ' a ;
52
54
}
53
55
54
56
pub type CredentialsProvider = Arc < dyn AsyncProvideCredentials > ;
66
68
T : Fn ( ) -> F + Send + Sync ,
67
69
F : Future < Output = CredentialsResult > + Send + ' static ,
68
70
{
69
- fn provide_credentials ( & self ) -> BoxFuture < CredentialsResult > {
71
+ fn provide_credentials < ' a > ( & ' a self ) -> BoxFuture < ' a , CredentialsResult >
72
+ where
73
+ Self : ' a ,
74
+ {
70
75
Box :: pin ( ( self . f ) ( ) )
71
76
}
72
77
}
81
86
/// use aws_auth::Credentials;
82
87
/// use aws_auth::provider::async_provide_credentials_fn;
83
88
///
89
+ /// async fn load_credentials() -> Credentials {
90
+ /// todo!()
91
+ /// }
92
+ ///
84
93
/// async_provide_credentials_fn(|| async {
85
94
/// // Async process to retrieve credentials goes here
86
- /// let credentials: Credentials = todo! ().await? ;
95
+ /// let credentials = load_credentials ().await;
87
96
/// Ok(credentials)
88
97
/// });
89
98
/// ```
@@ -107,7 +116,10 @@ impl<T> AsyncProvideCredentials for T
107
116
where
108
117
T : ProvideCredentials ,
109
118
{
110
- fn provide_credentials ( & self ) -> BoxFuture < CredentialsResult > {
119
+ fn provide_credentials < ' a > ( & ' a self ) -> BoxFuture < ' a , CredentialsResult >
120
+ where
121
+ Self : ' a ,
122
+ {
111
123
let result = self . provide_credentials ( ) ;
112
124
Box :: pin ( future:: ready ( result) )
113
125
}
@@ -130,12 +142,33 @@ pub fn set_provider(config: &mut PropertyBag, provider: Arc<dyn AsyncProvideCred
130
142
131
143
#[ cfg( test) ]
132
144
mod test {
145
+ use crate :: provider:: { AsyncProvideCredentials , BoxFuture , CredentialsResult } ;
133
146
use crate :: Credentials ;
147
+ use async_trait:: async_trait;
134
148
135
149
fn assert_send_sync < T : Send + Sync > ( ) { }
136
150
137
151
#[ test]
138
152
fn creds_are_send_sync ( ) {
139
153
assert_send_sync :: < Credentials > ( )
140
154
}
155
+
156
+ #[ async_trait]
157
+ trait AnotherTrait : Send + Sync {
158
+ async fn creds ( & self ) -> Credentials ;
159
+ }
160
+
161
+ struct AnotherTraitWrapper < T > {
162
+ inner : T ,
163
+ }
164
+
165
+ impl < T : AnotherTrait > AsyncProvideCredentials for AnotherTraitWrapper < T > {
166
+ fn provide_credentials < ' a > ( & ' a self ) -> BoxFuture < ' a , CredentialsResult >
167
+ where
168
+ Self : ' a ,
169
+ {
170
+ let inner_fut = self . inner . creds ( ) ;
171
+ Box :: pin ( async move { Ok ( inner_fut. await ) } )
172
+ }
173
+ }
141
174
}
0 commit comments