Extend #[derive(TransparentWrapper)]#147
Conversation
|
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,
} |
|
|
|
Typo #[derive(TransparentWrapper)]
#[transparent([T; 2])]
#[repr(transparent)]
struct ArrayPair<T> {
wrapped: [T; 2],
etc: ZST,
} |
| // If there were more tokens, the input was invalid | ||
| Some(_) => None, | ||
| None => res, | ||
| } |
There was a problem hiding this comment.
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?
| 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"); |
There was a problem hiding this comment.
We could suggest the type alias workaround here as well.
… idents in #[transparent(Type)].
e954e03 to
3c5ca31
Compare
|
released in the 1.10.2 version of the derive crate. |
(Based on PR 146, but not particularly related)
Allow types more complicated than single idents in
#[transparent(Type)]when derivingTransparentWrapper.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.