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

use extern crate as zerocopy to support intenral derive use #604

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@
__INTERNAL_USE_ONLY_NIGHLTY_FEATURES_IN_TESTS,
feature(layout_for_ptr, strict_provenance)
)]

// This is a hack to allow zerocopy-derive derives to work in this crate. They
// assume that zerocopy is linked as an extern crate, so they access items from
// it as `zerocopy::Xxx`. This makes that still work.
#[cfg(any(feature = "derive", test))]
extern crate self as zerocopy;

#[macro_use]
mod macros;

Expand Down Expand Up @@ -290,14 +297,6 @@ use {
#[allow(unused_imports)]
use crate::util::polyfills::NonNullExt as _;

// This is a hack to allow zerocopy-derive derives to work in this crate. They
// assume that zerocopy is linked as an extern crate, so they access items from
// it as `zerocopy::Xxx`. This makes that still work.
#[cfg(any(feature = "derive", test))]
mod zerocopy {
pub(crate) use crate::*;
}

#[rustversion::nightly]
#[cfg(all(test, not(__INTERNAL_USE_ONLY_NIGHLTY_FEATURES_IN_TESTS)))]
const _: () = {
Expand Down
10 changes: 5 additions & 5 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ fn impl_block<D: DataExt>(
let field_types = data.field_types();

let field_type_bounds = require_trait_bound_on_field_types
.then(|| field_types.iter().map(|ty| parse_quote!(#ty: zerocopy::#trait_ident)))
.then(|| field_types.iter().map(|ty| parse_quote!(#ty: ::zerocopy::#trait_ident)))
.into_iter()
.flatten()
.collect::<Vec<_>>();
Expand All @@ -560,8 +560,8 @@ fn impl_block<D: DataExt>(
let fields = field_types.iter();
let validator_macro = check.validator_macro_ident();
parse_quote!(
zerocopy::macro_util::HasPadding<#type_ident, {zerocopy::#validator_macro!(#type_ident, #(#fields),*)}>:
zerocopy::macro_util::ShouldBe<false>
::zerocopy::macro_util::HasPadding<#type_ident, {::zerocopy::#validator_macro!(#type_ident, #(#fields),*)}>:
::zerocopy::macro_util::ShouldBe<false>
)
});

Expand All @@ -579,7 +579,7 @@ fn impl_block<D: DataExt>(
// We currently only support deriving for sized types; this code will
// fail to compile for unsized types.
Some(quote!(
const LAYOUT: zerocopy::DstLayout = zerocopy::DstLayout::for_type::<Self>();
const LAYOUT: ::zerocopy::DstLayout = ::zerocopy::DstLayout::for_type::<Self>();

// SAFETY: `.cast` preserves address and provenance.
//
Expand Down Expand Up @@ -620,7 +620,7 @@ fn impl_block<D: DataExt>(
// TODO(#553): Add a test that generates a warning when
// `#[allow(deprecated)]` isn't present.
#[allow(deprecated)]
unsafe impl < #(#params),* > zerocopy::#trait_ident for #type_ident < #(#param_idents),* >
unsafe impl < #(#params),* > ::zerocopy::#trait_ident for #type_ident < #(#param_idents),* >
where
#(#bounds,)*
{
Expand Down