| Crates.io | id_newtype_macros |
| lib.rs | id_newtype_macros |
| version | 0.3.0 |
| created_at | 2025-11-07 22:28:09.234755+00 |
| updated_at | 2026-01-09 03:44:55.506493+00 |
| description | Compile time validation macros for the `id_newtype` crate. |
| homepage | https://github.com/azriel91/id_newtype |
| repository | https://github.com/azriel91/id_newtype |
| max_upload_size | |
| id | 1922263 |
| size | 14,018 |
Implements logic for a Cow<'static, str> newtype where only [A-Za-z0-9_]
are valid characters.
Implementations are provided for:
IdType::newIdType::new_unchecked (with #[doc(hidden)])IdType::is_valid_idIdType::into_innerstd::borrow::Borrow<str>std::convert::AsRef<str>std::convert::TryFrom<String>std::convert::TryFrom<&'static str>std::fmt::Displaystd::ops::Derefstd::ops::DerefMutstd::str::FromStrA separate error type is also generated, which indicates an invalid value
when the ID type is instantiated with new.
In Cargo.toml:
id_newtype = "0.3.0" # or
id_newtype = { version = "0.3.0", features = ["macros"] }
In code:
// in lib.rs
#[macro_use]
extern crate id_newtype;
// in your ID module, e.g. `my_id.rs`
use std::borrow::Cow;
// Rename your ID type
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct MyId(Cow<'static, str>);
id_newtype::id_newtype!(
MyId, // Name of the ID type
MyIdInvalidFmt // Name of the invalid value error
);
If you have a procedural macro that checks for ID validity1 at compile time, you may pass in its name as follows:
#[macro_use]
extern crate id_newtype;
use std::borrow::Cow;
// Either use `id_newtype::id`, or replace this with your own proc macro.
use id_newtype::id;
// use my_crate_static_check_macros::my_id;
// Rename your ID type
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct MyId(Cow<'static, str>);
id_newtype::id_newtype!(
MyId, // Name of the ID type
MyIdInvalidFmt, // Name of the invalid value error
my_id // Name of the proc macro
);
1 You can either enable the "macros" feature and have access to
the id! macro, or implement your own proc macro. See
id_newtype_macros for an example.
Finally, if you pass in a lifetime parameter (4th param) to the macro, the generated type can hold borrowed data:
#[macro_use]
extern crate id_newtype;
use std::borrow::Cow;
// Either use `id_newtype::id`, or replace this with your own proc macro.
use id_newtype::id;
// use my_crate_static_check_macros::my_id;
// Rename your ID type
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct MyId<'id>(Cow<'id, str>);
id_newtype::id_newtype!(
MyId, // Name of the ID type
MyIdInvalidFmt, // Name of the invalid value error
my_id // Name of the proc macro
'id // Lifetime parameter
);
A MyId<'id> can be converted into MyId<'static> by using
my_id.into_static() which takes self.
"macros" This feature enables the id! compile-time checked proc macro
for safe construction of IDs at compile time.
# #[cfg(feature = "my_feature")]
# {
#[macro_use]
extern crate id_newtype;
use id_newtype::id;
// Define a new ID type
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct MyId(Cow<'static, str>);
id_newtype::id_newtype!(MyId, MyIdInvalidFmt, id);
// ok!
let id = id!("my_id");
// `id` is not a valid `Id`
// `Id`s must begin with a letter or underscore, and contain only
// letters, numbers, or underscores.
let id = id!("invalid id");
# }
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.