here is a simple example of using an Associated type in a trait:
trait Female{
fn complain(&self) -> ();
}
trait Companion{
type F: Female;
fn get_comfort(&self, Self::F) -> ();
}
struct Cat{
}
impl Female for Cat{
fn complain(&self) -> () {
println!("it sucks to be a cat");
}
}
struct Dog{
}
impl Companion for Dog{
type F = Cat;
fn get_comfort(&self, companion: Self::F) -> () {
companion.complain();
}
}
fn main(){
let dog = Dog{};
let cat = Cat{};
dog.get_comfort(cat);
}
the other requirement i have is to make the Associated type a From trait, the reason being I need to pass a certain type of object (lets call it Foo) through a channel, and i want one of the traits methods to receive an object which can turn into Foo. so I end up with the following which isn’t allowed because i probably must put a concrete type as the Associated type. help?
trait Female{
fn complain(&self) -> ();
}
trait Companion{
type F: Female;
fn get_comfort(&self, Self::F) -> ();
}
struct Cat{
}
struct Dog{
}
struct SuperCat{
}
impl Female for SuperCat{
fn complain(&self) -> () {
println!("it sucks to be a cat");
}
}
impl From<Cat> for SuperCat{
fn from(cat: Cat) -> Self {
SuperCat{}
}
}
impl Companion for Dog{
**type F = From<Cat>;**
fn get_comfort(&self, companion: Self::F) -> () {
println("cant get this to work")
}
}
fn main(){
let dog = Dog{};
let cat = Cat{};
dog.get_comfort(cat);
}