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 zerocopy derives #326

Closed
wants to merge 2 commits into from
Closed

Support zerocopy derives #326

wants to merge 2 commits into from

Conversation

qwandor
Copy link

@qwandor qwandor commented Mar 29, 2023

Fixes #325.

@KodrAus
Copy link
Member

KodrAus commented Apr 3, 2023

Thanks for the PR @qwandor!

Before we do this, I think we need to decide how we want to support unstable dependencies in this library. I've opened #329 to do that. Any feedback would be much appreciated!

#[cfg(feature = "zerocopy_0_6")]
use ::core::marker::Sized;

#[cfg_attr(feature = "zerocopy_0_6", derive($crate::__private::zerocopy::AsBytes, $crate::__private::zerocopy::FromBytes))]
Copy link
Member

Choose a reason for hiding this comment

The 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 bitflags has this feature enabled. For other libraries, we instead expand to a macro that calls back into bitflags and generates appropriate impl blocks.

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 use statements.

If zerocopy 0.7 that's currently in-progress has a nicer experience for implementing its traits then I think it would be good to wait for it.

Copy link

Choose a reason for hiding this comment

The 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 only_derive_is_allowed_to_implement_this_trait or something else? We've recently done some work to make the docs more precise with regards to what needs to hold in order for a trait impl to be sound, so that might help you. Let me know if there's anything else we could add that would make this easier for you!

@@ -49,7 +49,7 @@ jobs:
run: cargo install cargo-hack

- name: Powerset
run: cargo hack test --feature-powerset --lib --optional-deps "std serde" --depth 3 --skip rustc-dep-of-std
run: cargo hack test --feature-powerset --lib --optional-deps "std serde zerocopy_0_6" --depth 3 --skip rustc-dep-of-std
Copy link
Member

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.

@@ -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 }
Copy link
Member

Choose a reason for hiding this comment

The 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 zerocopy directly, which will become confusing when a new version is released.

@KodrAus
Copy link
Member

KodrAus commented Apr 29, 2023

@qwandor It looks like this is going to be a bit trickier than I first thought. I'm thinking of adding a bitflags_attributes crate that can conditionally expand to derives for libraries like zerocopy that require them based on bitflags features.

If you're happy, I'll work on that and roll zerocopy support in with it.

@KodrAus
Copy link
Member

KodrAus commented May 17, 2023

With #348 merged I've added an example of how to integrate zerocopy with a generated flags type here. We don't need to add direct support for the library in bitflags for you to derive it anymore.

@KodrAus KodrAus closed this May 17, 2023
@KodrAus KodrAus mentioned this pull request May 17, 2023
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.

Support zerocopy
3 participants