feat: add newtype macro
This commit is contained in:
parent
04f550d660
commit
64dd15fcb9
2 changed files with 57 additions and 0 deletions
|
@ -1 +1,2 @@
|
|||
//
|
||||
mod newtype;
|
||||
|
|
56
src/newtype.rs
Normal file
56
src/newtype.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
//
|
||||
#[macro_export]
|
||||
macro_rules! newtype {
|
||||
($name:ident, $docs:literal) => {
|
||||
#[doc = $docs]
|
||||
#[derive(
|
||||
Clone,
|
||||
Copy,
|
||||
Default,
|
||||
Debug,
|
||||
derive_more::Display,
|
||||
derive_more::From,
|
||||
PartialEq,
|
||||
Eq,
|
||||
PartialOrd,
|
||||
Ord,
|
||||
Hash,
|
||||
derive_more::AsRef,
|
||||
derive_more::Constructor,
|
||||
serde::Serialize,
|
||||
serde::Deserialize,
|
||||
)]
|
||||
pub struct $name;
|
||||
};
|
||||
($name:ident, $type:ty $(, $derive:ty)*, $docs:literal) => {
|
||||
#[doc = $docs]
|
||||
#[derive(
|
||||
Clone,
|
||||
Debug,
|
||||
derive_more::From,
|
||||
PartialEq,
|
||||
Eq,
|
||||
derive_more::AsRef,
|
||||
derive_more::Deref,
|
||||
serde::Serialize,
|
||||
serde::Deserialize,
|
||||
$($derive),*
|
||||
)]
|
||||
pub struct $name($type);
|
||||
impl $name {
|
||||
pub fn new(value: impl Into<$type>) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
#[allow(clippy::missing_const_for_fn)]
|
||||
#[must_use]
|
||||
pub fn peel(self) -> $type {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
impl From<$name> for $type {
|
||||
fn from(value: $name) -> $type {
|
||||
value.peel()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Add table
Reference in a new issue