From daaca02abb19cb3d7538986645e667e8827b4866 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 25 Oct 2026 15:54:54 -0700 Subject: [PATCH 1/3] feat(uniffi): put generated C methods under _method_ The C backend joined a type's name and a method's, so moq-ffi's MoqAudioEncoder::finish and MoqAudioEncoderFinish both became moq_audio_encoder_finish. Methods and named constructors now go under _method_, and a remaining clash names both items. Co-Authored-By: Claude Opus 5.5 --- uniffi/MIGRATE.md | 4 + uniffi/cpp/README.md | 9 ++- uniffi/cpp/bindgen/src/bindings/c/gen_c.rs | 75 +++++++++++++------ uniffi/cpp/cpp-tests/tests/c_api/main.c | 72 ++++++++++-------- .../cpp/cpp-tests/tests/c_dispatcher/main.c | 10 +-- .../cpp/cpp-tests/tests/c_namespaces/main.c | 22 +++--- uniffi/cpp/fixtures/c-api/src/lib.rs | 14 ++++ 7 files changed, 132 insertions(+), 74 deletions(-) diff --git a/uniffi/MIGRATE.md b/uniffi/MIGRATE.md index 09bc22e8d..91391e449 100644 --- a/uniffi/MIGRATE.md +++ b/uniffi/MIGRATE.md @@ -14,3 +14,7 @@ style, where an async call returns `uniffi::Future`. - Generated C: every type and function name starts with the crate's namespace, so a crate whose items did not already start with it gets renamed C symbols. +- Generated C: methods and named constructors gain `_method_` after their type's name + (`app_my_object_do_thing` is now `app_my_object_method_do_thing`), and so do their async + callback typedefs. The primary constructor `_new`, `_free`, and top-level functions keep their + names. diff --git a/uniffi/cpp/README.md b/uniffi/cpp/README.md index a656e719a..32794079b 100644 --- a/uniffi/cpp/README.md +++ b/uniffi/cpp/README.md @@ -55,9 +55,12 @@ uniffi-bindgen-cpp --lang c --library your_rust_library.so --out-dir output_dire ``` Names are the uniffi names in snake case, prefixed with the namespace unless they already start -with it, since every crate's C API shares one scope: in namespace `app`, `MyObject.do_thing` is -`app_my_object_do_thing`, and its handle is `app_my_object *`, released with -`app_my_object_free`; `AppObject` is `app_object`. The rest follows from a few rules: +with it, since every crate's C API shares one scope: in namespace `app`, `MyObject` is +`app_my_object *`, created with `app_my_object_new` and released with `app_my_object_free`, and +`AppObject` is `app_object`. A method or named constructor goes under `_method_`, so +`MyObject.do_thing` is `app_my_object_method_do_thing` and never meets a type named +`MyObjectDoThing`. Two items that still render one name, such as `AppObject` and `Object`, are +refused with both named. The rest follows from a few rules: * A call that can fail returns an error struct, or NULL on success, and writes its result to a trailing `out` pointer. An error is a tagged union with a `message`; free it with `_free`. diff --git a/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs b/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs index 356221f54..ac9535448 100644 --- a/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs +++ b/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs @@ -212,6 +212,16 @@ impl CType { } } + /// The uniffi item this type renders, for errors. + fn item(&self) -> String { + match &self.ty { + Type::Record { name, .. } => format!("record `{name}`"), + Type::Enum { name, .. } => format!("enum `{name}`"), + Type::Object { name, .. } => format!("object `{name}`"), + _ => format!("type `{}`", self.cpp), + } + } + /// The function that frees a returned value, when the caller owns an allocation. fn free_fn(&self, ns: &str) -> Option { match &self.kind { @@ -290,8 +300,9 @@ struct Gen<'a> { ns: String, /// Every type a converter is emitted for, by canonical name. types: BTreeMap, - /// Every global C identifier, to refuse a collision instead of emitting one. - names: BTreeSet, + /// Every global C identifier and the item that claimed it, to refuse a collision instead of + /// emitting one. + names: BTreeMap, /// The crates whose types this one uses, each checked once for C support. externals: BTreeSet, } @@ -303,7 +314,7 @@ impl<'a> Gen<'a> { config, ns: ci.namespace().to_snake_case(), types: BTreeMap::new(), - names: BTreeSet::new(), + names: BTreeMap::new(), externals: BTreeSet::new(), } } @@ -319,9 +330,10 @@ impl<'a> Gen<'a> { .collect() } - fn claim(&mut self, name: &str) -> Result<()> { - if !self.names.insert(name.to_string()) { - bail!("the C backend would emit `{name}` twice; rename one of the uniffi items"); + /// Registers `name` for `item`, which describes the uniffi item the name renders. + fn claim(&mut self, name: &str, item: &str) -> Result<()> { + if let Some(other) = self.names.insert(name.to_string(), item.to_string()) { + bail!("the C backend names both {other} and {item} `{name}`; rename one of them"); } Ok(()) } @@ -558,7 +570,7 @@ impl<'a> Gen<'a> { "shutdown_dispatcher", "string_free", ] { - self.claim(&format!("{ns}_{name}"))?; + self.claim(&format!("{ns}_{name}"), &format!("the runtime's `{name}`"))?; } // Register every reachable type before rendering any of them. @@ -593,7 +605,7 @@ impl<'a> Gen<'a> { | Kind::List(_) | Kind::Map(..) ) { - self.claim(&ct.c)?; + self.claim(&ct.c, &ct.item())?; writeln!(forwards, "typedef struct {0} {0};", ct.c)?; } } @@ -675,6 +687,7 @@ impl<'a> Gen<'a> { self.ci.namespace(), filters::fn_name(func.name()).unwrap() ); + self.claim(&name, &format!("function `{}`", func.name()))?; self.render_callable(h, s, func, &name, None, &call)?; } @@ -688,12 +701,15 @@ impl<'a> Gen<'a> { fn render_flat_enum(&mut self, h: &mut String, ct: &CType) -> Result<()> { let e = self.enum_def(&ct.ty)?; - self.claim(&ct.c)?; + self.claim(&ct.c, &ct.item())?; h.push_str(&doc(e.docstring(), 0)); writeln!(h, "typedef enum {} {{", ct.c)?; for (i, variant) in e.variants().iter().enumerate() { let constant = variant_constant(&ct.c, variant.name()); - self.claim(&constant)?; + self.claim( + &constant, + &format!("variant `{}::{}`", e.name(), variant.name()), + )?; h.push_str(&doc(variant.docstring(), 4)); let comma = if i + 1 < e.variants().len() { "," } else { "" }; writeln!(h, " {constant}{comma}")?; @@ -798,7 +814,7 @@ impl<'a> Gen<'a> { if rec.fields().iter().any(|f| f.default_value().is_some()) { let name = format!("{}_default", ct.c); - self.claim(&name)?; + self.claim(&name, &format!("the defaults of {}", ct.item()))?; h.push_str(&line_doc( "A record with every defaulted field set and the rest zeroed, to fill in before passing it.", )); @@ -885,13 +901,16 @@ impl<'a> Gen<'a> { let e = self.enum_def(&ct.ty)?; let error = self.ci.is_used_as_error(e); let tag = format!("{}_tag", ct.c); - self.claim(&tag)?; + self.claim(&tag, &format!("the tag of {}", ct.item()))?; h.push_str(&line_doc(&format!("Which variant a `{}` holds.", ct.c))); writeln!(h, "typedef enum {tag} {{")?; for (i, variant) in e.variants().iter().enumerate() { let constant = variant_constant(&ct.c, variant.name()); - self.claim(&constant)?; + self.claim( + &constant, + &format!("variant `{}::{}`", e.name(), variant.name()), + )?; h.push_str(&doc(variant.docstring(), 4)); let comma = if i + 1 < e.variants().len() { "," } else { "" }; writeln!(h, " {constant}{comma}")?; @@ -912,7 +931,10 @@ impl<'a> Gen<'a> { ); } let fields = format!("{}_{member}", ct.c); - self.claim(&fields)?; + self.claim( + &fields, + &format!("the fields of variant `{}::{}`", e.name(), variant.name()), + )?; h.push_str(&line_doc(&format!( "The fields of a `{}` holding `{}`.", ct.c, @@ -955,7 +977,7 @@ impl<'a> Gen<'a> { if error { self.render_free(h, s, ct, "this error")?; let name = format!("{}_message", ct.c); - self.claim(&name)?; + self.claim(&name, &format!("the message of {}", ct.item()))?; h.push_str(&line_doc("The error's message, owned by the error.")); writeln!(h, "const char *{name}(const {} *error);\n", ct.c)?; writeln!( @@ -979,7 +1001,7 @@ impl<'a> Gen<'a> { what: &str, ) -> Result<()> { let name = format!("{}_free", ct.c); - self.claim(&name)?; + self.claim(&name, &format!("the release function of {}", ct.item()))?; h.push_str(&line_doc(&format!( "Frees {what}, as returned by this API; NULL is ignored." ))); @@ -1006,34 +1028,44 @@ impl<'a> Gen<'a> { } let free = format!("{c}_free"); - self.claim(&free)?; + self.claim( + &free, + &format!("the release function of object `{}`", obj.name()), + )?; h.push_str(&line_doc(&format!( "Releases this `{c}` handle; NULL is ignored. Other handles to the same object stay valid." ))); writeln!(h, "void {free}({c} *self);\n")?; writeln!(s, "void {free}({c} *self) {{\n delete self;\n}}\n")?; + // `_method_` keeps a function's name from spelling another type's (`AudioEncoder::finish` + // against `AudioEncoderFinish`). for ctor in obj.constructors() { + let item = format!("constructor `{}::{}`", obj.name(), ctor.name()); let (name, call) = if ctor.is_primary_constructor() { (format!("{c}_new"), format!("::{namespace}::{class}::init")) } else { ( - format!("{c}_{}", c_name(ctor.name())), + format!("{c}_method_{}", c_name(ctor.name())), format!("::{namespace}::{class}::{}", ctor.name()), ) }; + self.claim(&name, &item)?; self.render_callable(h, s, ctor, &name, None, &call)?; } for method in obj.methods() { - let name = format!("{c}_{}", c_name(method.name())); + let item = format!("method `{}::{}`", obj.name(), method.name()); + let name = format!("{c}_method_{}", c_name(method.name())); let call = format!("self->inner->{}", filters::fn_name(method.name()).unwrap()); + self.claim(&name, &item)?; self.render_callable(h, s, method, &name, Some(&c), &call)?; } Ok(()) } - /// Renders one function, constructor, or method. `call` names the C++ callee. + /// Renders one function, constructor, or method, after the caller claimed its `name`. + /// `call` names the C++ callee. fn render_callable( &mut self, h: &mut String, @@ -1045,7 +1077,6 @@ impl<'a> Gen<'a> { ) -> Result<()> { let namespace = self.ci.namespace().to_string(); let ns = self.ns.clone(); - self.claim(name)?; let mut params = Vec::new(); let mut checks = String::new(); @@ -1135,7 +1166,7 @@ impl<'a> Gen<'a> { } } let callback = format!("{name}_callback"); - self.claim(&callback)?; + self.claim(&callback, &format!("the callback of `{name}`"))?; let mut cb_params = vec!["void *user_data".to_string()]; if let Some(err) = &err { diff --git a/uniffi/cpp/cpp-tests/tests/c_api/main.c b/uniffi/cpp/cpp-tests/tests/c_api/main.c index e14c98972..cf7927c7b 100644 --- a/uniffi/cpp/cpp-tests/tests/c_api/main.c +++ b/uniffi/cpp/cpp-tests/tests/c_api/main.c @@ -133,7 +133,7 @@ static void records(void) { /* The echoed record owns a new handle to the same store. */ CHECK(echo->owner != NULL && echo->owner != owner); - owner_name = capi_store_name(echo->owner); + owner_name = capi_store_method_name(echo->owner); CHECK(streq(owner_name, "owner")); capi_string_free(owner_name); @@ -257,16 +257,17 @@ static void objects(void) { capi_item_list list; capi_item *got = NULL; capi_string_list *names; + capi_store_stats *stats; capi_optional_u64 limit; uint32_t index; char *text; - error = capi_store_open("", &store); + error = capi_store_method_open("", &store); CHECK(error && error->tag == CAPI_ERROR_INVALID && store == NULL); capi_error_free(error); - CHECK(capi_store_open("main", &store) == NULL && store != NULL); - CHECK(capi_store_len(store) == 0); + CHECK(capi_store_method_open("main", &store) == NULL && store != NULL); + CHECK(capi_store_method_len(store) == 0); item = capi_item_default(); item.name = "one"; @@ -274,62 +275,67 @@ static void objects(void) { item.limit.has_value = true; item.limit.value = 5; index = 99; - CHECK(capi_store_add(store, &item, &index) == NULL && index == 0); + CHECK(capi_store_method_add(store, &item, &index) == NULL && index == 0); /* A failed call clears its out-param. */ item.name = ""; index = 99; - error = capi_store_add(store, &item, &index); + error = capi_store_method_add(store, &item, &index); CHECK(error && error->tag == CAPI_ERROR_INVALID && index == 0); capi_error_free(error); item.name = "two"; item.note = NULL; item.limit.has_value = false; - CHECK(capi_store_add(store, &item, &index) == NULL && index == 1); - CHECK(capi_store_len(store) == 2); + CHECK(capi_store_method_add(store, &item, &index) == NULL && index == 1); + CHECK(capi_store_method_len(store) == 2); - CHECK(capi_store_get(store, 1, &got) == NULL); + CHECK(capi_store_method_get(store, 1, &got) == NULL); CHECK(streq(got->name, "two") && got->note == NULL); capi_item_free(got); - error = capi_store_get(store, 5, &got); + error = capi_store_method_get(store, 5, &got); CHECK(error && error->tag == CAPI_ERROR_CODE && error->code.code == 5 && got == NULL); capi_error_free(error); - got = capi_store_find(store, "one"); + got = capi_store_method_find(store, "one"); CHECK(got && got->count == 7); capi_item_free(got); - CHECK(capi_store_find(store, "nope") == NULL); + CHECK(capi_store_method_find(store, "nope") == NULL); - text = capi_store_note(store, 0); + text = capi_store_method_note(store, 0); CHECK(streq(text, "n1")); capi_string_free(text); - CHECK(capi_store_note(store, 1) == NULL); + CHECK(capi_store_method_note(store, 1) == NULL); - limit = capi_store_limit(store, 0); + limit = capi_store_method_limit(store, 0); CHECK(limit.has_value && limit.value == 5); - limit = capi_store_limit(store, 1); + limit = capi_store_method_limit(store, 1); CHECK(!limit.has_value); - names = capi_store_names(store); + names = capi_store_method_names(store); CHECK(names->len == 2 && streq(names->data[0], "one") && streq(names->data[1], "two")); capi_string_list_free(names); - fork = capi_store_fork(store, NULL); - text = capi_store_name(fork); + fork = capi_store_method_fork(store, NULL); + text = capi_store_method_name(fork); CHECK(streq(text, "main")); capi_string_free(text); - renamed = capi_store_fork(store, "other"); - text = capi_store_name(renamed); + renamed = capi_store_method_fork(store, "other"); + text = capi_store_method_name(renamed); CHECK(streq(text, "other")); capi_string_free(text); - CHECK(capi_store_total(store, fork, NULL) == 4); - CHECK(capi_store_total(store, fork, renamed) == 6); + CHECK(capi_store_method_total(store, fork, NULL) == 4); + CHECK(capi_store_method_total(store, fork, renamed) == 6); - capi_store_clear(store); - CHECK(capi_store_len(store) == 0 && capi_store_len(fork) == 2); + /* The method and the record it returns share a snake-case name. */ + stats = capi_store_method_stats(store); + CHECK(stats->items == 2 && stats->queued == 0); + capi_store_stats_free(stats); + + capi_store_method_clear(store); + CHECK(capi_store_method_len(store) == 0 && capi_store_method_len(fork) == 2); items[0] = capi_item_default(); items[0].name = "x"; @@ -337,8 +343,8 @@ static void objects(void) { items[1].name = "y"; list.data = items; list.len = 2; - filled = capi_store_with_items("filled", &list); - CHECK(capi_store_len(filled) == 2); + filled = capi_store_method_with_items("filled", &list); + CHECK(capi_store_method_len(filled) == 2); capi_store_free(filled); capi_store_free(renamed); @@ -418,8 +424,8 @@ static void async_calls(void) { capi_task_free(task); /* Pending until another call wakes it. */ - task = capi_store_next(store, on_next, &r); - capi_store_push(store, 42); + task = capi_store_method_next(store, on_next, &r); + capi_store_method_push(store, 42); test_latch_wait(r.done); CHECK(r.error == NULL && r.value == 42); capi_task_free(task); @@ -427,8 +433,8 @@ static void async_calls(void) { /* Freeing a task whose callback is running elsewhere waits for the callback. */ r.entered = test_latch_new(); r.release = test_latch_new(); - task = capi_store_next(store, on_next_slow, &r); - capi_store_push(store, 7); + task = capi_store_method_next(store, on_next_slow, &r); + capi_store_method_push(store, 7); test_latch_wait(r.entered); test_latch_signal_from_thread(r.release); capi_task_free(task); @@ -436,8 +442,8 @@ static void async_calls(void) { test_latch_free(r.entered); test_latch_free(r.release); - capi_store_close(store); - task = capi_store_next(store, on_next, &r); + capi_store_method_close(store); + task = capi_store_method_next(store, on_next, &r); test_latch_wait(r.done); CHECK(r.error && r.error->tag == CAPI_ERROR_NOT_FOUND); capi_error_free(r.error); diff --git a/uniffi/cpp/cpp-tests/tests/c_dispatcher/main.c b/uniffi/cpp/cpp-tests/tests/c_dispatcher/main.c index 822b6c87e..73f6fa57b 100644 --- a/uniffi/cpp/cpp-tests/tests/c_dispatcher/main.c +++ b/uniffi/cpp/cpp-tests/tests/c_dispatcher/main.c @@ -113,10 +113,10 @@ int main(void) { /* A pending future calls back once woken and drained. */ memset(&r, 0, sizeof r); - r.task = capi_store_next(store, on_next, &r); + r.task = capi_store_method_next(store, on_next, &r); drain(); CHECK(r.calls == 0); - capi_store_push(store, 11); + capi_store_method_push(store, 11); CHECK(r.calls == 0); drain(); CHECK(r.calls == 1 && r.error == NULL && r.value == 11); @@ -124,7 +124,7 @@ int main(void) { /* Freeing a pending task drops the Rust future, and its callback never runs. */ memset(&r, 0, sizeof r); - r.task = capi_store_wait_forever(store, on_wait, &r); + r.task = capi_store_method_wait_forever(store, on_wait, &r); CHECK(capi_pending_count() == 1); capi_task_free(r.task); drain(); @@ -165,10 +165,10 @@ int main(void) { /* Shutdown drains through the host, then no call calls back. */ memset(&r, 0, sizeof r); - r.task = capi_store_next(store, on_next, &r); + r.task = capi_store_method_next(store, on_next, &r); capi_shutdown_dispatcher(); CHECK(shutdowns == 1); - capi_store_push(store, 5); + capi_store_method_push(store, 5); CHECK(queued == 0); CHECK(r.calls == 0); capi_task_free(r.task); diff --git a/uniffi/cpp/cpp-tests/tests/c_namespaces/main.c b/uniffi/cpp/cpp-tests/tests/c_namespaces/main.c index ea730b0f6..610de5cca 100644 --- a/uniffi/cpp/cpp-tests/tests/c_namespaces/main.c +++ b/uniffi/cpp/cpp-tests/tests/c_namespaces/main.c @@ -28,26 +28,26 @@ static void object_wraps_its_namesake(void) { media_group_consumer *group = media_group_consumer_new(inner); net_group_consumer_free(inner); - net_group_consumer *held = media_group_consumer_group(group); - CHECK(net_group_consumer_sequence(held) == 3); + net_group_consumer *held = media_group_consumer_method_group(group); + CHECK(net_group_consumer_method_sequence(held) == 3); net_group_consumer_free(held); - media_priority *priority = media_group_consumer_priority(group); + media_priority *priority = media_group_consumer_method_priority(group); CHECK(priority->tag == MEDIA_PRIORITY_NET); CHECK(priority->net.priority == NET_PRIORITY_LOW); media_priority_free(priority); media_frame *frame = NULL; - CHECK(media_group_consumer_read_frame(group, &frame) == NULL); + CHECK(media_group_consumer_method_read_frame(group, &frame) == NULL); CHECK(payload_is(&frame->raw, first, 2)); CHECK(frame->keyframe); media_frame_free(frame); - CHECK(media_group_consumer_read_frame(group, &frame) == NULL); + CHECK(media_group_consumer_method_read_frame(group, &frame) == NULL); CHECK(!frame->keyframe); media_frame_free(frame); /* Each layer's error, both named `GroupError`. */ - media_group_error *error = media_group_consumer_read_frame(group, &frame); + media_group_error *error = media_group_consumer_method_read_frame(group, &frame); CHECK(error != NULL); CHECK(frame == NULL); CHECK(error->tag == MEDIA_GROUP_ERROR_NET); @@ -85,12 +85,12 @@ static void async_throws_the_net_error(void) { media_group_consumer *group = media_group_consumer_new(inner); net_group_consumer_free(inner); - media_priority *priority = media_group_consumer_priority(group); + media_priority *priority = media_group_consumer_method_priority(group); CHECK(priority->tag == MEDIA_PRIORITY_REALTIME); media_priority_free(priority); next_result r = {test_latch_new(), NULL, NULL}; - media_task *task = media_group_consumer_next_frame(group, on_next, &r); + media_task *task = media_group_consumer_method_next_frame(group, on_next, &r); test_latch_wait(r.done); media_task_free(task); CHECK(r.error == NULL); @@ -98,7 +98,7 @@ static void async_throws_the_net_error(void) { media_frame_free(r.frame); r.frame = NULL; - task = media_group_consumer_next_frame(group, on_next, &r); + task = media_group_consumer_method_next_frame(group, on_next, &r); test_latch_wait(r.done); media_task_free(task); CHECK(r.frame == NULL); @@ -155,8 +155,8 @@ static void containers_of_both(void) { CHECK(groups->groups.len == 2); for (size_t i = 0; i < 2; ++i) { - net_group_consumer *held = media_group_consumer_group(groups->groups.data[i]); - CHECK(net_group_consumer_sequence(held) == i + 1); + net_group_consumer *held = media_group_consumer_method_group(groups->groups.data[i]); + CHECK(net_group_consumer_method_sequence(held) == i + 1); net_group_consumer_free(held); } CHECK(groups->net.len == 2); diff --git a/uniffi/cpp/fixtures/c-api/src/lib.rs b/uniffi/cpp/fixtures/c-api/src/lib.rs index 486857392..4c903c724 100644 --- a/uniffi/cpp/fixtures/c-api/src/lib.rs +++ b/uniffi/cpp/fixtures/c-api/src/lib.rs @@ -76,6 +76,13 @@ pub struct CapiItem { pub maybes: Vec>, } +/// What `CapiStore::stats` returns, whose name the C method must not take. +#[derive(Clone, uniffi::Record)] +pub struct CapiStoreStats { + pub items: u64, + pub queued: u64, +} + #[derive(Debug, thiserror::Error, uniffi::Error)] #[uniffi::export(Display)] pub enum CapiError { @@ -188,6 +195,13 @@ impl CapiStore { self.items.lock().unwrap().get(index as usize)?.limit } + pub fn stats(&self) -> CapiStoreStats { + CapiStoreStats { + items: self.len(), + queued: self.queue.lock().unwrap().values.len() as u64, + } + } + pub fn names(&self) -> Vec { self.items .lock() From 77b35a81b827d8805cc22cc370a3bc9ac169bf65 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 25 Oct 2026 16:54:50 -0700 Subject: [PATCH 2/3] feat(uniffi): name generated C types in CamelCase, GLib style Replaces the _method_ namespace: types are CamelCase (MoqAudioEncoderFinish, MoqTask, MoqAudioEncoderFinishCallback) and functions stay snake_case (moq_audio_encoder_finish), so a type and a function never share a name. Co-Authored-By: Claude Opus 5.5 --- uniffi/MIGRATE.md | 8 +- uniffi/cpp/README.md | 26 ++-- uniffi/cpp/bindgen/src/bindings/c/gen_c.rs | 136 ++++++++++++----- .../src/bindings/c/templates/c_header.h | 16 +- .../src/bindings/c/templates/c_source.cpp | 20 +-- uniffi/cpp/cpp-tests/tests/c_api/main.c | 144 +++++++++--------- .../cpp/cpp-tests/tests/c_dispatcher/main.c | 28 ++-- .../cpp/cpp-tests/tests/c_namespaces/main.c | 80 +++++----- uniffi/cpp/fixtures/c-api/src/lib.rs | 2 +- 9 files changed, 258 insertions(+), 202 deletions(-) diff --git a/uniffi/MIGRATE.md b/uniffi/MIGRATE.md index 91391e449..75a94cae5 100644 --- a/uniffi/MIGRATE.md +++ b/uniffi/MIGRATE.md @@ -14,7 +14,7 @@ style, where an async call returns `uniffi::Future`. - Generated C: every type and function name starts with the crate's namespace, so a crate whose items did not already start with it gets renamed C symbols. -- Generated C: methods and named constructors gain `_method_` after their type's name - (`app_my_object_do_thing` is now `app_my_object_method_do_thing`), and so do their async - callback typedefs. The primary constructor `_new`, `_free`, and top-level functions keep their - names. +- Generated C: type names are CamelCase, as in GLib: `app_my_object *` is now `AppMyObject *`, + `app_task` is `AppTask`, enum tags are `Tag`, variant structs ``, and + async callback types `Callback` (`app_my_object_do_thing_callback` is now + `AppMyObjectDoThingCallback`). Functions and enum constants keep their names. diff --git a/uniffi/cpp/README.md b/uniffi/cpp/README.md index 32794079b..722c0dcb1 100644 --- a/uniffi/cpp/README.md +++ b/uniffi/cpp/README.md @@ -54,30 +54,32 @@ and RTTI may be off) and include only `.h` from C. uniffi-bindgen-cpp --lang c --library your_rust_library.so --out-dir output_directory ``` -Names are the uniffi names in snake case, prefixed with the namespace unless they already start -with it, since every crate's C API shares one scope: in namespace `app`, `MyObject` is -`app_my_object *`, created with `app_my_object_new` and released with `app_my_object_free`, and -`AppObject` is `app_object`. A method or named constructor goes under `_method_`, so -`MyObject.do_thing` is `app_my_object_method_do_thing` and never meets a type named -`MyObjectDoThing`. Two items that still render one name, such as `AppObject` and `Object`, are -refused with both named. The rest follows from a few rules: +Names follow GLib: functions are snake_case and types CamelCase, both starting with the +namespace unless the uniffi name already does, since every crate's C API shares one scope. In +namespace `app`, `MyObject` is `AppMyObject *`, created with `app_my_object_new` and released +with `app_my_object_free`, and its method `do_thing` is `app_my_object_do_thing`, which a record +`MyObjectDoThing` (`AppMyObjectDoThing`) never meets. `AppObject` stays `AppObject`, and types are +re-cased from snake case, so `HTTPServer` is `AppHttpServer`. Two items that still render one +name, such as `AppObject` and `Object`, or a method `free` and the release function, are refused +with both named. The rest follows from a few rules: * A call that can fail returns an error struct, or NULL on success, and writes its result to a trailing `out` pointer. An error is a tagged union with a `message`; free it with `_free`. * Records are plain structs, data-carrying enums are a `tag` plus an anonymous union of - `_` structs (`shape->circle.radius`), and flat enums are C enums. A record + `` structs (`shape->circle.radius`), and flat enums are C enums. A record with `#[uniffi(default)]` fields gets `_default()`. * Arguments are borrowed for the call. Everything returned is owned by the caller and freed with the matching `_free` (`_string_free` for strings). * Optional strings, objects, and structs are pointers that are NULL when absent; optional scalars are `{ has_value, value }` structs. Bytes, lists, and maps are pointer and length structs. -* An async call takes a callback and `user_data` and returns a `_task *`. The callback - runs once on the dispatcher with the result, which it owns, unless the dispatcher refuses the - work or has shut down, which abandons the call. Free every task: freeing a pending +* An async call takes a callback, typed as the function's name in CamelCase plus `Callback` + (`AppMyObjectDoThingCallback`), and `user_data`, and returns a `Task *`. The + callback runs once on the dispatcher with the result, which it owns, unless the dispatcher + refuses the work or has shut down, which abandons the call. Free every task: freeing a pending one cancels the Rust future, and once `_task_free` returns the callback has finished or will never run. * Callbacks run on the default dispatcher thread unless `_set_dispatcher` installs a - host's own, which receives `_work *` items to run with `_work_run`. + host's own, which receives `Work *` items to run with `_work_run`. * A type from another crate keeps that crate's C name and `_free`, and its header is included. Generate the C API of every crate involved (one `--crate` each, or all at once) and compile each one's `.cpp` and `_c.cpp`; a crate's implementation converts its diff --git a/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs b/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs index ac9535448..1211ae210 100644 --- a/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs +++ b/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs @@ -11,7 +11,7 @@ use std::fmt::Write; use anyhow::{bail, Context, Result}; use askama::Template; -use heck::{ToShoutySnakeCase, ToSnakeCase}; +use heck::{ToShoutySnakeCase, ToSnakeCase, ToUpperCamelCase}; use uniffi_bindgen::{ interface::{ AsType, Callable, DefaultValue, Enum, Field, Literal, Object, Radix, Record, Type, @@ -33,6 +33,8 @@ pub(crate) struct Bindings { #[template(syntax = "cpp", escape = "none", path = "c_header.h")] struct Header<'a> { ns: &'a str, + /// The namespace in CamelCase, which starts the runtime's type names. + ty: &'a str, guard: String, /// The namespaces whose headers declare the external types this one uses. includes: Vec, @@ -43,6 +45,7 @@ struct Header<'a> { #[template(syntax = "cpp", escape = "none", path = "c_source.cpp")] struct Source<'a> { ns: &'a str, + ty: &'a str, namespace: &'a str, objects: &'a str, externals: &'a str, @@ -66,7 +69,7 @@ pub(crate) fn generate(ci: &ComponentInterface, config: &Config) -> Result Result Result CType { + let mut ct = self.clone(); + ct.kind = match ct.kind { + Kind::Optional(inner) => Kind::Optional(Box::new(inner.qualified())), + Kind::OptionalValue(inner) => Kind::OptionalValue(Box::new(inner.qualified())), + Kind::List(inner) => Kind::List(Box::new(inner.qualified())), + Kind::Map(key, value) => { + Kind::Map(Box::new(key.qualified()), Box::new(value.qualified())) + } + kind => kind, + }; + if !ct.snake.is_empty() { + ct.c = format!("::{}", ct.c); + } + ct + } + /// The uniffi item this type renders, for errors. fn item(&self) -> String { match &self.ty { @@ -228,7 +255,7 @@ impl CType { Kind::Scalar | Kind::FlatEnum | Kind::OptionalValue(_) => None, Kind::String => Some(format!("{ns}_string_free")), Kind::Optional(inner) => inner.free_fn(ns), - _ => Some(format!("{}_free", self.c)), + _ => Some(format!("{}_free", self.snake)), } } @@ -262,8 +289,9 @@ fn c_name(name: &str) -> String { name.to_snake_case() } -/// The C name of a type or function: its name in snake case, prefixed with the snake-case -/// namespace unless it already is, since every crate's C API shares one global scope. +/// The snake-case C name of a function, or the base of a type's: its name in snake case, +/// prefixed with the snake-case namespace unless it already is, since every crate's C API shares +/// one global scope. fn prefixed(ns: &str, name: &str) -> String { let c = c_name(name); if c.starts_with(&format!("{ns}_")) { @@ -273,6 +301,11 @@ fn prefixed(ns: &str, name: &str) -> String { } } +/// The C name of a type, from its snake-case name: CamelCase, so it never meets a function's. +fn type_name(snake: &str) -> String { + snake.to_upper_camel_case() +} + fn c_field_name(field: &Field, index: usize) -> String { if field.name().is_empty() { format!("v{index}") @@ -430,9 +463,12 @@ impl<'a> Gen<'a> { } let ns = self.ns.clone(); let named = |name: &str| { - let c = prefixed(&ns, name); - let slug = c.strip_prefix(&format!("{ns}_")).unwrap_or(&c).to_string(); - (c, slug) + let snake = prefixed(&ns, name); + let slug = snake + .strip_prefix(&format!("{ns}_")) + .unwrap_or(&snake) + .to_string(); + (snake, slug) }; let scalar = |c: &str, slug: &str| (Kind::Scalar, c.to_string(), slug.to_string()); @@ -516,10 +552,16 @@ impl<'a> Gen<'a> { } }; + // Scalars and strings keep their C spelling; every other type is named from its snake case. + let (c, snake) = match kind { + Kind::Scalar | Kind::String | Kind::Optional(_) => (c, String::new()), + _ => (type_name(&c), c), + }; let ct = CType { ty: ty.clone(), kind, c, + snake, cpp, canon: canon.clone(), slug, @@ -547,11 +589,12 @@ impl<'a> Gen<'a> { let ct = CType { ty: ty.clone(), kind: own.kind, - c: own.c.clone(), + c: own.c, + snake: own.snake.clone(), cpp, canon: canon.clone(), // The whole C name, so a compound of it never matches one of a local namesake. - slug: own.c, + slug: own.snake, external: Some((ext.namespace().to_string(), own.canon)), }; self.types.insert(canon, ct.clone()); @@ -561,17 +604,20 @@ impl<'a> Gen<'a> { fn render(&mut self, h: &mut String, s: &mut String) -> Result<()> { let ns = self.ns.clone(); for name in [ - "task", "task_free", - "work", "work_run", - "dispatcher", "set_dispatcher", "shutdown_dispatcher", "string_free", ] { self.claim(&format!("{ns}_{name}"), &format!("the runtime's `{name}`"))?; } + for name in ["task", "work", "dispatcher"] { + self.claim( + &type_name(&format!("{ns}_{name}")), + &format!("the runtime's `{name}`"), + )?; + } // Register every reachable type before rendering any of them. let local: Vec = self.ci.iter_local_types().cloned().collect(); @@ -705,7 +751,7 @@ impl<'a> Gen<'a> { h.push_str(&doc(e.docstring(), 0)); writeln!(h, "typedef enum {} {{", ct.c)?; for (i, variant) in e.variants().iter().enumerate() { - let constant = variant_constant(&ct.c, variant.name()); + let constant = variant_constant(&ct.snake, variant.name()); self.claim( &constant, &format!("variant `{}::{}`", e.name(), variant.name()), @@ -813,7 +859,7 @@ impl<'a> Gen<'a> { self.render_free(h, s, ct, "this record's fields and the record")?; if rec.fields().iter().any(|f| f.default_value().is_some()) { - let name = format!("{}_default", ct.c); + let name = format!("{}_default", ct.snake); self.claim(&name, &format!("the defaults of {}", ct.item()))?; h.push_str(&line_doc( "A record with every defaulted field set and the rest zeroed, to fill in before passing it.", @@ -856,7 +902,7 @@ impl<'a> Gen<'a> { | Kind::List(_) | Kind::Map(..) => None, Kind::Record if self.has_default(&ct.ty)? => { - Some(format!("{}_default()", ct.c)) + Some(format!("{}_default()", ct.snake)) } Kind::Record => None, _ => bail!("the C backend cannot render a default `{}`", ct.cpp), @@ -880,7 +926,7 @@ impl<'a> Gen<'a> { Literal::Int(value, ..) => Some(format!("{value}LL")), Literal::Float(value, _) => Some(value.clone()), Literal::Enum(variant, _) if matches!(ct.kind, Kind::FlatEnum) => { - Some(variant_constant(&ct.c, variant)) + Some(variant_constant(&ct.snake, variant)) } _ => bail!( "the C backend cannot render the default {literal:?} for `{}`", @@ -900,13 +946,13 @@ impl<'a> Gen<'a> { fn render_data_enum(&mut self, h: &mut String, s: &mut String, ct: &CType) -> Result<()> { let e = self.enum_def(&ct.ty)?; let error = self.ci.is_used_as_error(e); - let tag = format!("{}_tag", ct.c); + let tag = format!("{}Tag", ct.c); self.claim(&tag, &format!("the tag of {}", ct.item()))?; h.push_str(&line_doc(&format!("Which variant a `{}` holds.", ct.c))); writeln!(h, "typedef enum {tag} {{")?; for (i, variant) in e.variants().iter().enumerate() { - let constant = variant_constant(&ct.c, variant.name()); + let constant = variant_constant(&ct.snake, variant.name()); self.claim( &constant, &format!("variant `{}::{}`", e.name(), variant.name()), @@ -930,7 +976,7 @@ impl<'a> Gen<'a> { variant.name() ); } - let fields = format!("{}_{member}", ct.c); + let fields = type_name(&format!("{}_{member}", ct.snake)); self.claim( &fields, &format!("the fields of variant `{}::{}`", e.name(), variant.name()), @@ -938,7 +984,7 @@ impl<'a> Gen<'a> { h.push_str(&line_doc(&format!( "The fields of a `{}` holding `{}`.", ct.c, - variant_constant(&ct.c, variant.name()) + variant_constant(&ct.snake, variant.name()) ))); writeln!(h, "typedef struct {fields} {{")?; for (i, field) in variant.fields().iter().enumerate() { @@ -976,7 +1022,7 @@ impl<'a> Gen<'a> { if error { self.render_free(h, s, ct, "this error")?; - let name = format!("{}_message", ct.c); + let name = format!("{}_message", ct.snake); self.claim(&name, &format!("the message of {}", ct.item()))?; h.push_str(&line_doc("The error's message, owned by the error.")); writeln!(h, "const char *{name}(const {} *error);\n", ct.c)?; @@ -1000,7 +1046,7 @@ impl<'a> Gen<'a> { ct: &CType, what: &str, ) -> Result<()> { - let name = format!("{}_free", ct.c); + let name = format!("{}_free", ct.snake); self.claim(&name, &format!("the release function of {}", ct.item()))?; h.push_str(&line_doc(&format!( "Frees {what}, as returned by this API; NULL is ignored." @@ -1017,7 +1063,8 @@ impl<'a> Gen<'a> { } fn render_object(&mut self, h: &mut String, s: &mut String, obj: &Object) -> Result<()> { - let c = prefixed(&self.ns, obj.name()); + let snake = prefixed(&self.ns, obj.name()); + let c = type_name(&snake); let class = CppCodeOracle.class_name(obj.name()); let namespace = self.ci.namespace(); @@ -1027,7 +1074,7 @@ impl<'a> Gen<'a> { writeln!(h, "{}", doc(Some(docstring), 0))?; } - let free = format!("{c}_free"); + let free = format!("{snake}_free"); self.claim( &free, &format!("the release function of object `{}`", obj.name()), @@ -1038,15 +1085,16 @@ impl<'a> Gen<'a> { writeln!(h, "void {free}({c} *self);\n")?; writeln!(s, "void {free}({c} *self) {{\n delete self;\n}}\n")?; - // `_method_` keeps a function's name from spelling another type's (`AudioEncoder::finish` - // against `AudioEncoderFinish`). for ctor in obj.constructors() { let item = format!("constructor `{}::{}`", obj.name(), ctor.name()); let (name, call) = if ctor.is_primary_constructor() { - (format!("{c}_new"), format!("::{namespace}::{class}::init")) + ( + format!("{snake}_new"), + format!("::{namespace}::{class}::init"), + ) } else { ( - format!("{c}_method_{}", c_name(ctor.name())), + format!("{snake}_{}", c_name(ctor.name())), format!("::{namespace}::{class}::{}", ctor.name()), ) }; @@ -1056,7 +1104,7 @@ impl<'a> Gen<'a> { for method in obj.methods() { let item = format!("method `{}::{}`", obj.name(), method.name()); - let name = format!("{c}_method_{}", c_name(method.name())); + let name = format!("{snake}_{}", c_name(method.name())); let call = format!("self->inner->{}", filters::fn_name(method.name()).unwrap()); self.claim(&name, &item)?; self.render_callable(h, s, method, &name, Some(&c), &call)?; @@ -1165,7 +1213,7 @@ impl<'a> Gen<'a> { ); } } - let callback = format!("{name}_callback"); + let callback = type_name(&format!("{name}_callback")); self.claim(&callback, &format!("the callback of `{name}`"))?; let mut cb_params = vec!["void *user_data".to_string()]; @@ -1221,7 +1269,11 @@ impl<'a> Gen<'a> { } }; - let signature = format!("{ns}_task *{name}({})", params.join(", ")); + let signature = format!( + "{} *{name}({})", + type_name(&format!("{ns}_task")), + params.join(", ") + ); notes.push( "Calls `callback` once on the dispatcher, unless the returned task is freed first." .into(), @@ -1302,8 +1354,9 @@ impl<'a> Gen<'a> { /// Declares every converter, then defines them, so their order does not matter. An external /// type's converters forward to its own crate's, which `externals` declares. fn render_converters(&self, out: &mut String, externals: &mut String) -> Result<()> { + let types: Vec = self.types.values().map(CType::qualified).collect(); let mut by_namespace: BTreeMap<&str, String> = BTreeMap::new(); - for ct in self.types.values() { + for ct in &types { if let Some((namespace, canon)) = &ct.external { let decls = by_namespace.entry(namespace).or_default(); let field = ct.field(); @@ -1334,7 +1387,7 @@ impl<'a> Gen<'a> { )?; } - for ct in self.types.values() { + for ct in &types { let field = ct.field(); writeln!( out, @@ -1359,7 +1412,7 @@ impl<'a> Gen<'a> { )?; } out.push('\n'); - for ct in self.types.values() { + for ct in &types { self.render_converter(out, ct)?; } Ok(()) @@ -1408,7 +1461,7 @@ impl<'a> Gen<'a> { ct.cpp, CppCodeOracle.enum_variant_name(variant.name(), self.config.enum_style()) ); - let constant = variant_constant(&ct.c, variant.name()); + let constant = variant_constant(&ct.snake, variant.name()); writeln!( to_cases, " case {cpp_variant}:\n return {constant};" @@ -1615,7 +1668,7 @@ impl<'a> Gen<'a> { let mut from_cases = String::new(); let mut free_cases = String::new(); for (index, variant) in e.variants().iter().enumerate() { - let constant = variant_constant(&ct.c, variant.name()); + let constant = variant_constant(&ct.snake, variant.name()); let cpp_variant = format!( "{}::{}", ct.cpp, @@ -1721,11 +1774,12 @@ fn pointer(ty: &str) -> String { } } -/// The constant naming one variant of a C enum, like `MOQ_TRANSPORT_QUIC`. -fn variant_constant(c: &str, variant: &str) -> String { +/// The constant naming one variant of a C enum, from the enum's snake-case name, like +/// `MOQ_TRANSPORT_QUIC`. +fn variant_constant(snake: &str, variant: &str) -> String { format!( "{}_{}", - c.to_shouty_snake_case(), + snake.to_shouty_snake_case(), variant.to_shouty_snake_case() ) } diff --git a/uniffi/cpp/bindgen/src/bindings/c/templates/c_header.h b/uniffi/cpp/bindgen/src/bindings/c/templates/c_header.h index e0bb7e1f5..215aa8b4e 100644 --- a/uniffi/cpp/bindgen/src/bindings/c/templates/c_header.h +++ b/uniffi/cpp/bindgen/src/bindings/c/templates/c_header.h @@ -27,35 +27,35 @@ extern "C" { * safe to do so from inside its callback. A dispatcher that refuses work or has shut down * abandons the call, and its callback never runs. */ -typedef struct {{ ns }}_task {{ ns }}_task; +typedef struct {{ ty }}Task {{ ty }}Task; /** Cancels the call if it is still pending and releases the task; NULL is ignored. */ -void {{ ns }}_task_free({{ ns }}_task *task); +void {{ ns }}_task_free({{ ty }}Task *task); /** One unit of async work handed to a custom dispatcher. */ -typedef struct {{ ns }}_work {{ ns }}_work; +typedef struct {{ ty }}Work {{ ty }}Work; /** Runs the work and releases it. Call it exactly once for every work item accepted. */ -void {{ ns }}_work_run({{ ns }}_work *work); +void {{ ns }}_work_run({{ ty }}Work *work); /** * Runs async work, and so every callback, somewhere the host chooses, such as its own loop. * Both functions may be called from any thread. */ -typedef struct {{ ns }}_dispatcher { +typedef struct {{ ty }}Dispatcher { /** Queues the work to run later with `{{ ns }}_work_run` and returns true, or returns false to refuse it. */ - bool (*dispatch)(void *user_data, {{ ns }}_work *work); + bool (*dispatch)(void *user_data, {{ ty }}Work *work); /** Stops accepting work, runs what was accepted, and returns once none can still run. */ void (*shutdown)(void *user_data); /** Passed to both functions. */ void *user_data; -} {{ ns }}_dispatcher; +} {{ ty }}Dispatcher; /** * Replaces the default dispatcher thread, before the first async call. The struct is copied. * Aborts when called after an async call or a shutdown, or with a NULL function. */ -void {{ ns }}_set_dispatcher(const {{ ns }}_dispatcher *dispatcher); +void {{ ns }}_set_dispatcher(const {{ ty }}Dispatcher *dispatcher); /** Stops dispatching for good: pending calls never call back. Call it before unloading. */ void {{ ns }}_shutdown_dispatcher(void); diff --git a/uniffi/cpp/bindgen/src/bindings/c/templates/c_source.cpp b/uniffi/cpp/bindgen/src/bindings/c/templates/c_source.cpp index 1023f6847..56eafe13f 100644 --- a/uniffi/cpp/bindgen/src/bindings/c/templates/c_source.cpp +++ b/uniffi/cpp/bindgen/src/bindings/c/templates/c_source.cpp @@ -86,13 +86,13 @@ class TaskState { } // namespace {{ namespace }}::c_detail -struct {{ ns }}_task { +struct {{ ty }}Task { std::shared_ptr<::{{ namespace }}::c_detail::TaskState> state; // Destroying it cancels the Rust future, which is a no-op once it completed. ::uniffi::FutureContinuation continuation; }; -struct {{ ns }}_work { +struct {{ ty }}Work { ::uniffi::AsyncTask task; }; @@ -107,7 +107,7 @@ namespace {{ namespace }}::c_detail { // Delivers a future's output to `deliver` on the dispatcher, never on the calling thread, // so a callback cannot run before the call that started it returns its task. template -::{{ ns }}_task *spawn(Future future, Deliver deliver) { +::{{ ty }}Task *spawn(Future future, Deliver deliver) { auto state = std::make_shared(); auto executor = [](::uniffi::AsyncTask task) { return ::uniffi::detail::dispatch_async(std::move(task)); @@ -117,12 +117,12 @@ template auto continuation = std::move(future).then(executor, [state, deliver]() { state->run(deliver); }); - return new ::{{ ns }}_task{std::move(state), std::move(continuation)}; + return new ::{{ ty }}Task{std::move(state), std::move(continuation)}; } else { auto continuation = std::move(future).then(executor, [state, deliver](Output output) { state->run([&] { deliver(std::move(output)); }); }); - return new ::{{ ns }}_task{std::move(state), std::move(continuation)}; + return new ::{{ ty }}Task{std::move(state), std::move(continuation)}; } } @@ -130,7 +130,7 @@ template } // namespace {{ namespace }}::c_detail -void {{ ns }}_task_free({{ ns }}_task *task) { +void {{ ns }}_task_free({{ ty }}Task *task) { if (!task) { return; } @@ -138,7 +138,7 @@ void {{ ns }}_task_free({{ ns }}_task *task) { delete task; } -void {{ ns }}_work_run({{ ns }}_work *work) { +void {{ ns }}_work_run({{ ty }}Work *work) { if (!work) { ::{{ namespace }}::c_detail::fail("{{ ns }}_work_run: work must not be NULL"); } @@ -147,14 +147,14 @@ void {{ ns }}_work_run({{ ns }}_work *work) { task(); } -void {{ ns }}_set_dispatcher(const {{ ns }}_dispatcher *dispatcher) { +void {{ ns }}_set_dispatcher(const {{ ty }}Dispatcher *dispatcher) { if (!dispatcher || !dispatcher->dispatch || !dispatcher->shutdown) { ::{{ namespace }}::c_detail::fail("{{ ns }}_set_dispatcher: dispatch and shutdown must not be NULL"); } - const {{ ns }}_dispatcher copy = *dispatcher; + const {{ ty }}Dispatcher copy = *dispatcher; ::uniffi::set_async_dispatcher( [copy](::uniffi::AsyncTask task) { - auto *work = new {{ ns }}_work{std::move(task)}; + auto *work = new {{ ty }}Work{std::move(task)}; if (copy.dispatch(copy.user_data, work)) { return true; } diff --git a/uniffi/cpp/cpp-tests/tests/c_api/main.c b/uniffi/cpp/cpp-tests/tests/c_api/main.c index cf7927c7b..a0ae7af93 100644 --- a/uniffi/cpp/cpp-tests/tests/c_api/main.c +++ b/uniffi/cpp/cpp-tests/tests/c_api/main.c @@ -10,7 +10,7 @@ static int streq(const char *a, const char *b) { } /* The value a map holds for `key`, or NULL. */ -static const char *lookup(const capi_string_string_map *map, const char *key) { +static const char *lookup(const CapiStringStringMap *map, const char *key) { size_t i; for (i = 0; i < map->len; i++) { if (streq(map->keys[i], key)) { @@ -21,7 +21,7 @@ static const char *lookup(const capi_string_string_map *map, const char *key) { } static void defaults(void) { - capi_item item = capi_item_default(); + CapiItem item = capi_item_default(); CHECK(item.name == NULL); CHECK(item.count == 7); CHECK(streq(item.title, "untitled \"draft\"")); @@ -45,16 +45,16 @@ static void records(void) { const char *label_keys[] = {"k1", "k2"}; const char *label_values[] = {"v1", "v2"}; const char *point_keys[] = {"p"}; - const capi_point point_values[] = {{5, 6}}; + const CapiPoint point_values[] = {{5, 6}}; const uint64_t values[] = {10, 20, 30}; - capi_optional_u32 maybes[2]; - capi_point path[2]; - capi_point target; - capi_bytes extra; - capi_store *owner = capi_store_new("owner"); - capi_item item = capi_item_default(); - capi_item *echo; - capi_string_string_map labels; + CapiOptionalU32 maybes[2]; + CapiPoint path[2]; + CapiPoint target; + CapiBytes extra; + CapiStore *owner = capi_store_new("owner"); + CapiItem item = capi_item_default(); + CapiItem *echo; + CapiStringStringMap labels; char *owner_name; maybes[0].has_value = true; @@ -133,7 +133,7 @@ static void records(void) { /* The echoed record owns a new handle to the same store. */ CHECK(echo->owner != NULL && echo->owner != owner); - owner_name = capi_store_method_name(echo->owner); + owner_name = capi_store_name(echo->owner); CHECK(streq(owner_name, "owner")); capi_string_free(owner_name); @@ -155,7 +155,7 @@ static void records(void) { } static void enums(void) { - capi_shape *shape = capi_make_shape(0); + CapiShape *shape = capi_make_shape(0); CHECK(shape->tag == CAPI_SHAPE_DOT); capi_shape_free(shape); @@ -175,10 +175,10 @@ static void enums(void) { static void scalars(void) { static const uint8_t data[] = {1, 2, 3}; - capi_bytes payload; - capi_optional_u64 value; - capi_optional_color color; - capi_point point; + CapiBytes payload; + CapiOptionalU64 value; + CapiOptionalColor color; + CapiPoint point; char *text; payload.data = data; @@ -210,8 +210,8 @@ static void scalars(void) { } static void errors(void) { - capi_error *error; - capi_flat_error *flat; + CapiError *error; + CapiFlatError *flat; CHECK(capi_fail(0) == NULL); @@ -247,27 +247,27 @@ static void errors(void) { } static void objects(void) { - capi_store *store = NULL; - capi_store *fork; - capi_store *renamed; - capi_store *filled; - capi_error *error; - capi_item item; - capi_item items[2]; - capi_item_list list; - capi_item *got = NULL; - capi_string_list *names; - capi_store_stats *stats; - capi_optional_u64 limit; + CapiStore *store = NULL; + CapiStore *fork; + CapiStore *renamed; + CapiStore *filled; + CapiError *error; + CapiItem item; + CapiItem items[2]; + CapiItemList list; + CapiItem *got = NULL; + CapiStringList *names; + CapiStoreStats *stats; + CapiOptionalU64 limit; uint32_t index; char *text; - error = capi_store_method_open("", &store); + error = capi_store_open("", &store); CHECK(error && error->tag == CAPI_ERROR_INVALID && store == NULL); capi_error_free(error); - CHECK(capi_store_method_open("main", &store) == NULL && store != NULL); - CHECK(capi_store_method_len(store) == 0); + CHECK(capi_store_open("main", &store) == NULL && store != NULL); + CHECK(capi_store_len(store) == 0); item = capi_item_default(); item.name = "one"; @@ -275,67 +275,67 @@ static void objects(void) { item.limit.has_value = true; item.limit.value = 5; index = 99; - CHECK(capi_store_method_add(store, &item, &index) == NULL && index == 0); + CHECK(capi_store_add(store, &item, &index) == NULL && index == 0); /* A failed call clears its out-param. */ item.name = ""; index = 99; - error = capi_store_method_add(store, &item, &index); + error = capi_store_add(store, &item, &index); CHECK(error && error->tag == CAPI_ERROR_INVALID && index == 0); capi_error_free(error); item.name = "two"; item.note = NULL; item.limit.has_value = false; - CHECK(capi_store_method_add(store, &item, &index) == NULL && index == 1); - CHECK(capi_store_method_len(store) == 2); + CHECK(capi_store_add(store, &item, &index) == NULL && index == 1); + CHECK(capi_store_len(store) == 2); - CHECK(capi_store_method_get(store, 1, &got) == NULL); + CHECK(capi_store_get(store, 1, &got) == NULL); CHECK(streq(got->name, "two") && got->note == NULL); capi_item_free(got); - error = capi_store_method_get(store, 5, &got); + error = capi_store_get(store, 5, &got); CHECK(error && error->tag == CAPI_ERROR_CODE && error->code.code == 5 && got == NULL); capi_error_free(error); - got = capi_store_method_find(store, "one"); + got = capi_store_find(store, "one"); CHECK(got && got->count == 7); capi_item_free(got); - CHECK(capi_store_method_find(store, "nope") == NULL); + CHECK(capi_store_find(store, "nope") == NULL); - text = capi_store_method_note(store, 0); + text = capi_store_note(store, 0); CHECK(streq(text, "n1")); capi_string_free(text); - CHECK(capi_store_method_note(store, 1) == NULL); + CHECK(capi_store_note(store, 1) == NULL); - limit = capi_store_method_limit(store, 0); + limit = capi_store_limit(store, 0); CHECK(limit.has_value && limit.value == 5); - limit = capi_store_method_limit(store, 1); + limit = capi_store_limit(store, 1); CHECK(!limit.has_value); - names = capi_store_method_names(store); + names = capi_store_names(store); CHECK(names->len == 2 && streq(names->data[0], "one") && streq(names->data[1], "two")); capi_string_list_free(names); - fork = capi_store_method_fork(store, NULL); - text = capi_store_method_name(fork); + fork = capi_store_fork(store, NULL); + text = capi_store_name(fork); CHECK(streq(text, "main")); capi_string_free(text); - renamed = capi_store_method_fork(store, "other"); - text = capi_store_method_name(renamed); + renamed = capi_store_fork(store, "other"); + text = capi_store_name(renamed); CHECK(streq(text, "other")); capi_string_free(text); - CHECK(capi_store_method_total(store, fork, NULL) == 4); - CHECK(capi_store_method_total(store, fork, renamed) == 6); + CHECK(capi_store_total(store, fork, NULL) == 4); + CHECK(capi_store_total(store, fork, renamed) == 6); - /* The method and the record it returns share a snake-case name. */ - stats = capi_store_method_stats(store); + /* The method and the record it returns differ only in case. */ + stats = capi_store_stats(store); CHECK(stats->items == 2 && stats->queued == 0); capi_store_stats_free(stats); - capi_store_method_clear(store); - CHECK(capi_store_method_len(store) == 0 && capi_store_method_len(fork) == 2); + capi_store_clear(store); + CHECK(capi_store_len(store) == 0 && capi_store_len(fork) == 2); items[0] = capi_item_default(); items[0].name = "x"; @@ -343,8 +343,8 @@ static void objects(void) { items[1].name = "y"; list.data = items; list.len = 2; - filled = capi_store_method_with_items("filled", &list); - CHECK(capi_store_method_len(filled) == 2); + filled = capi_store_with_items("filled", &list); + CHECK(capi_store_len(filled) == 2); capi_store_free(filled); capi_store_free(renamed); @@ -355,7 +355,7 @@ static void objects(void) { typedef struct { test_latch *done; - capi_error *error; + CapiError *error; uint64_t value; char *text; /* For the blocking-free test. */ @@ -370,21 +370,21 @@ static void on_sum(void *user_data, uint64_t value) { test_latch_signal(r->done); } -static void on_greet(void *user_data, capi_error *error, char *value) { +static void on_greet(void *user_data, CapiError *error, char *value) { result *r = (result *)user_data; r->error = error; r->text = value; test_latch_signal(r->done); } -static void on_next(void *user_data, capi_error *error, uint64_t value) { +static void on_next(void *user_data, CapiError *error, uint64_t value) { result *r = (result *)user_data; r->error = error; r->value = value; test_latch_signal(r->done); } -static void on_next_slow(void *user_data, capi_error *error, uint64_t value) { +static void on_next_slow(void *user_data, CapiError *error, uint64_t value) { result *r = (result *)user_data; (void)error; r->value = value; @@ -395,9 +395,9 @@ static void on_next_slow(void *user_data, capi_error *error, uint64_t value) { static void async_calls(void) { const uint64_t values[] = {10, 20, 30}; - capi_u64_list list; - capi_store *store = capi_store_new("async"); - capi_task *task; + CapiU64List list; + CapiStore *store = capi_store_new("async"); + CapiTask *task; result r; memset(&r, 0, sizeof r); @@ -424,8 +424,8 @@ static void async_calls(void) { capi_task_free(task); /* Pending until another call wakes it. */ - task = capi_store_method_next(store, on_next, &r); - capi_store_method_push(store, 42); + task = capi_store_next(store, on_next, &r); + capi_store_push(store, 42); test_latch_wait(r.done); CHECK(r.error == NULL && r.value == 42); capi_task_free(task); @@ -433,8 +433,8 @@ static void async_calls(void) { /* Freeing a task whose callback is running elsewhere waits for the callback. */ r.entered = test_latch_new(); r.release = test_latch_new(); - task = capi_store_method_next(store, on_next_slow, &r); - capi_store_method_push(store, 7); + task = capi_store_next(store, on_next_slow, &r); + capi_store_push(store, 7); test_latch_wait(r.entered); test_latch_signal_from_thread(r.release); capi_task_free(task); @@ -442,8 +442,8 @@ static void async_calls(void) { test_latch_free(r.entered); test_latch_free(r.release); - capi_store_method_close(store); - task = capi_store_method_next(store, on_next, &r); + capi_store_close(store); + task = capi_store_next(store, on_next, &r); test_latch_wait(r.done); CHECK(r.error && r.error->tag == CAPI_ERROR_NOT_FOUND); capi_error_free(r.error); diff --git a/uniffi/cpp/cpp-tests/tests/c_dispatcher/main.c b/uniffi/cpp/cpp-tests/tests/c_dispatcher/main.c index 73f6fa57b..d491d876d 100644 --- a/uniffi/cpp/cpp-tests/tests/c_dispatcher/main.c +++ b/uniffi/cpp/cpp-tests/tests/c_dispatcher/main.c @@ -11,12 +11,12 @@ #define QUEUE_CAPACITY 64 -static capi_work *queue[QUEUE_CAPACITY]; +static CapiWork *queue[QUEUE_CAPACITY]; static size_t queued; static bool accepting = true; static int shutdowns; -static bool dispatch(void *user_data, capi_work *work) { +static bool dispatch(void *user_data, CapiWork *work) { CHECK(user_data == &queued); if (!accepting) { return false; @@ -28,7 +28,7 @@ static bool dispatch(void *user_data, capi_work *work) { /* Runs the oldest queued work. */ static void run_one(void) { - capi_work *work; + CapiWork *work; CHECK(queued > 0); work = queue[0]; queued--; @@ -56,8 +56,8 @@ static void stop(void *user_data) { typedef struct { int calls; uint64_t value; - capi_error *error; - capi_task *task; + CapiError *error; + CapiTask *task; } result; static void on_sum(void *user_data, uint64_t value) { @@ -66,7 +66,7 @@ static void on_sum(void *user_data, uint64_t value) { r->value = value; } -static void on_next(void *user_data, capi_error *error, uint64_t value) { +static void on_next(void *user_data, CapiError *error, uint64_t value) { result *r = (result *)user_data; r->calls++; r->error = error; @@ -89,9 +89,9 @@ static void on_sum_free(void *user_data, uint64_t value) { int main(void) { const uint64_t values[] = {1, 2, 3}; - capi_u64_list list; - capi_dispatcher dispatcher; - capi_store *store; + CapiU64List list; + CapiDispatcher dispatcher; + CapiStore *store; result r; dispatcher.dispatch = dispatch; @@ -113,10 +113,10 @@ int main(void) { /* A pending future calls back once woken and drained. */ memset(&r, 0, sizeof r); - r.task = capi_store_method_next(store, on_next, &r); + r.task = capi_store_next(store, on_next, &r); drain(); CHECK(r.calls == 0); - capi_store_method_push(store, 11); + capi_store_push(store, 11); CHECK(r.calls == 0); drain(); CHECK(r.calls == 1 && r.error == NULL && r.value == 11); @@ -124,7 +124,7 @@ int main(void) { /* Freeing a pending task drops the Rust future, and its callback never runs. */ memset(&r, 0, sizeof r); - r.task = capi_store_method_wait_forever(store, on_wait, &r); + r.task = capi_store_wait_forever(store, on_wait, &r); CHECK(capi_pending_count() == 1); capi_task_free(r.task); drain(); @@ -165,10 +165,10 @@ int main(void) { /* Shutdown drains through the host, then no call calls back. */ memset(&r, 0, sizeof r); - r.task = capi_store_method_next(store, on_next, &r); + r.task = capi_store_next(store, on_next, &r); capi_shutdown_dispatcher(); CHECK(shutdowns == 1); - capi_store_method_push(store, 5); + capi_store_push(store, 5); CHECK(queued == 0); CHECK(r.calls == 0); capi_task_free(r.task); diff --git a/uniffi/cpp/cpp-tests/tests/c_namespaces/main.c b/uniffi/cpp/cpp-tests/tests/c_namespaces/main.c index 610de5cca..8b697dc39 100644 --- a/uniffi/cpp/cpp-tests/tests/c_namespaces/main.c +++ b/uniffi/cpp/cpp-tests/tests/c_namespaces/main.c @@ -4,50 +4,50 @@ #include #include -static net_group_consumer *net_group(uint64_t sequence, net_priority priority, const net_frame *frames, size_t len) { - net_frame_list list = {frames, len}; +static NetGroupConsumer *net_group(uint64_t sequence, NetPriority priority, const NetFrame *frames, size_t len) { + NetFrameList list = {frames, len}; return net_group_consumer_new(sequence, priority, &list); } -static net_frame frame_of(const uint8_t *data, size_t len) { - net_frame frame; +static NetFrame frame_of(const uint8_t *data, size_t len) { + NetFrame frame; frame.payload.data = data; frame.payload.len = len; return frame; } -static bool payload_is(const net_frame *frame, const uint8_t *data, size_t len) { +static bool payload_is(const NetFrame *frame, const uint8_t *data, size_t len) { return frame->payload.len == len && (len == 0 || memcmp(frame->payload.data, data, len) == 0); } static void object_wraps_its_namesake(void) { const uint8_t first[] = {1, 'a'}; const uint8_t second[] = {0, 'b'}; - net_frame frames[] = {frame_of(first, 2), frame_of(second, 2)}; - net_group_consumer *inner = net_group(3, NET_PRIORITY_LOW, frames, 2); - media_group_consumer *group = media_group_consumer_new(inner); + NetFrame frames[] = {frame_of(first, 2), frame_of(second, 2)}; + NetGroupConsumer *inner = net_group(3, NET_PRIORITY_LOW, frames, 2); + MediaGroupConsumer *group = media_group_consumer_new(inner); net_group_consumer_free(inner); - net_group_consumer *held = media_group_consumer_method_group(group); - CHECK(net_group_consumer_method_sequence(held) == 3); + NetGroupConsumer *held = media_group_consumer_group(group); + CHECK(net_group_consumer_sequence(held) == 3); net_group_consumer_free(held); - media_priority *priority = media_group_consumer_method_priority(group); + MediaPriority *priority = media_group_consumer_priority(group); CHECK(priority->tag == MEDIA_PRIORITY_NET); CHECK(priority->net.priority == NET_PRIORITY_LOW); media_priority_free(priority); - media_frame *frame = NULL; - CHECK(media_group_consumer_method_read_frame(group, &frame) == NULL); + MediaFrame *frame = NULL; + CHECK(media_group_consumer_read_frame(group, &frame) == NULL); CHECK(payload_is(&frame->raw, first, 2)); CHECK(frame->keyframe); media_frame_free(frame); - CHECK(media_group_consumer_method_read_frame(group, &frame) == NULL); + CHECK(media_group_consumer_read_frame(group, &frame) == NULL); CHECK(!frame->keyframe); media_frame_free(frame); /* Each layer's error, both named `GroupError`. */ - media_group_error *error = media_group_consumer_method_read_frame(group, &frame); + MediaGroupError *error = media_group_consumer_read_frame(group, &frame); CHECK(error != NULL); CHECK(frame == NULL); CHECK(error->tag == MEDIA_GROUP_ERROR_NET); @@ -56,7 +56,7 @@ static void object_wraps_its_namesake(void) { CHECK(media_group_error_message(error) != NULL); media_group_error_free(error); - net_frame empty = frame_of(NULL, 0); + NetFrame empty = frame_of(NULL, 0); error = media_decode(&empty, &frame); CHECK(error != NULL); CHECK(error->tag == MEDIA_GROUP_ERROR_EMPTY); @@ -67,11 +67,11 @@ static void object_wraps_its_namesake(void) { typedef struct { test_latch *done; - net_group_error *error; - media_frame *frame; + NetGroupError *error; + MediaFrame *frame; } next_result; -static void on_next(void *user_data, net_group_error *error, media_frame *value) { +static void on_next(void *user_data, NetGroupError *error, MediaFrame *value) { next_result *r = (next_result *)user_data; r->error = error; r->frame = value; @@ -80,17 +80,17 @@ static void on_next(void *user_data, net_group_error *error, media_frame *value) static void async_throws_the_net_error(void) { const uint8_t only[] = {1}; - net_frame frames[] = {frame_of(only, 1)}; - net_group_consumer *inner = net_group(1, NET_PRIORITY_HIGH, frames, 1); - media_group_consumer *group = media_group_consumer_new(inner); + NetFrame frames[] = {frame_of(only, 1)}; + NetGroupConsumer *inner = net_group(1, NET_PRIORITY_HIGH, frames, 1); + MediaGroupConsumer *group = media_group_consumer_new(inner); net_group_consumer_free(inner); - media_priority *priority = media_group_consumer_method_priority(group); + MediaPriority *priority = media_group_consumer_priority(group); CHECK(priority->tag == MEDIA_PRIORITY_REALTIME); media_priority_free(priority); next_result r = {test_latch_new(), NULL, NULL}; - media_task *task = media_group_consumer_method_next_frame(group, on_next, &r); + MediaTask *task = media_group_consumer_next_frame(group, on_next, &r); test_latch_wait(r.done); media_task_free(task); CHECK(r.error == NULL); @@ -98,7 +98,7 @@ static void async_throws_the_net_error(void) { media_frame_free(r.frame); r.frame = NULL; - task = media_group_consumer_method_next_frame(group, on_next, &r); + task = media_group_consumer_next_frame(group, on_next, &r); test_latch_wait(r.done); media_task_free(task); CHECK(r.frame == NULL); @@ -113,24 +113,24 @@ static void async_throws_the_net_error(void) { static void containers_of_both(void) { const uint8_t z[] = {0, 'z'}; - net_frame raw = frame_of(z, 2); - media_frame *frame = NULL; + NetFrame raw = frame_of(z, 2); + MediaFrame *frame = NULL; CHECK(media_decode(&raw, &frame) == NULL); - net_frame *encoded = media_encode(frame); + NetFrame *encoded = media_encode(frame); CHECK(payload_is(encoded, z, 2)); net_frame_free(encoded); media_frame_free(frame); const uint8_t a[] = {0, 'a'}; const uint8_t b[] = {1, 'b'}; - net_frame raws[] = {frame_of(a, 2), frame_of(NULL, 0), frame_of(b, 2)}; - media_net_frame_list raw_list = {raws, 3}; - media_frame_list *frames = media_decode_all(&raw_list); + NetFrame raws[] = {frame_of(a, 2), frame_of(NULL, 0), frame_of(b, 2)}; + MediaNetFrameList raw_list = {raws, 3}; + MediaFrameList *frames = media_decode_all(&raw_list); CHECK(frames->len == 2); CHECK(!frames->data[0].keyframe); CHECK(frames->data[1].keyframe); - media_bounds *bounds = media_find_bounds(frames); + MediaBounds *bounds = media_find_bounds(frames); CHECK(bounds->keyframe != NULL); CHECK(payload_is(&bounds->keyframe->raw, b, 2)); CHECK(bounds->last != NULL); @@ -138,37 +138,37 @@ static void containers_of_both(void) { media_bounds_free(bounds); media_frame_list_free(frames); - media_frame_list none = {NULL, 0}; + MediaFrameList none = {NULL, 0}; bounds = media_find_bounds(&none); CHECK(bounds->keyframe == NULL); CHECK(bounds->last == NULL); media_bounds_free(bounds); - net_group_consumer *inners[] = { + NetGroupConsumer *inners[] = { net_group(1, NET_PRIORITY_LOW, NULL, 0), net_group(2, NET_PRIORITY_HIGH, NULL, 0), }; - media_net_group_consumer_list inner_list = {inners, 2}; - media_groups *groups = media_wrap_all(&inner_list); + MediaNetGroupConsumerList inner_list = {inners, 2}; + MediaGroups *groups = media_wrap_all(&inner_list); net_group_consumer_free(inners[0]); net_group_consumer_free(inners[1]); CHECK(groups->groups.len == 2); for (size_t i = 0; i < 2; ++i) { - net_group_consumer *held = media_group_consumer_method_group(groups->groups.data[i]); - CHECK(net_group_consumer_method_sequence(held) == i + 1); + NetGroupConsumer *held = media_group_consumer_group(groups->groups.data[i]); + CHECK(net_group_consumer_sequence(held) == i + 1); net_group_consumer_free(held); } CHECK(groups->net.len == 2); CHECK(groups->media.len == 2); for (size_t i = 0; i < 2; ++i) { uint64_t sequence = groups->net.keys[i]; - net_priority net = groups->net.values[i]; + NetPriority net = groups->net.values[i]; CHECK(net == (sequence == 1 ? NET_PRIORITY_LOW : NET_PRIORITY_HIGH)); } for (size_t i = 0; i < 2; ++i) { uint64_t sequence = groups->media.keys[i]; - const media_priority *media = &groups->media.values[i]; + const MediaPriority *media = &groups->media.values[i]; if (sequence == 1) { CHECK(media->tag == MEDIA_PRIORITY_NET); CHECK(media->net.priority == NET_PRIORITY_LOW); diff --git a/uniffi/cpp/fixtures/c-api/src/lib.rs b/uniffi/cpp/fixtures/c-api/src/lib.rs index 4c903c724..29317a35c 100644 --- a/uniffi/cpp/fixtures/c-api/src/lib.rs +++ b/uniffi/cpp/fixtures/c-api/src/lib.rs @@ -76,7 +76,7 @@ pub struct CapiItem { pub maybes: Vec>, } -/// What `CapiStore::stats` returns, whose name the C method must not take. +/// What `CapiStore::stats` returns: the C type and method spell the same words. #[derive(Clone, uniffi::Record)] pub struct CapiStoreStats { pub items: u64, From f5a2b4995afbf093dfeee854bf56bd520e7e27bb Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:37:16 -0700 Subject: [PATCH 3/3] fix(uniffi): name the snake-case error free in C docs Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- uniffi/cpp/bindgen/src/bindings/c/gen_c.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs b/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs index 1211ae210..901839c34 100644 --- a/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs +++ b/uniffi/cpp/bindgen/src/bindings/c/gen_c.rs @@ -1230,7 +1230,7 @@ impl<'a> Gen<'a> { if let Some(err) = &err { cb_doc.push(format!( "`error` is NULL on success, or freed with `{}_free`.", - err.c + err.snake )); } if let Some(free) = &ret_free { @@ -1322,7 +1322,7 @@ impl<'a> Gen<'a> { let out = if ret.is_some() { " and sets `out`" } else { "" }; notes.push(format!( "Returns NULL on success{out}, or an error the caller frees with `{}_free`.", - err.c + err.snake )); } if let Some(free) = &ret_free {