Created
December 27, 2017 08:47
-
-
Save Pierozi/aea80a27b400d4032146bedf40fe772a to your computer and use it in GitHub Desktop.
Genric trait implementation type hint issue in RUST Lang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Provide { | |
| fn credentials(&self) -> bool; | |
| } | |
| struct StaticProvider { | |
| foo: String | |
| } | |
| impl StaticProvider { | |
| pub fn new(value: String) -> StaticProvider { | |
| return StaticProvider { | |
| foo: value, | |
| } | |
| } | |
| } | |
| impl Provide for StaticProvider { | |
| fn credentials(&self) -> bool { | |
| return self.foo == "foo"; | |
| } | |
| } | |
| struct BasicProvider { | |
| bar: String | |
| } | |
| impl BasicProvider { | |
| pub fn new(value: String) -> BasicProvider { | |
| return BasicProvider { | |
| bar: value, | |
| } | |
| } | |
| } | |
| impl Provide for BasicProvider { | |
| fn credentials(&self) -> bool { | |
| return self.bar == "bar"; | |
| } | |
| } | |
| struct Client<P> | |
| where P: Provide | |
| { | |
| provider: P | |
| } | |
| impl<P> Client<P> | |
| where P: Provide | |
| { | |
| pub fn new(provider: P) -> Self { | |
| return Client { | |
| provider: provider, | |
| } | |
| } | |
| } | |
| pub fn main() { | |
| let dilema = "static"; | |
| // How can I type hint this variable? | |
| // Considering that I do not have control on Client and Provider in real life | |
| // because it's generated code inside Rusto library (https://github.com/rusoto/rusoto) | |
| let client = if dilema == "static" { | |
| let provider = StaticProvider::new("foo".to_owned()); | |
| Client::new(provider); | |
| } else { | |
| let provider = BasicProvider::new("bar".to_owned()); | |
| Client::new(provider); | |
| }; | |
| worker(client); | |
| /*if dilema == "static" { | |
| println!("foo: {}", client.provider.foo); | |
| } else { | |
| println!("bar: {}", client.provider.bar); | |
| }*/ | |
| } | |
| // Here is my worker method that expect client to avoid evaluate dilema on all worker | |
| pub fn worker(client: Client<???????>) { | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment