I have two structs, A
and B
, that implement a common trait X
. A
and B
should also implement some traits from the standard library, in particular Eq
, Copy
, Hash
and From
. So, I ended up with this code:
use std::hash::Hash;
#[derive(Eq, PartialEq, Copy, Clone, Hash)]
struct A;
#[derive(Eq, PartialEq, Copy, Clone, Hash)]
struct B;
trait X : Eq + Copy + Hash {
fn useful(self);
}
impl X for A {
fn useful(self) {}
}
impl X for B {
fn useful(self) {}
}
This is nice, because I can now use trait X in generic functions to abstract over A or B:
fn do_useful_thing<T: X>(arg: T) {
arg.useful();
}
Now, I don't know how to deal with From
because I need to use a lifetime parameter for him. What I want to do is this:
trait X : Eq + Copy + Hash + From<&[u8]>
This doesn't work (error: missing lifetime specifier)
trait X : Eq + Copy + Hash + From<&'a [u8]>
This doesn't work either (use of undeclared lifetime name 'a
)
trait X<'a> : Eq + Copy + Hash + From<&'a [u8]>
This works, but now I have to add a lifetime parameter everywhere I use X.
impl<'a> X<'a> for A { ...
fn do_useful_thing<'a, T: X<'a>>(arg: T) { ...
How can I define my trait X, so that it can be used easily and encompasses all the capabilities of my structs?
Thanks!