Skip to content

Instantly share code, notes, and snippets.

@trha
Created May 17, 2019 11:01
Show Gist options
  • Select an option

  • Save trha/ea53d90f798e52013c1faead4814bde8 to your computer and use it in GitHub Desktop.

Select an option

Save trha/ea53d90f798e52013c1faead4814bde8 to your computer and use it in GitHub Desktop.
use frunk::{
hlist::{HCons, HNil},
labelled::{Field, LabelledGeneric},
};
use frunk_derives::LabelledGeneric;
trait GShow<T> {
fn gshow(self) -> String;
}
trait Show<T> {
fn show(self) -> String;
}
#[derive(LabelledGeneric)]
struct A {
a: String,
b: i32,
}
fn main() {
println!(
"{}",
A {
a: "hello".into(),
b: 32
}
.show()
);
}
impl<N, T, Tail, A, B> GShow<HCons<A, B>> for HCons<Field<N, T>, Tail>
where
Tail: GShow<B>,
T: Show<A>,
{
fn gshow(self) -> String {
let (field, tail) = self.pop();
format!("{}: {}, {}", field.name, field.value.show(), tail.gshow())
}
}
impl GShow<HNil> for HNil {
fn gshow(self) -> String {
String::new()
}
}
impl Show<HNil> for String {
fn show(self) -> String {
self
}
}
impl Show<HNil> for i32 {
fn show(self) -> String {
self.to_string()
}
}
impl<T, A, B> Show<HCons<A, B>> for T
where
T: LabelledGeneric,
T::Repr: GShow<HCons<A, B>>,
{
fn show(self) -> String {
LabelledGeneric::into(self).gshow()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment