-
-
Notifications
You must be signed in to change notification settings - Fork 80
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
the FooBits
type generated by #[derive(CheckedBitPattern)]
should implement NoUninit
if Self
is NoUninit
#215
Comments
@fu5ha this seems like something for you since you're the proc-macro guardian |
👍 agree with the analysis, feel free to PR and I will review or ping me in a couple weeks and I can hopefully implement it |
I tried to take a look at the code. I find it more complicated then expected. the main issue is, #[derive(Foo, Bar)]
struct MyStruct {} the procedural macro of in this case,
there's also another "non-solution": in document, ask the user to write multiple #[derive(Foo)]
#[derive(Bar)]
struct MyStruct {} the input token stream of but this is a "non-solution", not only because it is delicate and feels "hacky", but most importantly, at this point, I feel like it's probably just not worth it to add this feature. anyway, I'm not an expert on procedural macros, maybe I'm missing something obvious. @fu5ha any opinions or suggestions? thanks. |
summary
currently, the
#[derive(CheckedBitPattern)]
on a structFoo
expands to code like this:however, the generated
FooBits
type cannot be used as a buffer which will be later read into, becausebytes_of_mut
requires the type to beNoUninit + AnyBitPattern
, whileFooBits
doesn't implementNoUninit
, even ifFoo
isNoUninit
.I suggest the derive macro for
CheckedBitPattern
to add an implmentation ofNoUninit
for the generated "Bits" type, something like:context
I was using
CheckedBitPattern
as a validator for very simple binary file format. I could write something like this:but I would like to use the
HeaderBits
as buffer, like this:but the code won't compile, unless I manually add:
my
Header
type looks like this:btw, it would also be convenient if the
CheckedBitPattern
trait could have a conversion function, e.g.try_from_bits()
, something like:or, alternatively, we can add an
try_into_checked()
method for the generated "Bits" type.The text was updated successfully, but these errors were encountered: