Macro serde_plain::derive_serialize_from_display [] [src]

macro_rules! derive_serialize_from_display {
    ($type:ty) => { ... };
}

Derives serde::Serialize a type that implements fmt::Display.

use std::fmt;
#[macro_use] extern crate serde;
#[macro_use] extern crate serde_plain;

pub struct MyStruct(u32);

impl fmt::Display for MyStruct {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

derive_serialize_from_display!(MyStruct);

This automatically implements fmt::Serialize which will invoke the to_string method on the target.