From 4e79d5dcc6a07e40f84a316b37d3e283e068d013 Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Tue, 31 Oct 2023 19:17:47 +0100 Subject: [PATCH] Remove useless cyclic dependency (#2387) * Remove useless cyclic dependency * Move the call to `crate_ident` outside of the derive functions --- vulkano-macros/Cargo.toml | 3 --- vulkano-macros/src/derive_buffer_contents.rs | 27 ++++++++++++-------- vulkano-macros/src/derive_vertex.rs | 4 +-- vulkano-macros/src/lib.rs | 6 +++-- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/vulkano-macros/Cargo.toml b/vulkano-macros/Cargo.toml index 5f54724e..b89ec692 100644 --- a/vulkano-macros/Cargo.toml +++ b/vulkano-macros/Cargo.toml @@ -20,6 +20,3 @@ syn = { workspace = true } quote = { workspace = true } proc-macro2 = { workspace = true } proc-macro-crate = { workspace = true } - -[dev-dependencies] -vulkano = { workspace = true } diff --git a/vulkano-macros/src/derive_buffer_contents.rs b/vulkano-macros/src/derive_buffer_contents.rs index 42ca2270..4b5a8872 100644 --- a/vulkano-macros/src/derive_buffer_contents.rs +++ b/vulkano-macros/src/derive_buffer_contents.rs @@ -15,9 +15,7 @@ use syn::{ Result, Type, TypeArray, TypeSlice, WherePredicate, }; -pub fn derive_buffer_contents(mut ast: DeriveInput) -> Result { - let crate_ident = crate::crate_ident(); - +pub fn derive_buffer_contents(crate_ident: &Ident, mut ast: DeriveInput) -> Result { let struct_ident = &ast.ident; let is_repr_rust = ast @@ -63,7 +61,7 @@ pub fn derive_buffer_contents(mut ast: DeriveInput) -> Result { ast.generics.split_for_impl() }; - let layout = write_layout(&crate_ident, &ast)?; + let layout = write_layout(crate_ident, &ast)?; Ok(quote! { #[allow(unsafe_code)] @@ -269,25 +267,28 @@ fn find_innermost_element_type(mut field_type: &Type) -> &Type { #[cfg(test)] mod tests { use super::*; + use proc_macro2::Span; #[test] fn repr() { + let crate_ident = Ident::new("vulkano", Span::call_site()); + let default_repr = parse_quote! { struct Test(u8, [u8]); }; - assert!(derive_buffer_contents(default_repr).is_err()); + assert!(derive_buffer_contents(&crate_ident, default_repr).is_err()); let irellevant_reprs = parse_quote! { #[repr(packed(2), align(16))] struct Test(u8, [u8]); }; - assert!(derive_buffer_contents(irellevant_reprs).is_err()); + assert!(derive_buffer_contents(&crate_ident, irellevant_reprs).is_err()); let transparent_repr = parse_quote! { #[repr(transparent)] struct Test([u8]); }; - assert!(derive_buffer_contents(transparent_repr).is_ok()); + assert!(derive_buffer_contents(&crate_ident, transparent_repr).is_ok()); let multiple_reprs = parse_quote! { #[repr(align(16))] @@ -295,24 +296,28 @@ mod tests { #[repr(packed)] struct Test(u8, [u8]); }; - assert!(derive_buffer_contents(multiple_reprs).is_ok()); + assert!(derive_buffer_contents(&crate_ident, multiple_reprs).is_ok()); } #[test] fn zero_sized() { + let crate_ident = Ident::new("vulkano", Span::call_site()); + let unit = parse_quote! { struct Test; }; - assert!(derive_buffer_contents(unit).is_err()); + assert!(derive_buffer_contents(&crate_ident, unit).is_err()); } #[test] fn unsupported_datatype() { + let crate_ident = Ident::new("vulkano", Span::call_site()); + let enum_ = parse_quote! { #[repr(C)] enum Test { A, B, C } }; - assert!(derive_buffer_contents(enum_).is_err()); + assert!(derive_buffer_contents(&crate_ident, enum_).is_err()); let union = parse_quote! { #[repr(C)] @@ -321,6 +326,6 @@ mod tests { b: f32, } }; - assert!(derive_buffer_contents(union).is_err()); + assert!(derive_buffer_contents(&crate_ident, union).is_err()); } } diff --git a/vulkano-macros/src/derive_vertex.rs b/vulkano-macros/src/derive_vertex.rs index 1da12017..c96408f2 100644 --- a/vulkano-macros/src/derive_vertex.rs +++ b/vulkano-macros/src/derive_vertex.rs @@ -16,7 +16,7 @@ use syn::{ Data, DataStruct, Fields, Ident, LitStr, Result, Token, }; -pub fn derive_vertex(ast: syn::DeriveInput) -> Result { +pub fn derive_vertex(crate_ident: &Ident, ast: syn::DeriveInput) -> Result { let struct_name = &ast.ident; let fields = match &ast.data { @@ -27,8 +27,6 @@ pub fn derive_vertex(ast: syn::DeriveInput) -> Result { _ => bail!("expected a struct with named fields"), }; - let crate_ident = crate::crate_ident(); - let mut members = quote! { let mut offset = 0; let mut members = ::std::collections::HashMap::default(); diff --git a/vulkano-macros/src/lib.rs b/vulkano-macros/src/lib.rs index 9649f30e..a9b0507b 100644 --- a/vulkano-macros/src/lib.rs +++ b/vulkano-macros/src/lib.rs @@ -24,8 +24,9 @@ mod derive_vertex; #[proc_macro_derive(Vertex, attributes(name, format))] pub fn derive_vertex(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); + let crate_ident = crate_ident(); - derive_vertex::derive_vertex(ast) + derive_vertex::derive_vertex(&crate_ident, ast) .unwrap_or_else(Error::into_compile_error) .into() } @@ -36,8 +37,9 @@ pub fn derive_vertex(input: TokenStream) -> TokenStream { #[proc_macro_derive(BufferContents)] pub fn derive_buffer_contents(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); + let crate_ident = crate_ident(); - derive_buffer_contents::derive_buffer_contents(ast) + derive_buffer_contents::derive_buffer_contents(&crate_ident, ast) .unwrap_or_else(Error::into_compile_error) .into() }