Crates.io | id_newtype |
lib.rs | id_newtype |
version | 0.1.0 |
created_at | 2025-02-01 02:35:56.548701+00 |
updated_at | 2025-02-01 02:35:56.548701+00 |
description | Implements logic for a `Cow<'static, str>` newtype where only `[A-Za-z0-9_]` are valid characters. |
homepage | https://github.com/azriel91/id_newtype |
repository | https://github.com/azriel91/id_newtype |
max_upload_size | |
id | 1538025 |
size | 36,937 |
Implements logic for a Cow<'static, str>
newtype where only [A-Za-z0-9_]
are valid characters.
Implementations are provided for:
IdType::new
IdType::new_unchecked
(with #[doc(hidden)]
)IdType::is_valid_id
IdType::into_inner
std::borrow::Borrow<str>
std::convert::AsRef<str>
std::convert::TryFrom<String>
std::convert::TryFrom<&'static str>
std::fmt::Display
std::ops::Deref
std::ops::DerefMut
std::str::FromStr
A separate error type is also generated, which indicates an invalid value when the ID type is instantiated with new
.
use std::borrow::Cow;
// Rename your ID type
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct MyIdType(Cow<'static, str>);
crate::id_newtype!(
MyIdType, // Name of the ID type
MyIdTypeInvalidFmt // 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:
use std::borrow::Cow;
// replace this with your ID type's macro
use my_crate_static_check_macros::my_id_type;
// Rename your ID type
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct MyIdType(Cow<'static, str>);
crate::id_newtype!(
MyIdType, // Name of the ID type
MyIdTypeInvalidFmt, // Name of the invalid value error
my_id_type // Name of the static check macro
);
1 This crate was extracted from peace
, so the my_crate_static_check_macros
is not generated for you. You must implement it yourself. See static_check_macros
for an example.
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.