Skip to content

Instantly share code, notes, and snippets.

@willcrichton willcrichton/rust_printf.rs Secret

Created Jun 22, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.