| Crates.io | newer-type |
| lib.rs | newer-type |
| version | 0.2.0 |
| created_at | 2025-03-27 01:21:42.011509+00 |
| updated_at | 2025-07-21 05:00:38.382926+00 |
| description | Support defining newtype wrapper with inheriting trait implementations |
| homepage | https://github.com/yasuo-ozu/newer-type |
| repository | https://github.com/yasuo-ozu/newer-type |
| max_upload_size | |
| id | 1607410 |
| size | 40,711 |
Ergonomic support for the newtype pattern in Rust, with automatic trait implementations.
The newtype pattern in Rust is useful for creating distinct types without runtime overhead. However, it often requires boilerplate code to re-implement traits of the inner type.
The newer-type crate provides a procedural macro #[implement(...)] to reduce that boilerplate by automatically implementing traits for your wrapper types.
The #[implement(...)] macro currently supports automatic implementations for:
#[target]std, see newer_type_std crate documentationnewer-type (manual newtype definition)trait SayHello {
fn say_hello(&self) -> String;
}
impl SayHello for String {
fn say_hello(&self) -> String {
format!("Hello, {}!", self)
}
}
pub struct MyName(String);
impl SayHello for MyName {
fn say_hello(&self) -> String {
self.0.say_hello()
}
}
newer-type crate# use newer_type::{implement, target};
#[target]
trait SayHello {
fn say_hello(&self) -> String;
}
impl SayHello for String {
fn say_hello(&self) -> String {
format!("Hello, {}!", self)
}
}
#[implement(SayHello)]
pub struct MyName(String);
That's it! The selected traits are automatically implemented for you.
newer_type::traitsIn order to implement traits defined in Rust's standard library for your newtype, there are empty definitions
in newer_type_std crate. You can pick up
traits to be implemented from it.
# use newer_type::implement;
use newer_type_std::{iter::IntoIterator, iter::Extend, cmp::PartialEq, cmp::Eq};
#[implement(IntoIterator, Extend<T>, PartialEq, Eq)]
struct MyVec<T>(Vec<T>);
// now `MyVec` implements std::iter::IntoIterator, std::ops::Extend, std::cmp::{PartialEq, Eq}
This crate is particularly useful when:
Add this to your Cargo.toml:
[dependencies]
newer-type = "0.1"
MIT