Last active
August 29, 2015 13:56
-
-
Save marcbowes/8940968 to your computer and use it in GitHub Desktop.
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
> rustc templated-traits.rs | |
templated-traits.rs:37:5: 37:19 error: type `Client<,ClientConfig>` does not implement any method in scope named `ping` | |
templated-traits.rs:37 client.ping(); | |
^~~~~~~~~~~~~~ | |
error: aborting due to previous error |
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
pub enum ClientConfig { | |
TestingConfig, | |
ProductionConfig | |
} | |
pub struct PingResponse; | |
pub trait ServiceInterface { | |
fn ping(&self) -> PingResponse; | |
} | |
pub struct Client<'a, T> { | |
some_dependency: &'a T | |
} | |
impl<'a, T> Client<'a, T> { | |
pub fn new(inject_dependency: &'a T) -> Client<'a, T> { | |
Client { some_dependency: inject_dependency } | |
} | |
} | |
impl<'a, T> ServiceInterface for Client<'a, T> { | |
fn ping(&self) -> PingResponse { | |
PingResponse | |
} | |
} | |
pub fn main() { | |
let config = TestingConfig; | |
let client = Client::new(&config); | |
client.ping(); | |
} |
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
impl<'a, T> Client<'a, T> { | |
pub fn new(inject_dependency: &'a T) -> ~ServiceInterface { | |
~Client { some_dependency: inject_dependency } as ~ServiceInterface | |
} | |
} | |
impl<'a, T> Client<'a, T> { | |
pub fn new(inject_dependency: &'a T) -> ~ServiceInterface { | |
~Client { some_dependency: inject_dependency } as ~ServiceInterface | |
} | |
} |
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
templated-traits.rs:24:9: 24:16 error: value may contain references; add `'static` bound | |
templated-traits.rs:24 ~Client { some_dependency: inject_dependency } as ~ServiceInterface | |
^~~~~~~ | |
templated-traits.rs:24:9: 24:16 error: cannot pack type `~Client<,T>`, which does not fulfill `Send`, as a trait bounded by Send | |
templated-traits.rs:24 ~Client { some_dependency: inject_dependency } as ~ServiceInterface | |
^~~~~~~ | |
error: aborting due to 2 previous errors |
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
Questions: | |
1) Why do I have to return a ~ServiceInterface? | |
2) Does this change with DST? | |
3) Why does it fail to compile? | |
4) Is this (return a trait rather than the client) reasonable? I figure it's a good idea for testing *. | |
5) What else am I doing wrong here? | |
* assume Client makes a network connection and I want consumers to be able to swap in a test implementation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment