wiwi

Macro nominal

Source
macro_rules! nominal {
    ($vis:vis $name:ident, marker: $marker:ident, wraps: $( ref <$($lifetimes:lifetime),+> )? $ty:ty) => { ... };
}
Expand description

Declare a new nominal type (alias), with the provided name, a name for the marker type struct, and the wrapped type

The returned type alias will be of a guaranteed unique type. This is done by creating a new ZST with the provided marker type struct name.

The name of the new marker type struct is something we hope to eventually be able to generate automatically from the given newtype name. If there is a way, we don’t know how >~<

§Examples

Basic usage:

// type NewType = Nominal<String, NewTypeMarker>;
nominal!(NewType, marker: NewTypeMarker, wraps: String);

// these two are identical
let item: NewType = NewType::new(String::new());
let item: Nominal<String, NewTypeMarker> = Nominal::new(String::new());

// and of course, it's a type alias
let item: NewType = Nominal::new(String::new());
let item: Nominal<String, NewTypeMarker> = NewType::new(String::new());

The macro does indeed create a unique newtype:

nominal!(AnotherNewType, marker: AnotherNewTypeMarker, wraps: String);

let item: NewType = NewType::new(String::new());
// this won't compile
let another_item: NewType = AnotherNewType::new(String::new());

Controlling visibility of the type alias / marker struct:

mod inner {
   nominal!(pub NewType, marker: NewTypeMarker, wraps: String);
   //   ↑
}

let item = inner::NewType::new(String::new());

Private visibility is default (like the rest of Rust visibilities):

mod inner {
   nominal!(NewType, marker: NewTypeMarker, wraps: String);
   //       ↑ no `pub`
}

// this won't compile
let item = inner::NewType::new(String::new());

Other visibilities work too, of course:

mod outer {
   mod inner {
      nominal!(pub(super) NewType, marker: NewTypeMarker, wraps: String);
   }

   // this is fine...
   let item = inner::NewType::new(String::new());
}

// but this won't compile
let item = outer::inner::NewType::new(String::new());