Macro wiwi::nom_mod

source ·
macro_rules! nom_mod {
    {
		$(
			$mod_vis:vis mod $mod_name:ident {
				$( nom!($item_vis:vis $name:ident, wraps: $( ref <$($lifetimes:lifetime),+> )? $type:ty); )*
			}
		)*
	} => { ... };
}
Expand description

Declare many new nominal types (aliases), in a module

Usage is more or less identical to nom, but you define a module inside the macro invocation. Because this macro creates a new module (with the name you specify), and the created module is only used for defining these nominal types, there can be nothing else in there, which we take advantage of to create a marker submodule to define marker types in. This way it can have a new namespace just for the marker types, so reusing the newtype name won’t collide with anything else.

So, all of that is to say this macro also saves you a bit of boilerplate declaring names for the newtype ZSTs.

§Examples

nom_mod! {
   pub mod nom {
      nom!(pub NewType, wraps: String);
   }
}

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

Still creating newtypes as expected:

nom_mod! {
   pub mod nom {
      nom!(pub NewType, wraps: String);
      nom!(pub AnotherNewType, wraps: String);
   }
}

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

Still “just” a type alias:


let item: nom::NewType = Nom::new(String::new());

Created marker structs are in a marker submodule:


let item: Nom<String, nom::marker::NewType> = nom::NewType::new(String::new());
let item: Nom<String, nom::marker::AnotherNewType> = nom::AnotherNewType::new(String::new());