-
Notifications
You must be signed in to change notification settings - Fork 263
Add zerofrom::transparent! macro #7788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // This file is part of ICU4X. For terms of use, please see the file | ||
| // called LICENSE at the top level of the ICU4X source tree | ||
| // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
|
||
| /// Implements [`ZeroFrom`](crate::ZeroFrom) on a transparent type | ||
| /// from a reference to the inner type. | ||
| /// | ||
| /// Also supports creating concrete functions. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use crate::zerofrom::ZeroFrom; | ||
| /// | ||
| /// zerofrom::transparent!( | ||
| /// #[repr(transparent)] | ||
| /// pub struct StrWrap(str); | ||
| /// impl ZeroFrom<&str> for &StrWrap; | ||
| /// ); | ||
| /// | ||
| /// let s = "hello"; | ||
| /// let wrap = <&StrWrap>::zero_from(s); | ||
| /// | ||
| /// assert_eq!(&wrap.0, "hello"); | ||
| /// ``` | ||
| #[macro_export] | ||
| macro_rules! transparent { | ||
| ( | ||
| #[repr(transparent)] | ||
| $(#[$meta:meta])* | ||
|
Comment on lines
+29
to
+30
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is sensitive to attribute ordering, which it shouldn't be
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A different approach is to have the macro generate the repr but that's less obvious to the reader.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right. As currently written the macro does not add anything to the struct definition. It just echoes it back out. The only thing is does is add impls which it claims are safe due to the shape of the struct. |
||
| $vis:vis struct $name:ident($type:ty); | ||
| $( | ||
| impl ZeroFrom<&$type_zf:ty> for &$name_zf:ident; | ||
| )? | ||
| $(impl { | ||
| $( | ||
| @ref | ||
| $(#[$meta_ref:meta])* | ||
| $vis_ref:vis fn $fn_ref:ident(&$type_ref:ty) -> &Self; | ||
| )? | ||
| $( | ||
| @slice | ||
| $(#[$meta_slice:meta])* | ||
| $vis_slice:vis fn $fn_slice:ident(&[$type_slice:ty]) -> &[Self]; | ||
| )? | ||
| $( | ||
| @box | ||
| $(#[$meta_box:meta])* | ||
| $vis_box:vis fn $fn_box:ident(Box<$type_box:ty>) -> Box<Self>; | ||
| )? | ||
| $( | ||
| @rc | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's try to keep custom syntax to a minimum. it should suffice to match on the argument |
||
| $(#[$meta_rc:meta])* | ||
| $vis_rc:vis fn $fn_rc:ident(Rc<$type_rc:ty>) -> Rc<Self>; | ||
| )? | ||
| })? | ||
| ) => { | ||
| #[repr(transparent)] | ||
| $(#[$meta])* | ||
| $vis struct $name($type); | ||
| $( | ||
| impl<'zf> $crate::ZeroFrom<'zf, $type_zf> for &'zf $name { | ||
| fn zero_from(inner: &'zf $type) -> Self { | ||
| unsafe { core::mem::transmute(inner) } | ||
| } | ||
| } | ||
| )? | ||
| $(impl $name { | ||
| $( | ||
| $(#[$meta_ref])* | ||
| $vis_ref fn $fn_ref(inner: &$type_ref) -> &Self { | ||
| unsafe { core::mem::transmute(inner) } | ||
| } | ||
| )? | ||
| $( | ||
| $(#[$meta_slice])* | ||
| $vis_slice fn $fn_slice(inner: &[$type_slice]) -> &[Self] { | ||
| unsafe { core::mem::transmute(inner) } | ||
| } | ||
| )? | ||
| $( | ||
| $(#[$meta_box])* | ||
| $vis_box fn $fn_box(inner: $crate::internal::Box<$type_box>) -> $crate::internal::Box<Self> { | ||
| unsafe { core::mem::transmute(inner) } | ||
| } | ||
| )? | ||
| $( | ||
| $(#[$meta_rc])* | ||
| $vis_rc fn $fn_rc(inner: $crate::internal::Rc<$type_rc>) -> $crate::internal::Rc<Self> { | ||
| unsafe { core::mem::transmute(inner) } | ||
| } | ||
| )? | ||
| })? | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // This file is part of ICU4X. For terms of use, please see the file | ||
| // called LICENSE at the top level of the ICU4X source tree | ||
| // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
|
||
| use zerofrom::transparent; | ||
|
|
||
| transparent!( | ||
| #[repr(transparent)] | ||
| /// hello world | ||
| #[derive(Debug)] | ||
| pub(crate) struct Foo([u8; 3]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like type declarations living inside macro invocations. From a call site it's not known that this code is passed through without modifications, the macro could do anything to it (change the repr, add fields, wrap fields, etc).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is why I've overall been very lukewarm on #7607 so far. But I think this is the most maintainable and low-impact version. We can document what the macro does, and it's pretty easy to verify it, which is why I think this is still a good idea overall.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't love this approach, either. It was not my first or second or third choice at solving the problem. But @Manishearth identified flaws in all the other approaches. My conclusion is that this is the least bad, and better than not solving the problem. |
||
| impl ZeroFrom<&[u8; 3]> for &Foo; | ||
| impl { | ||
| @ref | ||
| /// Cast a transparent ref! | ||
| #[inline] | ||
| fn from_transparent_ref(&[u8; 3]) -> &Self; | ||
| @slice | ||
| /// Cast a transparent slice! | ||
| pub fn from_transparent_slice(&[[u8; 3]]) -> &[Self]; | ||
| @box | ||
| /// Cast a transparent box! | ||
| #[cfg(feature = "alloc")] | ||
| fn from_transparent_box(Box<[u8; 3]>) -> Box<Self>; | ||
| } | ||
| ); | ||
|
|
||
| #[test] | ||
| fn test() {} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: