use macros in macros
Some checks failed
Rust / build-gnu-apt (pull_request) Successful in 2m0s
Rust / build-size-gnu-unstable (pull_request) Failing after 2m48s

This commit is contained in:
Vinzenz Schroeter 2025-06-18 20:41:22 +02:00
parent 27f231eba0
commit 21987d05f3
2 changed files with 248 additions and 111 deletions

View file

@ -1,24 +1,22 @@
macro_rules! wrap_free {
($prefix:ident :: $typ:ty) => {
paste::paste! {
#[doc = concat!("Deallocates a [", stringify!($typ), "] instance.")]
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _free>](instance: NonNull<$typ>) {
$crate::macros::wrap_functions!($prefix;
#[doc = concat!("Deallocates a [`", stringify!($typ), "`] instance.")]
fn free(instance: NonNull<$typ>) {
unsafe { $crate::mem::heap_drop(instance) }
}
}
);
};
}
macro_rules! wrap_clone {
($prefix:ident :: $typ:ty) => {
paste::paste! {
#[doc = concat!("Clones a [", stringify!($typ), "] instance.")]
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _clone>](instance: NonNull<$typ>) -> NonNull<$typ> {
$crate::macros::wrap_functions!($prefix;
#[doc = concat!("Clones a [`", stringify!($typ), "`] instance.")]
fn clone(instance: NonNull<$typ>) -> NonNull<$typ> {
unsafe { $crate::mem::heap_clone(instance) }
}
}
);
};
}
@ -50,33 +48,34 @@ macro_rules! wrap_methods {
)+
) => {
paste::paste! {
$crate::macros::wrap_functions!($prefix;
$(
#[doc = concat!(" Calls [`servicepoint::", stringify!($object_type),
"::", stringify!($function), "`].")]
#[doc = ""]
$(#[$meta])*
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _ $function>](
instance: NonNull<$object_type>,
$($param_name: $param_type),*
) $(-> $return_type)? {
let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) };
$($(
$(let $param_let_name = $param_let_expr;)*
)?)?
#[allow(
unused_variables,
reason = "This variable may not be used depending on macro variables" )]
let result = instance.$function($($param_name),*);
$(
#[doc = concat!(" Calls [`servicepoint::", stringify!($object_type),
"::", stringify!($function), "`].")]
#[doc = ""]
$(#[$meta])*
fn $function(
instance: NonNull<$object_type>,
$($param_name: $param_type),*
) $(-> $return_type)? {
let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) };
$($(
$(let $param_let_name = $param_let_expr;)*
)?)?
#[allow(
unused_variables,
reason = "This variable may not be used depending on macro variables" )]
let result = instance.$function($($param_name),*);
$(
let $it = result;
let result = $return_expr;
$(
let $it = result;
let result = $return_expr;
)?
)?
)?
return result;
}
return result;
}
)+
);
}
};
}
@ -103,48 +102,48 @@ macro_rules! wrap_fields {
)+
) => {
paste::paste! {
$(
$crate::macros::wrap_functions!($prefix;
$(
#[doc = concat!("Gets the value of field `", stringify!($prop_name),
"` of the [`servicepoint::",stringify!($object_type),"`].")]
$($(
#[doc = ""]
#[$get_meta]
)*)?
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _ get _ $prop_name>](
instance: NonNull<$object_type>
) -> $prop_type {
let instance = unsafe { $crate::macros::nonnull_as_ref!(instance) };
let $prop_name = instance.$prop_name;
$(
#[doc = concat!(" Gets the value of field `", stringify!($prop_name),
"` of the [`servicepoint::",stringify!($object_type),"`].")]
$($(
let $prop_name = $get_expr;
)?)?
return $prop_name;
}
)?
#[doc = ""]
#[$get_meta]
)*)?
fn [<get _ $prop_name>](
instance: NonNull<$object_type>
) -> $prop_type {
let instance = unsafe { $crate::macros::nonnull_as_ref!(instance) };
let $prop_name = instance.$prop_name;
$($(
let $prop_name = $get_expr;
)?)?
return $prop_name;
}
)?
$(
#[doc = concat!("Sets the value of field `", stringify!($prop_name),
"` of the [`servicepoint::",stringify!($object_type),"`].")]
$($(
#[doc = ""]
#[$set_meta]
)*)?
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _ set _ $prop_name>](
instance: NonNull<$object_type>,
value: $prop_type,
) {
let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) };
$(
#[doc = concat!(" Sets the value of field `", stringify!($prop_name),
"` of the [`servicepoint::",stringify!($object_type),"`].")]
$($(
let $value = value;
let value = $set_expr;
)?)?
instance.$prop_name = value;
}
)?
)+
#[doc = ""]
#[$set_meta]
)*)?
fn [<set _ $prop_name>](
instance: NonNull<$object_type>,
value: $prop_type,
) {
let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) };
$($(
let $value = value;
let value = $set_expr;
)?)?
instance.$prop_name = value;
}
)?
)+
);
}
};
}