Skip to content

Instantly share code, notes, and snippets.

@satellitex
Created April 18, 2019 16:29
Show Gist options
  • Save satellitex/a3436f6b19ccb990b84d4d938513e049 to your computer and use it in GitHub Desktop.
Save satellitex/a3436f6b19ccb990b84d4d938513e049 to your computer and use it in GitHub Desktop.
サブトレイトで使いたい関数を呼んでメイントレイトに変換する。
use std::marker::PhantomData;
pub trait Trait {
type A: Clone + std::fmt::Debug + From<(u64,String)>;
type Sub: Subtrait<Self::A>;
}
pub struct Struct;
impl Trait for Struct {
type A = (u64, String);
type Sub = SubStruct<Self::A>;
}
pub trait Subtrait<A> {
type A: From<A> + Into<A> + From<(u64,String)>;
fn get(&self) -> Self::A;
}
pub struct SubStruct<A> {
a: u64,
b: String,
_a: PhantomData<A>,
}
impl<A: From<(u64, String)>> Subtrait<A> for SubStruct<A> {
type A = A;
fn get(&self) -> Self::A {
Self::A::from((self.a.clone(),self.b.clone()))
}
}
pub fn conv<T: Trait>(a: SubStruct<T::A>) -> T::A {
a.get().into()
}
fn main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment