Has there been a (rejected?) RFC on derive attributes for newtypes?


#1

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?


#2

#3

(Note that you already automatically get an Into when you implement From, thanks to https://github.com/rust-lang/rust/blob/9cc0b2247509d61d6a246a5c5ad67f84b9a2d8b6/src/libcore/convert.rs#L148-L154 )


#4

I thought you get an Into the other way? That is, if I implement From u32 to Bar, I get Into for Bar into u32?

That is, the invariant is that these calls are equivalent:

let b1 = Bar::from(42);
let b2:Bar = 42.into();

Not:

let b = Bar::from(42);
let i : u32 = b.into();

Or do I misunderstand (which is very very likely).


#5

Yes, sorry, I got my parameters confused.