Macro wiwi::nom

source ·
macro_rules! nom {
    ($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 = Nom<String, NewTypeMarker>;
nom!(NewType, marker: NewTypeMarker, wraps: String);

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

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

The macro does indeed create a unique newtype:

nom!(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 {
   nom!(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 {
   nom!(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 {
      nom!(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());