My recent adventures in macros are about making newtypes more convenient to use. What I realize I really want, after looking at Haskell newtypes, is #[derive]
attributes on newtypes.
In most cases, the ops
and convert
traits have convenient default implementation. For example:
#[derive(From,Into)]
Miles(u32);
Should be identical to:
Miles(u32);
impl Into<u32> for Miles {
fn into(self) -> u32 {
let Miles(v) = self;
v
}
}
impl From<u32> for Miles {
fn from(v: u32) -> Self {
Miles(v)
}
}
The traits that can't be derived currently, but have natural defaults are: From
, Into
, Deref
, DerefMut
. If there's a definition for From
, the arithmetic operators also have natural defaults (assuming the underlying type supports them).
Has there been a (possibly rejected) RFC for this?