Skip to content

Instantly share code, notes, and snippets.

@willcrichton
Created June 22, 2020 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willcrichton/e7fcf1b0e84b4be7f172f0e0245cf149 to your computer and use it in GitHub Desktop.
Save willcrichton/e7fcf1b0e84b4be7f172f0e0245cf149 to your computer and use it in GitHub Desktop.
use frunk::hlist::*;
use std::marker::PhantomData;
pub struct FString(&'static str);
pub struct FVar;
trait Format<ArgList> {
fn format(&self, args: ArgList) -> String;
}
impl Format<HNil> for HNil {
fn format(&self, _args: HNil) -> String {
"".to_string()
}
}
impl<ArgList, FmtList> Format<ArgList> for HCons<FString, FmtList>
where
FmtList: Format<ArgList>,
{
fn format(&self, args: ArgList) -> String {
self.head.0.to_owned() + &self.tail.format(args)
}
}
impl<T, ArgList, FmtList> Format<HCons<T, ArgList>> for HCons<FVar, FmtList>
where
FmtList: Format<ArgList>,
T: ToString,
{
fn format(&self, args: HCons<T, ArgList>) -> String {
args.head.to_string() + &self.tail.format(args.tail)
}
}
#[test]
fn test() {
use frunk::hlist;
let fmt = hlist![FString("Hello "), FVar];
assert_eq!(fmt.format(hlist!["world"]), "Hello world");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment