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

Adds re-exporting macros and crate renaming support; #57

Closed
wants to merge 5 commits into from
Closed
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
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ edition = "2018"
license = "Zlib OR Apache-2.0 OR MIT"
exclude = ["/pedantic.bat"]

[workspace]
members = ["derive"]

[features]
# Note: Yeah these names are non-standard, we'll fix it in v2 some day maybe
extern_crate_alloc = []
extern_crate_std = ["extern_crate_alloc"]
zeroable_maybe_uninit = []
derive = ["bytemuck_derive"]
proc-macro-crate = ["bytemuck_derive/proc-macro-crate"]

[dependencies]
# use the upper line for testing against bytemuck_derive changes, if any
#bytemuck_derive = { version = "1.0.1-alpha.0", path = "derive", optional = true }
bytemuck_derive = { version = "1", optional = true }
bytemuck_derive = { version = "1.0.2-alpha.0", path = "derive", optional = true }
# bytemuck_derive = { version = "1", optional = true }

[package.metadata.docs.rs]
all-features = true
Expand Down
1 change: 1 addition & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ proc-macro = true
syn = "1"
quote = "1"
proc-macro2 = "1"
proc-macro-crate = { version = "1", optional = true }

[dev-dependencies]
bytemuck = "1.2"
22 changes: 22 additions & 0 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,25 @@ fn derive_marker_trait_inner<Trait: Derivable>(
}
})
}

#[test]
fn test_derive_output() {
let input = quote!(
#[repr(C)]
#[derive(Copy, Clone, Contiguous)]
struct Foo {
bar: u32,
}
);
let input = syn::parse_str::<DeriveInput>(&input.to_string()[..])
.expect("syntax error");
let result = derive_marker_trait::<Pod>(input);

#[cfg(not(feature = "proc-macro-crate"))]
assert!(dbg!(result.to_string())
.contains("unsafe impl bytemuck :: Pod for Foo { }"));

#[cfg(feature = "proc-macro-crate")]
assert!(dbg!(result.to_string())
.contains("unsafe impl :: bytemuck :: Pod for Foo { }"));
}
40 changes: 37 additions & 3 deletions derive/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub struct Pod;

impl Derivable for Pod {
fn ident() -> TokenStream {
quote!(::bytemuck::Pod)
let bytemuck_name = get_bytemuck_crate_name();
quote!(#bytemuck_name::Pod)
}

fn struct_asserts(input: &DeriveInput) -> Result<TokenStream, &'static str> {
Expand Down Expand Up @@ -60,7 +61,8 @@ pub struct Zeroable;

impl Derivable for Zeroable {
fn ident() -> TokenStream {
quote!(::bytemuck::Zeroable)
let bytemuck_name = get_bytemuck_crate_name();
quote!(#bytemuck_name::Zeroable)
}

fn struct_asserts(input: &DeriveInput) -> Result<TokenStream, &'static str> {
Expand Down Expand Up @@ -90,7 +92,8 @@ impl TransparentWrapper {

impl Derivable for TransparentWrapper {
fn ident() -> TokenStream {
quote!(::bytemuck::TransparentWrapper)
let bytemuck_name = get_bytemuck_crate_name();
quote!(#bytemuck_name::TransparentWrapper)
}

fn generic_params(input: &DeriveInput) -> Result<TokenStream, &'static str> {
Expand Down Expand Up @@ -326,3 +329,34 @@ fn parse_int_expr(expr: &Expr) -> Result<i64, &'static str> {
_ => Err("Not an integer expression"),
}
}

#[cfg(not(feature = "proc-macro-crate"))]
fn get_bytemuck_crate_name() -> TokenStream {
use proc_macro2::Span;
quote_spanned!(Span::mixed_site() => bytemuck)
}

#[cfg(feature = "proc-macro-crate")]
fn get_bytemuck_crate_name() -> TokenStream {
dman-os marked this conversation as resolved.
Show resolved Hide resolved
use proc_macro2::Span;
use proc_macro_crate::{crate_name, FoundCrate};

// check for bytemuck in Cargo.toml
let crate_name = crate_name("bytemuck");
match crate_name {
// if found
Ok(found_name) => match found_name {
// if macro is being used inside bytemuck codebase
FoundCrate::Itself => quote!(crate),
// if found in toml (possibly renamed)
FoundCrate::Name(name) => {
let ident = Ident::new(&name, Span::call_site());
quote!(::#ident)
}
},
// if not found
Err(_) => {
quote_spanned!(Span::mixed_site() => bytemuck)
}
}
}