Skip to content
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

Extend #[derive(TransparentWrapper)] #147

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

zachs18
Copy link
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
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
Owner

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

@zachs18
Copy link
Contributor Author

zachs18 commented May 29, 2024

Typo

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants