diff --git a/api/sixtyfps-cpp/include/sixtyfps_interpreter.h b/api/sixtyfps-cpp/include/sixtyfps_interpreter.h index 06fdff798..8153d75df 100644 --- a/api/sixtyfps-cpp/include/sixtyfps_interpreter.h +++ b/api/sixtyfps-cpp/include/sixtyfps_interpreter.h @@ -374,22 +374,16 @@ public: bool set_property(std::string_view name, const Value &value) const { - cbindgen_private::Slice name_view { - const_cast(reinterpret_cast(name.data())), - name.size() - }; - return cbindgen_private::sixtyfps_interpreter_component_instance_set_property( - &inner, name_view, &value.inner); + using namespace cbindgen_private; + return sixtyfps_interpreter_component_instance_set_property( + &inner, Slice::from_string(name), &value.inner); } std::optional get_property(std::string_view name) const { - cbindgen_private::Slice name_view { - const_cast(reinterpret_cast(name.data())), - name.size() - }; - cbindgen_private::ValueOpaque out; - if (cbindgen_private::sixtyfps_interpreter_component_instance_get_property( - &inner, name_view, &out)) { + using namespace cbindgen_private; + ValueOpaque out; + if (sixtyfps_interpreter_component_instance_get_property( + &inner, Slice::from_string(name), &out)) { return Value(out); } else { return {}; @@ -398,16 +392,11 @@ public: // FIXME! Slice in public API? Should be std::span (c++20) or we need to improve the Slice API std::optional invoke_callback(std::string_view name, Slice args) const { - cbindgen_private::Slice args_view { - reinterpret_cast(args.ptr), args.len - }; - cbindgen_private::Slice name_view { - const_cast(reinterpret_cast(name.data())), - name.size() - }; - cbindgen_private::ValueOpaque out; - if (cbindgen_private::sixtyfps_interpreter_component_instance_invoke_callback( - &inner, name_view, args_view, &out)) { + using namespace cbindgen_private; + Slice args_view { reinterpret_cast(args.ptr), args.len }; + ValueOpaque out; + if (sixtyfps_interpreter_component_instance_invoke_callback( + &inner, Slice::from_string(name), args_view, &out)) { return Value(out); } else { return {}; @@ -418,17 +407,13 @@ public: bool set_callback(std::string_view name, F callback) const { using cbindgen_private::ValueOpaque; - cbindgen_private::Slice name_view { - const_cast(reinterpret_cast(name.data())), - name.size() - }; auto actual_cb = [](void *data, Slice arg, ValueOpaque *ret) { Slice args_view { reinterpret_cast(arg.ptr), arg.len }; Value r = (*reinterpret_cast(data))(arg); new (ret) Value(std::move(r)); }; return cbindgen_private::sixtyfps_interpreter_component_instance_set_callback( - &inner, name_view, actual_cb, new F(std::move(callback)), + &inner, Slice::from_string(name), actual_cb, new F(std::move(callback)), [](void *data) { delete reinterpret_cast(data); }); } }; diff --git a/xtask/src/cbindgen.rs b/xtask/src/cbindgen.rs index 7e38adba6..9efba43f8 100644 --- a/xtask/src/cbindgen.rs +++ b/xtask/src/cbindgen.rs @@ -103,6 +103,17 @@ fn gen_corelib(root_dir: &Path, include_dir: &Path) -> anyhow::Result<()> { let mut string_config = config.clone(); string_config.export.exclude = vec!["SharedString".into()]; + string_config.export.body.insert( + "Slice".to_owned(), + " static Slice from_string(std::string_view str) { + return { + const_cast(reinterpret_cast(str.data())), + str.size() + }; + }" + .to_owned(), + ); + cbindgen::Builder::new() .with_config(string_config) .with_src(crate_dir.join("string.rs"))