-
-
Notifications
You must be signed in to change notification settings - Fork 79
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
add basic derive macro for Pod, Zeroable and TransparentWrapper for structs #30
Conversation
Now that @danielhenrymantilla do I recall correctly that you've come up with a way to derive these via macros-by-example only? |
I'm very interested in the idea of this PR, but I won't have the time to look until later today, and I'm also a little nervous about putting proc-macros near unsafe code since I don't really use them much, so I'm going to ask around for extra eyes to look at this to make sure it's all good. |
added a macro for deriving it checks that the struct is If the struct only contains a single field it will automatically use the type of that field for the train bound, if there is more than one field (extra zero-sized fields) you'll need to specify the type using For example: #[derive(TransparentWrapper)]
#[repr(transparent)]
struct TransparentSingle {
a: u16,
}
#[derive(TransparentWrapper)]
#[repr(transparent)]
#[transparent(u16)]
struct TransparentWithZeroSized {
a: u16,
b: (),
} note that this macro is more strict that the requirements of the trait, since the trait allows for "nested" transparent wrappers: #[repr(transparent)]
struct A(u16);
#[repr(transparent)]
struct B(A);
unsafe impl TransparentWrapper<u16> for B{}; the macro currently rejects this since I can't think of a good way to verify the constraints while allowing this |
it looks like some of the output code is calling std::mem::size_of ? That at least can be core::mem::size_of. |
https://crates.io/crates/zerocopy-derive solves largely the same problem, perhaps some techniques from it can be adopted here. |
Fixed |
running cargo expand --test basic from the derive folder is probably the easiest way to check what checks are being generated by the macro |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me, I really want this! 😅
51c829d
to
9ef53c7
Compare
Co-authored-by: Lucien Greathouse <me@lpghatguy.com>
…tructs (Lokathor#30) * add basic derive macro for Pod and Zeroable for structs * add derive macro for TransparentWrapper * use core::mem::size_of instead of std::mem::size_of in generated code * cleanup error handling a bit * remove unneeded iter logic * remove unneeded clone and order impl * fix generics * fix doc typo Co-authored-by: Lucien Greathouse <me@lpghatguy.com> * remove unneeded lifetime anotation * use unreachable for already rejected patch Co-authored-by: Lucien Greathouse <me@lpghatguy.com>
Adds derive macros for
Zeroable
andPod
for structs.The macro is exported behind the
derive
feature flag (same way as serde does)Zeroable
macro checks that all fields areZeroable
.Pod
macro checks that all fields arePod
, there is no padding bytes and that the struct is#[repr(C)]
or#[repr(packed)]
This currently only supports deriving for structs, deriving
Zeroable
would be possible in the future.Fixes #4