Skip to content

Instantly share code, notes, and snippets.

@marcbowes
Last active August 29, 2015 13:56
Show Gist options
  • Save marcbowes/8940968 to your computer and use it in GitHub Desktop.
Save marcbowes/8940968 to your computer and use it in GitHub Desktop.
> 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
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();
}
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
}
}
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
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