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

Support deriving AsBytes on types with type parameters #10

Open
joshlf opened this issue Sep 19, 2022 · 9 comments
Open

Support deriving AsBytes on types with type parameters #10

joshlf opened this issue Sep 19, 2022 · 9 comments
Labels
compatibility-nonbreaking Changes that are (likely to be) non-breaking

Comments

@joshlf
Copy link
Member

joshlf commented Sep 19, 2022

Migrated from https://fxbug.dev/100235

In order to accomplish this, we need to be able to emit an impl which is bounded by a const expr, which requires the unstable feature generic_const_exprs. This issue is blocked until that feature is stabilized.

@bjorn3
Copy link

bjorn3 commented Nov 7, 2022

Would it be possible to have a special case if the type parameter has a bound on ByteOrder and only on ByteOrder by checking all implementations of ByteOrder? ByteOrder is a sealed trait, so all implementations of it are known. This would make it much easier to write code that handles file formats that can be both little and big endian.

@joshlf
Copy link
Member Author

joshlf commented Nov 10, 2022

I'm not sure what the relevance of ByteOrder is here? The byteorder-aware types in the byteorder module implement AsBytes, but a type can satisfy the rules of AsBytes even if its type parameters do not implement ByteOrder. What's the connection you're getting at?

@bjorn3
Copy link

bjorn3 commented Nov 10, 2022

I did like to have types like

#[derive(FromBytes)]
struct Foo<B: ByteOrder> {
    a: U32<B>,
    b: U32<B>,
}

to work with both little endian and big endian using the same type definition. However the restriction of not having any type parameters when deriving FromBytes prevents this. I thought it did be easier to allow FromBytes just for type parameters bound by ByteOrder than implementing the general case.

@joshlf
Copy link
Member Author

joshlf commented Nov 10, 2022

That code should work fine. It's just #[derive(AsBytes)] that doesn't currently support generics (actually, on ToT, it supports generics on a struct so long as that struct is also #[repr(transparent)] or #[repr(packed)]).

@bjorn3
Copy link

bjorn3 commented Nov 10, 2022

(actually, on ToT, it supports generics on a struct so long as that struct is also #[repr(transparent)] or #[repr(packed)]).

I see. Adding #[repr(packed)] would work for my case.

@InternetOfTofu
Copy link

I wonder if single field #[repr(C)] generic structs can be treated the same way as #[repr(transparent)] for this purpose?

@joshlf
Copy link
Member Author

joshlf commented Aug 1, 2023

They could, but I'm not sure what the point would be. If you're considering adding #[derive(AsBytes)] to such a type, then:

  • You own the code, and so you can change it
  • You could replace #[repr(C)] with #[repr(transparent)] and not change the behavior
  • You could then use #[derive(AsBytes)] thanks to #[repr(transparent)]

@joshlf joshlf added the compatibility-nonbreaking Changes that are (likely to be) non-breaking label Aug 12, 2023
@joshlf joshlf mentioned this issue Aug 20, 2023
@gio256
Copy link

gio256 commented Jun 27, 2024

@joshlf I'm interested in working on this. Would you be interested in seeing an exploration of any of the following?

@joshlf
Copy link
Member Author

joshlf commented Jul 9, 2024

Thanks for your interest! The type-level representation of size and alignment is what we'd consider. I'll warn you that the design space there is large and there are lots of thorns, and since any traits introduced will be part of our public API, we'll be very nitpicky about the exact design. That said, it would be great to have some experimentation with this at a minimum, and if you're willing to put up with our nitpickiness, then a full implementation would obviously be very welcome!

For inspiration, see: #1316 (comment) (although I haven't looked closely at those examples, and I'm not sure whether they address all of our needs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatibility-nonbreaking Changes that are (likely to be) non-breaking
Projects
None yet
Development

No branches or pull requests

4 participants