Existing crate to derive owned and reference newtype

Quite commonly I have to create two newtypes: one owned, one with a reference. Example struct Email(String) and struct EmailRef(&self). Seems like a common pattern where different ownership type needs a different newtype.

Is there any existing crate to do that? My search-fu is failing me.

In that case the usual idiom is "not to hide the & / &mut nature of the type", at least when dealing with Sized types such as String: to "just" newtype the owned variant, and use:

as a helper crate to derive &String → &Email and &mut String → &mut Email casts :slightly_smiling_face:

I was hopping for something that can handle Vec<u8> vs &[u8] and such without any unsafe transmutation magic, and purely compacting the boilerplate.

You are not

case, then :sweat_smile:


I recently stumbled upon:

https://lib.rs/impl_twice

seems relevant.

Does Cow<[u8]> not fit your needs?

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.