Skip to content

Extend #[derive(TransparentWrapper)]#147

Merged
Lokathor merged 3 commits intoLokathor:mainfrom
zachs18:derive-transparentwrapper-extend
Oct 3, 2025
Merged

Extend #[derive(TransparentWrapper)]#147
Lokathor merged 3 commits intoLokathor:mainfrom
zachs18:derive-transparentwrapper-extend

Conversation

@zachs18
Copy link
Copy Markdown
Contributor

@zachs18 zachs18 commented Nov 16, 2022

(Based on PR 146, but not particularly related)
Allow types more complicated than single idents in #[transparent(Type)] when deriving TransparentWrapper.

  • It still does string comparison, because resolving type equality is not possible in a proc-macro.
  • This is valid as long as there is no way to make the same token stream/source string refer to different types in the struct definition and the derived impl, which I believe is the case.
    • Maybe some unnamed const or function could be emitted to ensure the two types are actually the same at compile-time? like const _: () = { fn _detail<Generics>(x: (Type1, Type2), _generic_use: PhantomData<Struct<Generics>>) -> (Type2, Type1) { x } };

TODO: think about and verify that variance cannot break things.

@zachs18
Copy link
Copy Markdown
Contributor Author

zachs18 commented May 28, 2024

Since users can in most cases just do like

type Wrapped = Complex<Type>;
#[derive(TransparentWrapper)]
#[transparent(Wrapped)]
#[repr(transparent)]
struct MyType {
  wrapped: Wrapped,
  etc: ZST,
}

I'm not sure if this is worth the trouble. Except I guess complex types involving generics are not representable without this, e.g.

#[derive(TransparentWrapper)]
#[transparent(Wrapped)]
#[repr([T; 2])]
struct ArrayPair<T> {
  wrapped: [T; 2],
  etc: ZST,
}

@Lokathor
Copy link
Copy Markdown
Owner

#[repr([T; 2])]?

@zachs18
Copy link
Copy Markdown
Contributor Author

zachs18 commented May 29, 2024

Typo

#[derive(TransparentWrapper)]
#[transparent([T; 2])]
#[repr(transparent)]
struct ArrayPair<T> {
  wrapped: [T; 2],
  etc: ZST,
}

Comment thread derive/src/traits.rs
// If there were more tokens, the input was invalid
Some(_) => None,
None => res,
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

weird formatting

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Formatting fixed.

For the semantics of this section though, we might want to allow a trailing comma here. As-is, this rejects #[transparent(T, )], which might be useful if T is long and would naturally spill onto another line.

Also, if the parsing of the transparent helper fails, the code falls back to the "find the one field" strategy, e.g. this succeeds:

#[derive(bytemuck::TransparentWrapper)]
#[repr(transparent)]
#[transparent(not a type)]
struct Foo {
 one_field: u32,
};

which is not ideal. Maybe get_type_from_simple_attr should bail if it finds the attribute but fails to parse it?

Comment thread derive/src/traits.rs Outdated
Comment thread derive/src/traits.rs Outdated
Comment thread derive/src/traits.rs Outdated
if explicit {
bail!("TransparentWrapper must have one field of the wrapped type. \
The type given in `#[transparent(Type)]` must match tokenwise\
with the type in the struct definition, not just be the same type");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We could suggest the type alias workaround here as well.

@zachs18 zachs18 force-pushed the derive-transparentwrapper-extend branch from e954e03 to 3c5ca31 Compare June 19, 2025 16:28
@zachs18 zachs18 marked this pull request as ready for review June 19, 2025 16:36
@Lokathor Lokathor merged commit d31d92c into Lokathor:main Oct 3, 2025
14 checks passed
@Lokathor Lokathor added semver-patch semver patch change semver-derive We need to update the main crate's use of the derive crate not yet released labels Oct 3, 2025
@Lokathor
Copy link
Copy Markdown
Owner

Lokathor commented Oct 3, 2025

released in the 1.10.2 version of the derive crate.

@zachs18 zachs18 deleted the derive-transparentwrapper-extend branch November 10, 2025 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver-derive We need to update the main crate's use of the derive crate semver-patch semver patch change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants