Repository navigation
Strip a type prefix, and always use the bundled tl::expected #4
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,11 @@ | ||
| // Generated by uniffi-bindgen-cpp for `error_style = "expected"`. | ||
| // | ||
| // `uniffi::expected` is `std::expected` when the standard library has it and the bundled | ||
| // `tl::expected` otherwise. Every translation unit that shares these bindings must agree on | ||
| // which one it gets, so build the generated sources with the same standard as their users. | ||
| // `uniffi::expected` is the bundled `tl::expected` at every C++ standard, never `std::expected`, | ||
| // so bindings built at one standard link with code built at another. | ||
| #pragma once | ||
|
|
||
| #if defined(__has_include) | ||
| #if __has_include(<version>) | ||
| #include <version> | ||
| #endif | ||
| #endif | ||
|
|
||
| #if defined(__cpp_lib_expected) && __cpp_lib_expected >= 202211L | ||
| #include <expected> | ||
|
|
||
| namespace uniffi { | ||
| using std::expected; | ||
| using std::unexpected; | ||
| } // namespace uniffi | ||
| #else | ||
| {tl_expected} | ||
| namespace uniffi { | ||
| using tl::expected; | ||
| using tl::unexpected; | ||
| } // namespace uniffi | ||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,16 @@ | ||
| pub(crate) mod gen_cpp; | ||
|
|
||
| use std::{fmt::Debug, fs}; | ||
| use std::{ | ||
| collections::{BTreeSet, HashMap}, | ||
| fmt::Debug, | ||
| fs, | ||
| }; | ||
|
|
||
| use anyhow::Result; | ||
| use camino::Utf8Path; | ||
| use uniffi_bindgen::{ | ||
| interface::Literal, BindingGenerator, Component, ComponentInterface, GenerationSettings, | ||
| interface::{rename, Literal, Type}, | ||
| BindingGenerator, Component, ComponentInterface, GenerationSettings, | ||
| }; | ||
|
|
||
| use self::gen_cpp::{generate_cpp_bindings, Bindings}; | ||
|
|
@@ -62,9 +67,29 @@ impl BindingGenerator for CppBindingGenerator { | |
| fn update_component_configs( | ||
| &self, | ||
| _settings: &GenerationSettings, | ||
| _components: &mut Vec<uniffi_bindgen::Component<Self::Config>>, | ||
| components: &mut Vec<uniffi_bindgen::Component<Self::Config>>, | ||
| ) -> Result<()> { | ||
| return Ok(()); | ||
| // Keyed by crate, so a component that imports a renamed type sees the new name too. | ||
| let mut renames = HashMap::new(); | ||
| for Component { ci, config, .. } in components.iter() { | ||
| if let Some(prefix) = config.strip_type_prefix() { | ||
| renames.insert(ci.crate_name().to_string(), strip_type_prefix(ci, prefix)?); | ||
| } | ||
| } | ||
| if renames.is_empty() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let flat: HashMap<String, String> = renames | ||
| .values() | ||
| .flatten() | ||
| .filter_map(|(old, new)| Some((old.clone(), new.as_str()?.to_string()))) | ||
| .collect(); | ||
|
Comment on lines
+83
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When library mode generates multiple components, flattening the crate-keyed rename tables and copying the result to every component loses the type's module identity. If one generated crate strips Useful? React with 👍 / 👎. |
||
| for component in components.iter_mut() { | ||
| rename(&mut component.ci, &renames); | ||
| component.config.renames = flat.clone(); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn write_bindings( | ||
|
|
@@ -83,6 +108,36 @@ impl BindingGenerator for CppBindingGenerator { | |
| } | ||
| } | ||
|
|
||
| /// Renames each local record, object, enum, and callback interface named `<prefix><Name>` to | ||
| /// `<Name>`, where `<Name>` starts with an uppercase letter. Custom types keep their names, since | ||
| /// `custom_types` is keyed by them. Refuses a rename onto a name that is already a type. | ||
| fn strip_type_prefix(ci: &ComponentInterface, prefix: &str) -> Result<toml::Table> { | ||
| let names: BTreeSet<&str> = ci.iter_local_types().filter_map(Type::name).collect(); | ||
|
|
||
| let mut renames = toml::Table::new(); | ||
| for ty in ci.iter_local_types() { | ||
| let name = match ty { | ||
| Type::Record { name, .. } | ||
| | Type::Object { name, .. } | ||
| | Type::Enum { name, .. } | ||
| | Type::CallbackInterface { name, .. } => name, | ||
| _ => continue, | ||
| }; | ||
| let Some(stripped) = name.strip_prefix(prefix) else { | ||
| continue; | ||
| }; | ||
| if !stripped.starts_with(|c: char| c.is_ascii_uppercase()) { | ||
| continue; | ||
| } | ||
| anyhow::ensure!( | ||
| !names.contains(stripped), | ||
| "strip_type_prefix = \"{prefix}\" renames `{name}` to `{stripped}`, which is already a type" | ||
| ); | ||
| renames.insert(name.clone(), stripped.into()); | ||
| } | ||
| Ok(renames) | ||
| } | ||
|
|
||
| /// Writes `<namespace>.hpp`, `<namespace>.cpp`, and `<namespace>_scaffolding.hpp`, plus | ||
| /// `uniffi_expected.hpp` under the expected error style. | ||
| pub(crate) fn write_cpp_bindings( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| `tl_expected.hpp` is [TartanLlama/expected](https://github.com/TartanLlama/expected) v1.1.0 | ||
| (`include/tl/expected.hpp`), unmodified, under CC0. The `error_style = "expected"` bindings ship | ||
| it as the `expected` type below C++23. | ||
| (`include/tl/expected.hpp`) under CC0. The `error_style = "expected"` bindings ship it as their | ||
| `expected` type at every C++ standard. One change: with exceptions disabled, `value()` on an error | ||
| prints a message and aborts instead of being `__builtin_unreachable()`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [bindings.cpp] | ||
| strip_type_prefix = "Flat" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [bindings.cpp] | ||
| error_style = "expected" | ||
| strip_type_prefix = "Capi" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When stripping a prefix makes a local record share a simple name with an imported record—for example, local
FooCoordinatesbecomesCoordinateswhile one of its fields isext::Coordinates—this name-only, local-first lookup conflates the two. The dependency graph can consequently contain a falseCoordinates -> Coordinatesself-edge and report a cyclic dependency even though the C++ types are valid in separate namespaces. Use the module path as part of topology keys and lookup.Useful? React with 👍 / 👎.