Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions utils/zerofrom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ extern crate alloc;

mod macro_impls;
mod zero_from;
mod zf_transparent;

#[cfg(feature = "derive")]
pub use zerofrom_derive::ZeroFrom;

pub use crate::zero_from::ZeroFrom;

#[cfg(feature = "alloc")]
#[doc(hidden)] // for macros
pub mod internal {
pub use alloc::boxed::Box;
pub use alloc::rc::Rc;
}
95 changes: 95 additions & 0 deletions utils/zerofrom/src/zf_transparent.rs
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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
/// Also supports creating concrete functions.
/// Also supports adding concrete conversion functions between various reference types.

///
/// # 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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is sensitive to attribute ordering, which it shouldn't be

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$(#[$meta:meta])* is greedy and there isn't a clean way to make it non-greedy so I need to put the #[repr(transparent)] at the top.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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) }
}
)?
})?
};
}
29 changes: 29 additions & 0 deletions utils/zerofrom/tests/test_transparent.rs
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]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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() {}
Loading