Skip to content

Instantly share code, notes, and snippets.

@Sgeo
Created August 13, 2016 04:01
Show Gist options
  • Save Sgeo/02eddabff0906a6b4408921ceaea2509 to your computer and use it in GitHub Desktop.
Save Sgeo/02eddabff0906a6b4408921ceaea2509 to your computer and use it in GitHub Desktop.
#![feature(specialization)]
#![allow(dead_code)]
struct Here;
struct There<T>(T);
struct Nil;
struct Cons<Head, Index, Tail>(Head, Index, Tail);
trait ContainsAt<T, I> {
fn get_(&self) -> &T;
}
trait Contains<T> {
fn get(&self) -> &T;
}
impl<T, Tail, Index> ContainsAt<T, Index> for Cons<T, Index, Tail> {
fn get_(&self) -> &T {
&self.0
}
}
impl<T, TailI, Head, Tail: ContainsAt<T, TailI>> ContainsAt<T, There<TailI>> for Cons<Head, There<TailI>, Tail> {
fn get_(&self) -> &T {
&self.2.get_()
}
}
impl<T, Head, Tail, I> Contains<T> for Cons<Head, I, Tail>
where Cons<Head, I, Tail>: ContainsAt<T, I> {
default fn get(&self) -> &T {
self.get_()
}
}
impl<T, Head, Tail, TailI> Contains<T> for Cons<Head, There<TailI>, Tail>
where Cons<Head, There<TailI>, Tail>: ContainsAt<T, There<TailI>> {
fn get(&self) -> &T {
self.get_()
}
}
fn main() {
fn show_i32<T: ContainsAt<i32, There<There<Here>>>>(t: T) {
println!("{}", t.get());
}
let list1 = Cons(1i32, There(There(Here)), Cons(6i64, There(Here), Cons("Boo!", Here, Nil)));
show_i32(list1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment