summaryrefslogtreecommitdiff
path: root/profcollectd
diff options
context:
space:
mode:
authorJoel Galenson <jgalenson@google.com>2021-07-28 18:17:19 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2021-07-28 18:17:19 +0000
commit20100be3f3121a84e8577af093438a6bec752875 (patch)
tree29385b08163eed817a36c82b6ea125c4ad48633f /profcollectd
parented06edc2ec78b792d1c0c2e6c0fb29957fa0dd80 (diff)
parentc8fb4177717b50c3c5789011e4ab795771a62102 (diff)
downloadextras-20100be3f3121a84e8577af093438a6bec752875.tar.gz
Merge "Migrate profcollectd's libflags bindings from bindgen to cxx."
Diffstat (limited to 'profcollectd')
-rw-r--r--profcollectd/libprofcollectd/bindings/libflags/Android.bp15
-rw-r--r--profcollectd/libprofcollectd/bindings/libflags/get_flags.cpp13
-rw-r--r--profcollectd/libprofcollectd/bindings/libflags/get_flags.hpp7
-rw-r--r--profcollectd/libprofcollectd/bindings/libflags/lib.rs37
-rw-r--r--profcollectd/libprofcollectd/config.rs2
5 files changed, 36 insertions, 38 deletions
diff --git a/profcollectd/libprofcollectd/bindings/libflags/Android.bp b/profcollectd/libprofcollectd/bindings/libflags/Android.bp
index 102a6c05..fb846678 100644
--- a/profcollectd/libprofcollectd/bindings/libflags/Android.bp
+++ b/profcollectd/libprofcollectd/bindings/libflags/Android.bp
@@ -26,20 +26,23 @@ package {
cc_library_static {
name: "libprofcollect_libflags",
srcs: ["get_flags.cpp"],
+ generated_headers: ["cxx-bridge-header"],
+ generated_sources: ["libprofcollect_libflags_bridge_code"],
}
-rust_bindgen {
- name: "libprofcollect_libflags_bindgen",
- wrapper_src: "get_flags.hpp",
- crate_name: "profcollect_libflags_bindgen",
- source_stem: "bindings",
+genrule {
+ name: "libprofcollect_libflags_bridge_code",
+ tools: ["cxxbridge"],
+ cmd: "$(location cxxbridge) $(in) >> $(out)",
+ srcs: ["lib.rs"],
+ out: ["libprofcollect_libflags_cxx_generated.cc"],
}
rust_library {
name: "libprofcollect_libflags_rust",
crate_name: "profcollect_libflags_rust",
srcs: ["lib.rs"],
- rlibs: ["libprofcollect_libflags_bindgen"],
+ rustlibs: ["libcxx"],
static_libs: ["libprofcollect_libflags"],
shared_libs: [
"libc++",
diff --git a/profcollectd/libprofcollectd/bindings/libflags/get_flags.cpp b/profcollectd/libprofcollectd/bindings/libflags/get_flags.cpp
index e5ce65a1..d8bab841 100644
--- a/profcollectd/libprofcollectd/bindings/libflags/get_flags.cpp
+++ b/profcollectd/libprofcollectd/bindings/libflags/get_flags.cpp
@@ -17,12 +17,9 @@
#include "../../../../../server_configurable_flags/libflags/include/server_configurable_flags/get_flags.h"
#include "get_flags.hpp"
-const char* GetServerConfigurableFlag(const char* experiment_category_name,
- const char* experiment_flag_name,
- const char* default_value) {
- auto v = server_configurable_flags::GetServerConfigurableFlag(
- std::string(experiment_category_name),
- std::string(experiment_flag_name),
- std::string(default_value));
- return strdup(v.c_str());
+rust::String GetServerConfigurableFlag(rust::Str experiment_category_name,
+ rust::Str experiment_flag_name, rust::Str default_value) {
+ return server_configurable_flags::GetServerConfigurableFlag(std::string(experiment_category_name),
+ std::string(experiment_flag_name),
+ std::string(default_value));
}
diff --git a/profcollectd/libprofcollectd/bindings/libflags/get_flags.hpp b/profcollectd/libprofcollectd/bindings/libflags/get_flags.hpp
index ab3be989..a42c52e8 100644
--- a/profcollectd/libprofcollectd/bindings/libflags/get_flags.hpp
+++ b/profcollectd/libprofcollectd/bindings/libflags/get_flags.hpp
@@ -14,5 +14,8 @@
* limitations under the License.
*/
-// C declaration for bindgen.
-const char* GetServerConfigurableFlag(const char*, const char*, const char*);
+#pragma once
+
+#include "rust/cxx.h"
+
+rust::String GetServerConfigurableFlag(rust::Str, rust::Str, rust::Str);
diff --git a/profcollectd/libprofcollectd/bindings/libflags/lib.rs b/profcollectd/libprofcollectd/bindings/libflags/lib.rs
index c8875cb1..c6435bbd 100644
--- a/profcollectd/libprofcollectd/bindings/libflags/lib.rs
+++ b/profcollectd/libprofcollectd/bindings/libflags/lib.rs
@@ -17,27 +17,22 @@
//! This module implements safe wrappers for GetServerConfigurableFlag method
//! from libflags.
-use std::ffi::{CStr, CString};
+pub use ffi::GetServerConfigurableFlag;
-/// Use the category name and flag name registered in SettingsToPropertiesMapper.java
-/// to query the experiment flag value. This method will return default_value if
-/// querying fails.
-/// Note that for flags from Settings.Global, experiment_category_name should
-/// always be global_settings.
-pub fn get_server_configurable_flag<'a>(
- experiment_category_name: &str,
- experiment_flag_name: &str,
- default_value: &'a str,
-) -> &'a str {
- let experiment_category_name = CString::new(experiment_category_name).unwrap();
- let experiment_flag_name = CString::new(experiment_flag_name).unwrap();
- let default_value = CString::new(default_value).unwrap();
- unsafe {
- let cstr = profcollect_libflags_bindgen::GetServerConfigurableFlag(
- experiment_category_name.as_ptr(),
- experiment_flag_name.as_ptr(),
- default_value.as_ptr(),
- );
- CStr::from_ptr(cstr).to_str().unwrap()
+#[cxx::bridge]
+mod ffi {
+ unsafe extern "C++" {
+ include!("get_flags.hpp");
+
+ /// Use the category name and flag name registered in SettingsToPropertiesMapper.java
+ /// to query the experiment flag value. This method will return default_value if
+ /// querying fails.
+ /// Note that for flags from Settings.Global, experiment_category_name should
+ /// always be global_settings.
+ fn GetServerConfigurableFlag(
+ experiment_category_name: &str,
+ experiment_flag_name: &str,
+ default_value: &str,
+ ) -> String;
}
}
diff --git a/profcollectd/libprofcollectd/config.rs b/profcollectd/libprofcollectd/config.rs
index e7dac241..bc41e99e 100644
--- a/profcollectd/libprofcollectd/config.rs
+++ b/profcollectd/libprofcollectd/config.rs
@@ -107,7 +107,7 @@ where
T::Err: Error + Send + Sync + 'static,
{
let default_value = default_value.to_string();
- let config = profcollect_libflags_rust::get_server_configurable_flag(
+ let config = profcollect_libflags_rust::GetServerConfigurableFlag(
&PROFCOLLECT_CONFIG_NAMESPACE,
&key,
&default_value,