rust - Implementing trait for multiple newtypes at once -
i have bunch newtypes wrapping string
object:
#[deriving(show)] struct is(pub string); #[deriving(show)] struct hd(pub string); #[deriving(show)] struct ei(pub string); #[deriving(show)] struct rp(pub string); #[deriving(show)] struct pl(pub string);
now, #[deriving(show)]
, produces next output: ei(mystringhere)
, , output mystringhere
. implementing show
explicitly works, there way implement these newtypes @ once?
there no such way in language itself, can employ macros easily:
#![feature(macro_rules)] struct is(pub string); struct hd(pub string); struct ei(pub string); struct rp(pub string); struct pl(pub string); macro_rules! newtype_show( ($($t:ty),+) => ($( impl ::std::fmt::show $t { fn fmt(&self, f: &mut ::std::fmt::formatter) -> ::std::fmt::result { write!(f, "{}", self.0[]) } } )+) ) newtype_show!(is, hd, ei, rp, pl) fn main() { allow hd = hd("abcd".to_string()); println!("{}", hd); }
(try here)
rust
No comments:
Post a Comment