Remove useless cyclic dependency (#2387)

* Remove useless cyclic dependency

* Move the call to `crate_ident` outside of the derive functions
This commit is contained in:
marc0246 2023-10-31 19:17:47 +01:00 committed by GitHub
parent af57608ac5
commit 4e79d5dcc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 19 deletions

View File

@ -20,6 +20,3 @@ syn = { workspace = true }
quote = { workspace = true }
proc-macro2 = { workspace = true }
proc-macro-crate = { workspace = true }
[dev-dependencies]
vulkano = { workspace = true }

View File

@ -15,9 +15,7 @@ use syn::{
Result, Type, TypeArray, TypeSlice, WherePredicate,
};
pub fn derive_buffer_contents(mut ast: DeriveInput) -> Result<TokenStream> {
let crate_ident = crate::crate_ident();
pub fn derive_buffer_contents(crate_ident: &Ident, mut ast: DeriveInput) -> Result<TokenStream> {
let struct_ident = &ast.ident;
let is_repr_rust = ast
@ -63,7 +61,7 @@ pub fn derive_buffer_contents(mut ast: DeriveInput) -> Result<TokenStream> {
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());
}
}

View File

@ -16,7 +16,7 @@ use syn::{
Data, DataStruct, Fields, Ident, LitStr, Result, Token,
};
pub fn derive_vertex(ast: syn::DeriveInput) -> Result<TokenStream> {
pub fn derive_vertex(crate_ident: &Ident, ast: syn::DeriveInput) -> Result<TokenStream> {
let struct_name = &ast.ident;
let fields = match &ast.data {
@ -27,8 +27,6 @@ pub fn derive_vertex(ast: syn::DeriveInput) -> Result<TokenStream> {
_ => 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();

View File

@ -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()
}