Newtype
traitSometimes you want to wrap a type in a newtype, but you want the newtype implements the same traits as the wrapped type. The Newtype
trait helps you to implement the traits of the wrapped type for the newtype automatically.
When you define a trait, you can support the Newtype
trait and any type that implements the Newtype
trait will automatically implement your trait.
```rust use thenewtype::Newtype; use derivemore::AsRef;
pub trait MyTrait { fn my_method(&self) -> String; }
impl
// Now we can use the MyTrait
trait for the newtype.
struct Foo;
impl MyTrait for Foo { fn mymethod(&self) -> String { "foo".tostring() } }
struct Bar(Foo);
impl Newtype for Bar { type Inner = Foo; }
fn main() { let bar = Bar(Foo); asserteq!(bar.mymethod(), "foo"); } ```
You can use the Newtype
trait when you want to wrap a type in a newtype and you want the newtype implements ALL the newtype-supported traits of the wrapped type. If you need some traits, you should implement them manually and avoid using the Newtype
trait.
The Newtype
trait is not a good solution for the following cases:
Fancy
trait will implement the Awesome
trait). only one general implementation for each trait is possible. You can't use the Newtype
trait in this case.```rust use the_newtype::Newtype;
trait Fancy { fn fancy_method(&self) -> String; }
// it's ok to implement the Fancy
trait for the Newtype
trait
impl
trait Awesome { fn awesome_method(&self) -> String; }
// every type that implements the Fancy
trait will implement the Awesome
trait
// it's not possible to implement the Awesome
trait for the Newtype
trait
impl
```
derive_more
and Newtype
macrosYou can use the derive_more
crate to implement the AsRef
, AsMut
, and Into
traits for the newtype. And you can use the Newtype
macro to implement the Newtype
trait for the newtype.
```rust use thenewtype::Newtype; use derivemore::AsRef;
struct Bar(String); ```
&self
If you want to implement a trait for &self
, you can use the AsRef
trait.
```rust use the_newtype::Newtype;
trait MyTrait { fn my_method(&self) -> String; }
impl
&mut self
If you want to implement a trait for &mut self
, you can use the AsMut
trait.
```rust use the_newtype::Newtype;
trait MyTrait { fn my_method(&mut self) -> String; }
impl
self
If you want to implement a trait for self
, you can use the Into
trait.
```rust use the_newtype::Newtype;
trait MyTrait { fn my_method(self) -> String; }
impl
self
If you want to implement a trait without self
, no extra trait is needed.
```rust use the_newtype::Newtype;
trait MyTrait { fn my_method() -> String; }
impl
self
, &self
or &mut self
If you want to implement a trait for self
, &self
or &mut self
, you can use the Into
, AsRef
or AsMut
traits together.
```rust use the_newtype::Newtype;
trait MyTrait { fn mymethod(&self) -> String; fn mymethod_mut(&mut self) -> String; }
impl
fn my_method_mut(&mut self) -> String {
self.as_mut().my_method_mut()
}
} ```
toml
[dependencies]
the-newtype = "0.1"