Newtypes
The newtypes crate is meant to ease the implementation of the
Newtype
pattern while trying to make as few assumptions as possible.
Layout guarantee
Every type produced by every macro in this crate is annotated with
#[repr(transparent)],
so its size and alignment match the inner type's. This is a stable part of
the crate's contract and will not be removed.
newtype! Macro
The newtype! macro creates a barebones newtype by wrapping an inner type with
a struct, and, if possible, implementing some extra traits for it.
By default, all declared structs have visibility pub.
use newtype;
newtype!;
// In case we want to automatically derive traits (like serde's
// Serialize or Deserialize):
newtype!;
Example:
use newtype;
use
newtype!;
newtype!;
Implemented traits
- For all:
- For integers:
- For floating point numbers:
- For
String:
IMPORTANT: The inner value is private. If there are no constraints on the inner value, then we can rely on
newtype_from!macro to implement theFromandFromStrtraits for us.Otherwise we can choose to implement
From,FromStrandTryFrommanually.
newtype_ord! Macro
The newtype_ord! macro extends the functionality provided with
newtype! by implementing the
PartialOrd and
Ord traits
(when possible).
Example:
use newtype_ord;
use
newtype_ord!;
newtype_ord!;
NOTE: It only works for integers, floating point numbers, and
String.
newtype_unit! Macro
The newtype_unit! macro extends the functionality provided with
newtype_ord! by implementing the
Add,
Sub,
AddAssign,
SubAssign, and
Default traits.
Example:
use newtype_unit;
use Deserialize;
newtype_unit!;
newtype_unit!;
NOTE: It only works for integers and floating point numbers.
NOTE: It does not implement arithmetic operations beyond addition and subtraction. Doing that properly would require a more complex library focused on dealing with "units" (example: multiplying lengths gives us an area).
newtype_struct! Macro
The newtype_struct! macro creates a newtype that wraps a user-defined struct
(or any non-scalar inner type), implementing
Debug,
Clone,
PartialEq, and
Into<inner_type>.
The inner type must itself implement Debug + Clone + PartialEq. Extra traits
(Copy, Hash, Eq, Ord, PartialOrd, Serialize, …) can be requested by
passing them after the ; separator and will be derived on the generated
struct.
Example:
use newtype_struct;
newtype_struct!;
With extra derives:
use newtype_struct;
use ;
newtype_struct!;
newtype_validated! Macro
The newtype_validated! macro creates a newtype whose constructor enforces an
invariant. Validation always runs; there is no unsafe escape hatch. Invalid
values yield an error.
The error type is generated alongside the newtype as a zero-sized struct
implementing Debug + Clone + Copy + PartialEq + Eq + Display + Error. Its
name must be supplied explicitly because macro_rules! cannot synthesise
{Name}Error on stable Rust without third-party dependencies.
From<inner> is not emitted (it would bypass validation).
TryFrom<inner> is
emitted and delegates to Self::new.
The validator can be either a closure or a free-function path; both must be
callable as Fn(&inner) -> bool.
Example:
use newtype_validated;
newtype_validated!;
assert!;
assert!;
With a free function:
use newtype_validated;
newtype_validated!;
WARNING: Validation runs only at construction time. Wrapping a type with interior mutability (e.g.
RefCell,Cell) defeats the invariant because the inner value can mutate after construction.
WARNING: Combining
newtype_validated!withnewtype_from!(ornewtype_from_only!) on the same newtype emitsFrom<inner>and silently bypasses validation.
newtype_from! Macro
The macro newtype_from! implements the From and FromStr traits for us
in case it's possible.
It has to be used in combination with one of the other ones
(newtype!, newtype_ord!, or
newtype_unit!).
Example:
use ;
newtype!;
newtype_from!;
NOTE: It only works for integers, floating point numbers, and
String.
newtype_from_only! Macro
The macro newtype_from_only! implements only the From trait, leaving
FromStr to the caller. Use it when a type should be constructible from its
inner value but should not parse from text.
Example:
use ;
newtype!;
newtype_from_only!;
NOTE: It works for integers, floating point numbers,
String, and arbitrary inner types.
newtype_fromstr! Macro
The macro newtype_fromstr! implements only the FromStr trait, leaving
From<inner> to the caller. Use it when a type should parse from text but
should not be implicitly constructible from its inner value.
Example:
use ;
use FromStr;
newtype!;
newtype_fromstr!;
let a = from_str.unwrap;
NOTE: It only works for integers, floating point numbers, and
String. For arbitrary inner types implementFromStrmanually.