-
-
Save willcrichton/e7fcf1b0e84b4be7f172f0e0245cf149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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