Derive Macro amplify::Display [−][src]
#[derive(Display)] { // Attributes available to this derive: #[display] }
Expand description
Usage
- Generate
Display
descriptions using other formatting trait:#[derive(Display, Debug)] #[display(Debug)] enum Some { Once, Twice(u8) }
- Use existing function for displaying descriptions:
Formatting function must return
#[macro_use] extern crate amplify; #[derive(Display)] #[display(Int::print)] union Int { uint: u32, int: i32 }; impl Int { pub fn print(&self) -> String { s!("Integer representation") } } pub trait ToSpecialString { fn to_special_string(&self) -> String { s!("Special string") } } #[derive(Display)] #[display(Some::to_special_string)] struct Some { uint: u32, int: i32 }; impl ToSpecialString for Some {} assert_eq!( format!("{}", Int { uint: 2 }), s!("Integer representation") ); #[derive(Display)] #[display(some_fmt)] enum Enum { Once(u8), Twice }; fn some_fmt(_: &Enum) -> String { s!("Some") } assert_eq!(format!("{}", Enum::Once(3)), s!("Some"))
String
and take a singleself
argument (if you need formatting with streamed output, use one of existing formatting traits as shown in pt. 1). - Custom format string:
#[derive(Display)] #[display("({x}, {y})")] struct Point { x: u32, y: u32 } assert_eq!(format!("{}", Point { x: 0, y: 1 }), "(0, 1)");
- Support for alternative formatting with
alt
parameter:#[derive(Display)] #[display("({x}, {y})", alt = "{x}:{y}")] struct Point { x: u32, y: u32 } assert_eq!(format!("{}", Point { x: 0, y: 1 }), "(0, 1)"); assert_eq!(format!("{:#}", Point { x: 0, y: 1 }), "0:1");
- Use of doc comments for descrition representation. In this case doc
comments may also contain formatting like in the case 3:
You can also mix in this mode with other fors of display tags on a specific options; in this case doc comments are ignored
#[macro_use] extern crate amplify; /// Example enum with doc comments converted into display #[derive(Display)] #[display(doc_comments)] enum Variants { /// Letter A. /// Multiline comments are also working A, /// Letter B B, /// This comment is ignored #[display("Letter C")] C, /// Letter {0} Letter(String), /// You can omit parameters and just have a normal doc comment Number(u8), /// ... for variants with named fields as well Named { some: String } }; assert_eq!( format!("{}", Variants::A), "Letter A. Multiline comments are also working" ); assert_eq!(format!("{}", Variants::C), "Letter C"); assert_eq!(format!("{}", Variants::Letter(s!("K"))), "Letter K");
- Support of unit structs:
/// Some unit struct #[derive(Clone, Debug, Display, Error)] #[display(doc_comments)] pub struct UnitStruct;
Example
Advanced use with enums:
#[derive(Display)] enum Test { Some, #[display("OtherName")] Other, /// Document comment working as display string Commented, Named { x: u8, }, #[display("Custom{x}", alt = "this is alternative")] NamedCustom { x: u8, }, #[display(inner)] Inner { a: String, }, Unnamed(u16), // NB: Use indexes for tuple values #[display("Custom{0}")] UnnamedCustom(String), } assert_eq!(format!("{}", Test::Some), "Some"); assert_eq!(format!("{}", Test::Other), "OtherName"); assert_eq!(format!("{}", Test::Named { x: 1 }), "Named { .. }"); assert_eq!(format!("{}", Test::Unnamed(5)), "Unnamed(..)"); assert_eq!(format!("{}", Test::NamedCustom { x: 8 }), "Custom8"); assert_eq!( format!("{:#}", Test::NamedCustom { x: 8 }), "this is alternative" ); assert_eq!( format!("{}", Test::UnnamedCustom("Test".to_string())), "CustomTest" );
Use with tuple types:
#[derive(Clone, Copy, Debug, Display)] #[display("{0}")] struct Tuple(u8); #[derive(Clone, Copy, Debug, Display)] #[display(inner)] // `inner` is synonym to "{0}" struct Tuple2(u8); assert_eq!(format!("{}", Tuple(5)), format!("{}", Tuple2(5)))
Using inner enum variant representation, defaulting to the variant name if the variant does not have inner data:
use std::net::{IpAddr, Ipv4Addr}; #[derive(Clone, Copy, Debug, Display)] #[display(inner)] // `inner` is synonym to "{0}" enum Variants { First, Second, WithData(u8), WithComplexData(IpAddr), }; assert_eq!(Variants::First.to_string(), "First"); assert_eq!(Variants::WithData(5).to_string(), "5"); assert_eq!( Variants::WithComplexData(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))).to_string(), "127.0.0.1" );