Skip to main content

new_type

Macro new_type 

Source
macro_rules! new_type {
    (
        $(#[$attr:meta])*
        $name:ident(
            $(#[$type_attr:meta])*
            $type:ty
        )
    ) => { ... };
    (
        $(#[$attr:meta])*
        $name:ident(
            $(#[$type_attr:meta])*
            $type:ty
        )
        impl {
            $($item:tt)*
        }
    ) => { ... };
    (
        @new_type $(#[$attr:meta])*,
        $name:ident(
            $(#[$type_attr:meta])*
            $type:ty
        ),
        $new_doc:expr,
        impl {
            $($item:tt)*
        }
    ) => { ... };
}
Expand description

Creates a newtype with following code automatically generated:

  • Derives Clone, Debug, PartialEq, Eq, PartialOrd, Ord, and Display.
  • Implements new, Deref, and From.
  • Allows one to implement additional traits and features as needed.

This was copied from the oauth2-rs crate.

ยงExamples

new_type! [
    /// A safe wrapper around a `user_id` string
    #[derive(Eq, Hash)]
    UserId(String)
    impl {
        fn as_str(&self) -> &str {
            &self
        }
    }
];