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

Add ability to emit type aliases to C headers #228

Merged
merged 5 commits into from
Jul 15, 2024
Merged
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ path = "src/_lib.rs"

[package]
name = "safer-ffi"
version = "0.1.9" # Keep in sync
version = "0.1.10-rc1" # Keep in sync
authors = [
"Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>",
]
Expand Down Expand Up @@ -179,7 +179,7 @@ version = "0.0.3"

[dependencies.safer_ffi-proc_macros]
path = "src/proc_macro"
version = "=0.1.9" # Keep in sync
version = "=0.1.10-rc1" # Keep in sync

[workspace]
members = [
Expand Down
4 changes: 2 additions & 2 deletions ffi_tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ffi_tests/generated.cffi
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ int32_t const *
max (
slice_ref_int32_t xs);

void *
my_renamed_ptr_api (void);

foo_t *
new_foo (void);

Expand Down
5 changes: 5 additions & 0 deletions ffi_tests/generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ public unsafe partial class Ffi {
slice_ref_int32_t xs);
}

public unsafe partial class Ffi {
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
void * my_renamed_ptr_api ();
}

public unsafe partial class Ffi {
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
foo_t * new_foo ();
Expand Down
7 changes: 7 additions & 0 deletions ffi_tests/generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ int32_t const *
max (
slice_ref_int32_t xs);

/** <No documentation available> */
typedef void * my_renamed_ptr_t;

/** <No documentation available> */
my_renamed_ptr_t
my_renamed_ptr_api (void);

/** <No documentation available> */
foo_t *
new_foo (void);
Expand Down
15 changes: 15 additions & 0 deletions ffi_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ pub struct MyPtr {
bar: (),
}

#[derive_ReprC(rename = "my_renamed_ptr")]
#[repr(transparent)]
pub struct MyRenamedPtr {
foo: ::core::ptr::NonNull<()>,
bar: (),
}

#[ffi_export]
fn my_renamed_ptr_api() -> MyRenamedPtr {
MyRenamedPtr {
foo: ::core::ptr::NonNull::new(0xbad000 as _).unwrap(),
bar: ()
}
}

macro_rules! docs {() => (
"Hello, `World`!"
)}
Expand Down
2 changes: 2 additions & 0 deletions ffi_tests/tests/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ int main (
});
while (X > 0);

assert(my_renamed_ptr_api() == (void *)0xbad000);

puts("C: [ok]");

return EXIT_SUCCESS;
Expand Down
4 changes: 4 additions & 0 deletions ffi_tests/tests/csharp/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ static void Main(string[] _)
Trace.Assert(Ffi.returns_a_fn_ptr()(0x42) == 0x4200);
}

unsafe {
Trace.Assert(Ffi.my_renamed_ptr_api() == (void *)0xbad000);
}

Console.WriteLine("C#: [ok]");
}
}
4 changes: 2 additions & 2 deletions js_tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions js_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,18 @@ fn sleep (ms: u32)
let _ = wasm_bindgen_futures::JsFuture::from(sleep(ms)).await;
}
}

#[derive_ReprC(js, rename = "my_renamed_ptr")]
#[repr(transparent)]
pub struct MyRenamedPtr {
pub foo: ::core::ptr::NonNull<()>,
pub bar: (),
}

#[ffi_export(js)]
fn my_renamed_ptr_api() -> MyRenamedPtr {
MyRenamedPtr {
foo: ::core::ptr::NonNull::new(0xbad000 as _).unwrap(),
bar: (),
}
}
44 changes: 23 additions & 21 deletions js_tests/tests/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -284,29 +284,31 @@ export async function run_tests({ ffi, performance, assert, is_web }) {
)
}

if (!is_web) {
assert.equal(ffi.getDeadlockTimeout(), 5000);

let error;
try {
ffi.setDeadlockTimeout("huehuehue");
} catch (e) {
error = e;
if (!is_web) {
assert.equal(ffi.getDeadlockTimeout(), 5000);

let error;
try {
ffi.setDeadlockTimeout("huehuehue");
} catch (e) {
error = e;
}
assert.equal(error?.message, "Expected a positive number");

ffi.setDeadlockTimeout(500);

assert.equal(ffi.getDeadlockTimeout(), 500);

error = null;
try {
ffi.setDeadlockTimeout(0);
} catch (e) {
error = e;
}
assert.equal(error?.message, "Deadlock timeout can only be set once");
}
assert.equal(error?.message, "Expected a positive number");

ffi.setDeadlockTimeout(500);

assert.equal(ffi.getDeadlockTimeout(), 500);

error = null;
try {
ffi.setDeadlockTimeout(0);
} catch (e) {
error = e;
}
assert.equal(error?.message, "Deadlock timeout can only be set once");
}
assert.equal(ffi.my_renamed_ptr_api().addr, 0xbad000);

console.log('Js tests passed successfully ✅');
}
Expand Down
2 changes: 0 additions & 2 deletions src/headers/_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ mod languages;
pub use definer::{Definer, HashSetDefiner};
mod definer;



match_! {(
/// Sets up the name of the `ifndef` guard of the header file.
///
Expand Down
29 changes: 29 additions & 0 deletions src/headers/languages/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,35 @@ impl HeaderLanguage for C {
Ok(())
}

fn supports_type_aliases(self: &'_ C)
-> Option<&'_ dyn HeaderLanguageSupportingTypeAliases>
{
return Some(self);
// where
impl HeaderLanguageSupportingTypeAliases for C {
fn emit_type_alias(
self: &'_ Self,
ctx: &'_ mut dyn Definer,
docs: Docs<'_>,
self_ty: &'_ dyn PhantomCType,
inner_ty: &'_ dyn PhantomCType,
) -> io::Result<()>
{
let ref indent = Indentation::new(4 /* ctx.indent_width() */);
mk_out!(indent, ctx.out());
self.emit_docs(ctx, docs, indent)?;
let ref aliaser = self_ty.name(self);
let ref aliasee = inner_ty.name(self);
out!((
"typedef {aliasee} {aliaser};"
));

out!("\n");
Ok(())
}
}
}

fn emit_simple_enum (
self: &'_ Self,
ctx: &'_ mut dyn Definer,
Expand Down
20 changes: 19 additions & 1 deletion src/headers/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@ type Docs<'lt> = &'lt [&'lt str];

pub
trait HeaderLanguage : UpcastAny {
fn language_name(&self) -> &'static str {
fn language_name(self: &'_ Self) -> &'static str {
::core::any::type_name::<Self>()
}

fn supports_type_aliases(self: &'_ Self)
-> Option<&'_ dyn HeaderLanguageSupportingTypeAliases>
{
None
}

fn emit_simple_enum (
self: &'_ Self,
ctx: &'_ mut dyn Definer,
Expand Down Expand Up @@ -128,6 +134,18 @@ trait HeaderLanguage : UpcastAny {
}
}

pub
trait HeaderLanguageSupportingTypeAliases : HeaderLanguage {
fn emit_type_alias(
self: &'_ Self,
ctx: &'_ mut dyn Definer,
docs: Docs<'_>,
self_ty: &'_ dyn PhantomCType,
inner_ty: &'_ dyn PhantomCType,
) -> io::Result<()>
;
}

pub
struct EnumVariant<'lt> {
pub
Expand Down
2 changes: 1 addition & 1 deletion src/proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ proc-macro = true

[package]
name = "safer_ffi-proc_macros"
version = "0.1.9" # Keep in sync
version = "0.1.10-rc1" # Keep in sync
authors = ["Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>"]
edition = "2021"

Expand Down
Loading
Loading