Modify GetToken::token.

Change it to accept an iterator of items that can be converted to
`String`s rather than an iterator of items that can be referenced as
`&str`s.

Primarily this allows it to be called with a larger variety of inputs.
For example ::std::env::args().skip(1) can now be passed directly to
token, where before it would first need to be collected into a vector.

Since all implementations unconditionally collected the iterator into a
vector this shouldn't have any negative impact on performance and should
actually reduce the number of allocations in some uses.

It simplifies the signature since the lifetime bounds are no longer
required.
This commit is contained in:
Glenn Griffin
2019-08-08 14:21:21 -07:00
parent 460af32922
commit 2b18f3679e
6 changed files with 32 additions and 36 deletions

View File

@@ -61,15 +61,15 @@ where
impl<FD: FlowDelegate + 'static + Send + Clone, C: hyper::client::connect::Connect + 'static>
GetToken for InstalledFlow<FD, C>
{
fn token<'b, I, T>(
fn token<I, T>(
&mut self,
scopes: I,
) -> Box<dyn Future<Item = Token, Error = RequestError> + Send>
where
T: AsRef<str> + Ord + 'b,
I: Iterator<Item = &'b T>,
T: Into<String>,
I: IntoIterator<Item = T>,
{
Box::new(self.obtain_token(scopes.into_iter().map(|s| s.as_ref().to_string()).collect()))
Box::new(self.obtain_token(scopes.into_iter().map(Into::into).collect()))
}
fn api_key(&mut self) -> Option<String> {
None
@@ -625,7 +625,7 @@ mod tests {
.create();
let fut = inf
.token(vec!["https://googleapis.com/some/scope"].iter())
.token(vec!["https://googleapis.com/some/scope"])
.and_then(|tok| {
assert_eq!("accesstoken", tok.access_token);
assert_eq!("refreshtoken", tok.refresh_token);
@@ -653,7 +653,7 @@ mod tests {
.create();
let fut = inf
.token(vec!["https://googleapis.com/some/scope"].iter())
.token(vec!["https://googleapis.com/some/scope"])
.and_then(|tok| {
assert_eq!("accesstoken", tok.access_token);
assert_eq!("refreshtoken", tok.refresh_token);
@@ -675,7 +675,7 @@ mod tests {
.create();
let fut = inf
.token(vec!["https://googleapis.com/some/scope"].iter())
.token(vec!["https://googleapis.com/some/scope"])
.then(|tokr| {
assert!(tokr.is_err());
assert!(format!("{}", tokr.unwrap_err()).contains("invalid_code"));