-
Notifications
You must be signed in to change notification settings - Fork 139
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 zerocopy derives #326
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ exclude = ["tests", ".github"] | |
serde = { version = "1.0", optional = true, default-features = false } | ||
core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core" } | ||
compiler_builtins = { version = "0.1.2", optional = true } | ||
zerocopy = { version = "0.6.1", optional = true, default-features = false } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should do something like: zerocopy_0_6 = { version = "0.6.1", optional = true, default-features = false, package = "zerocopy" } here and remove the explicit feature. Otherwise users can depend on |
||
|
||
[dev-dependencies] | ||
trybuild = "1.0" | ||
|
@@ -35,6 +36,7 @@ serde_test = "1.0" | |
std = [] | ||
example_generated = [] | ||
rustc-dep-of-std = ["core", "compiler_builtins"] | ||
zerocopy_0_6 = ["zerocopy"] | ||
|
||
[package.metadata.docs.rs] | ||
features = ["example_generated"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#[cfg(feature = "zerocopy_0_6")] | ||
fn main() { | ||
use bitflags::bitflags; | ||
use zerocopy::{AsBytes, FromBytes}; | ||
|
||
bitflags! { | ||
#[derive(AsBytes, Debug, Eq, FromBytes, PartialEq)] | ||
#[repr(transparent)] | ||
pub struct Flags: u32 { | ||
const A = 1; | ||
const B = 2; | ||
const C = 4; | ||
const D = 8; | ||
} | ||
} | ||
|
||
let flags = Flags::A | Flags::B; | ||
let bytes = flags.as_bytes(); | ||
println!("{:?} -> {:?}", flags, bytes); | ||
|
||
let flags_from_bytes = Flags::read_from(bytes).unwrap(); | ||
println!("{:?} -> {:?}", bytes, flags_from_bytes); | ||
|
||
assert_eq!(flags, flags_from_bytes); | ||
} | ||
|
||
#[cfg(not(feature = "zerocopy_0_6"))] | ||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,12 @@ macro_rules! __declare_internal_bitflags { | |
$iter_vis:vis struct $Iter:ident; | ||
$iter_names_vis:vis struct $IterNames:ident; | ||
) => { | ||
#[cfg(feature = "zerocopy_0_6")] | ||
use $crate::__private::zerocopy; | ||
#[cfg(feature = "zerocopy_0_6")] | ||
use ::core::marker::Sized; | ||
|
||
#[cfg_attr(feature = "zerocopy_0_6", derive($crate::__private::zerocopy::AsBytes, $crate::__private::zerocopy::FromBytes))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately this won't actually work. This code is expanded in an end-user's library where there's no knowledge of whether or not Since this library requires using their custom derive I think we'll need a different strategy that accepts the struct definition as input and annotates it with its attributes. We'll need a second macro that also includes those necessary If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey, came across this while discussing google/zerocopy#287. Zerocopy 0.7 has been released now. Is the experience you're referring to the need to emit an impl for the function |
||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
#[repr(transparent)] | ||
$vis struct $InternalBitFlags { | ||
|
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.
After rebasing on
main
this shouldn't be necessary anymore. We automatically test all of these features.