1

I am using yup_oauth2 and attempting to create a Token source that implements the GetToken trait for use on Google Compute Engine.

pub trait GetToken {
    fn token<'b, I, T>(&mut self, scopes: I) -> Result<Token, Box<Error>>
    where
        T: AsRef<str> + Ord + 'b,
        I: IntoIterator<Item = &'b T>;

    fn api_key(&mut self) -> Option<String>;
}

Depending on where the application is running, I want to resolve to different types that implement the above trait but the compiler is not happy because apparently traits with generics are not trait-object safe.

What is the idiomatic way of solving such a problem?

cobie
  • 7,023
  • 11
  • 38
  • 60
  • It looks like your question might be answered by the answers of [Is it possible to have a generic function on a trait?](https://stackoverflow.com/q/49952612/155423); [Why does a generic method inside a trait require trait object to be sized?](https://stackoverflow.com/q/42620022/155423); [How to use a trait object to refer to struct that has generic methods](https://stackoverflow.com/q/29202698/155423); etc. If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jun 26 '19 at 18:31
  • TL;DR: no you cannot have a trait object with generic methods, *period*. Make things more concrete. – Shepmaster Jun 26 '19 at 18:31
  • decided to go with a function signature such as `pub fn find_default_credentials( ) -> Result, GCETokenProvider>, ReadCredentialError> ` – cobie Jun 26 '19 at 19:13

0 Answers0