This commit is contained in:
2025-06-22 15:11:37 +10:00
parent 5be3f2970c
commit 0667f79ff5
14 changed files with 508 additions and 319 deletions

View File

@@ -22,7 +22,7 @@ pub fn transform(_args: Args, mut item: Item) -> Result<TokenStream> {
_ => syn_error!(item, "expected struct or enum"),
};
let mod_name = format_ident!("__cdef__{name}");
let mod_name = format_ident!("__{name}_cdef");
Ok(quote! {
#[repr(C)]
@@ -42,23 +42,28 @@ pub fn transform(_args: Args, mut item: Item) -> Result<TokenStream> {
fn generate_type(ty: &Ident) -> Result<TokenStream> {
let ffi = ffi_crate();
let fmt = quote!(::std::format!);
let name_fmt = LitStr::new(&format!("{ty}"), ty.span());
let cdecl_fmt = LitStr::new(&format!("struct {ty} {{name}}"), ty.span());
let name = LitStr::new(&format!("{ty}"), ty.span());
let cdecl_fmt = LitStr::new(&format!("struct {ty} {{}}"), ty.span());
Ok(quote! {
unsafe impl #ffi::Type for #ty {
fn name() -> ::std::string::String {
#fmt(#name_fmt)
fn name() -> impl ::std::fmt::Display {
#name
}
fn cdecl(name: impl ::std::fmt::Display) -> ::std::string::String {
#fmt(#cdecl_fmt)
fn cdecl(name: impl ::std::fmt::Display) -> impl ::std::fmt::Display {
#fmt(#cdecl_fmt, name)
}
fn build(b: &mut #ffi::TypeBuilder) {
b.cdef::<Self>().metatype::<Self>();
}
}
unsafe impl #ffi::ToFfi for #ty {
type To = Self;
fn convert(self) -> Self::To { self }
}
})
}
@@ -113,6 +118,11 @@ struct CField {
attrs: CFieldAttrs,
}
#[derive(Default)]
struct CFieldAttrs {
opaque: bool,
}
fn to_cfields(fields: &mut Fields) -> Result<Vec<CField>> {
match fields {
Fields::Named(fields) => fields.named.iter_mut(),
@@ -133,11 +143,6 @@ fn to_cfields(fields: &mut Fields) -> Result<Vec<CField>> {
.collect()
}
#[derive(Default)]
struct CFieldAttrs {
opaque: bool,
}
fn parse_attrs(attrs: &mut Vec<Attribute>) -> Result<CFieldAttrs> {
let mut parsed = CFieldAttrs::default();
let mut i = 0;