Skip to content

Add zerofrom::transparent! macro#7788

Draft
sffc wants to merge 4 commits intounicode-org:mainfrom
sffc:zf-macro-rules
Draft

Add zerofrom::transparent! macro#7788
sffc wants to merge 4 commits intounicode-org:mainfrom
sffc:zf-macro-rules

Conversation

@sffc
Copy link
Copy Markdown
Member

@sffc sffc commented Mar 18, 2026

Latest attempt at #7607

Looking for feedback on the general approach, and then on the concrete implementation.

Changelog

zerofrom: Adds macro to derive ZeroFrom and concrete functions for repr(transparent) conversions

  • New macro: zerofrom::transparent!

@sffc sffc requested a review from Manishearth as a code owner March 18, 2026 15:03
@sffc sffc mentioned this pull request Mar 18, 2026
Copy link
Copy Markdown
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

In favor of the general implementation. I think the way you handle method docs/pub/etc is clever.

This is exactly along the lines as what I was envisioning for transparent casts for a long time, and is significantly better than my imagined level of complexity.

@sffc sffc marked this pull request as draft March 19, 2026 20:41
@sffc
Copy link
Copy Markdown
Member Author

sffc commented Mar 19, 2026

Thanks; if this direction looks good to you then I'll clean up this PR and get it ready for merging.

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

Comment on lines +29 to +30
#[repr(transparent)]
$(#[$meta:meta])*
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_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

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'll try again, but if I match on the argument then every token up to the argument can't be itself a match. macro_rules doesn't do fancy lookahead matching.

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.

Here's a simplified example if you want to see if you can get it to work:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=8931afd392ba47737907ee2fdb37a2fb

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.

Discussion with @robertbastian: if we remove the meta, vis, and function name, then we can match on the argument type. This seems fine.

#[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.

Comment thread utils/zerofrom/src/zf_transparent.rs Outdated
$(
$(#[$meta_box])*
$vis_box fn $fn_box(inner: $crate::internal::Box<$type_box>) -> $crate::internal::Box<Self> {
unsafe { core::mem::transmute(inner) }
Copy link
Copy Markdown
Member Author

@sffc sffc Apr 14, 2026

Choose a reason for hiding this comment

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

Reminder for self: I was lazy and wrote core::mem::transmute here but we should not use transmute on boxes. Box::from_raw is what we should use instead. (#7871)

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.

Same for the Rcs

@sffc sffc added the discuss-priority Discuss at the next ICU4X meeting label May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

discuss-priority Discuss at the next ICU4X meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants