| 1 |
|
| 2 |
. |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#[macro_use] extern crate custom_derive; |
| 11 |
#[macro_use] extern crate newtype_derive; |
| 12 |
|
| 13 |
use std::fmt::{self, Binary, Debug, Display, LowerExp, LowerHex, Octal, Pointer, |
| 14 |
UpperExp, UpperHex}; |
| 15 |
|
| 16 |
macro_rules! impl_fmt { |
| 17 |
(impl $tr:ident for $name:ident: $msg:expr) => { |
| 18 |
impl $tr for $name { |
| 19 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 20 |
write!(fmt, $msg) |
| 21 |
} |
| 22 |
} |
| 23 |
}; |
| 24 |
} |
| 25 |
|
| 26 |
struct Dummy; |
| 27 |
|
| 28 |
impl_fmt!(impl Binary for Dummy: "binary"); |
| 29 |
impl_fmt!(impl Debug for Dummy: "debug"); |
| 30 |
impl_fmt!(impl Display for Dummy: "display"); |
| 31 |
impl_fmt!(impl LowerExp for Dummy: "lowerexp"); |
| 32 |
impl_fmt!(impl LowerHex for Dummy: "lowerhex"); |
| 33 |
impl_fmt!(impl Octal for Dummy: "octal"); |
| 34 |
impl_fmt!(impl Pointer for Dummy: "pointer"); |
| 35 |
impl_fmt!(impl UpperExp for Dummy: "upperexp"); |
| 36 |
impl_fmt!(impl UpperHex for Dummy: "upperhex"); |
| 37 |
|
| 38 |
custom_derive! { |
| 39 |
#[derive( |
| 40 |
NewtypeBinary, |
| 41 |
NewtypeDebug, |
| 42 |
NewtypeDisplay, |
| 43 |
NewtypeLowerExp, |
| 44 |
NewtypeLowerHex, |
| 45 |
NewtypeOctal, |
| 46 |
NewtypePointer, |
| 47 |
NewtypeUpperExp, |
| 48 |
NewtypeUpperHex |
| 49 |
)] |
| 50 |
struct Wrapper(Dummy); |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn test_fmt() { |
| 55 |
let a = Wrapper(Dummy); |
| 56 |
|
| 57 |
assert_eq!(&*format!("{:b}", a), "binary"); |
| 58 |
assert_eq!(&*format!("{:?}", a), "debug"); |
| 59 |
assert_eq!(&*format!("{}", a), "display"); |
| 60 |
assert_eq!(&*format!("{:e}", a), "lowerexp"); |
| 61 |
assert_eq!(&*format!("{:x}", a), "lowerhex"); |
| 62 |
assert_eq!(&*format!("{:o}", a), "octal"); |
| 63 |
assert_eq!(&*format!("{:p}", a), "pointer"); |
| 64 |
assert_eq!(&*format!("{:E}", a), "upperexp"); |
| 65 |
assert_eq!(&*format!("{:X}", a), "upperhex"); |
| 66 |
} |