I want a trait to retrieve Ref
from Cache
. It’s easy to retrieve Ref<T>
from Cache
.
impl<T> Foo for T {
fn foo(…) -> Ref<'_, Self> …
}
Then, I want to retrieve (Ref<T1>, Ref<T2>, )
from Cache
, which leads me to use macro to achieve the goal.
Then I should implement the trait for (T1, T2,)
, and return (Ref<T1>, Ref<T2>, )
. I certainly need associated type to claim type Refs = (Ref<T1>, Ref<T2>, )
or directly claiming it like Foo<(Ref<T1>, Ref<T2>, )>
.
#![allow(unused)]
use std::marker::PhantomData;
trait Foo<T> {
// just use &Self instead of &Cache
fn foo(&self) -> T;
}
struct Ref<'a, T> { _marker: PhantomData<&'a T> }
impl<'a, T> Foo<Ref<'a, T>> for T {
fn foo(&self) -> Ref<'a, T> {
todo!()
}
}
impl<'a, T> Foo<(Ref<'a, T>,)> for (T,) {
fn foo(&self) -> (Ref<'a, T>,) {
todo!()
}
}
trait Bar<'a> {
type Ref;
fn bar(&self) -> Self::Ref;
}
impl<'a, T: 'a> Bar<'a> for T {
type Ref = Ref<'a, T>;
fn bar(&self) -> Self::Ref {
todo!()
}
}
// Err conflicting implementations
impl<'a, T:'a> Bar<'a> for (T,) {
type Ref = (Ref<'a, T>,);
fn bar(&self) -> Self::Ref {
todo!()
}
}
Above, to implement the trait with macro, I certainly should choose Foo
instead of Bar
’s associated type. Am I right or there’s better way?