summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2021-07-16 01:09:53 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2021-07-16 01:09:53 +0000
commit73438ee54930f03bdde8112d3c7dcb2664beb21a (patch)
tree267b4b32eec1a9c1ee6b136866a9352f2d782157
parent76e2159b55be0c4476ed9f44da3f6b397d225b89 (diff)
parent237505836d7cdb7b89008910620e00cb35f58623 (diff)
downloadextras-73438ee54930f03bdde8112d3c7dcb2664beb21a.tar.gz
Snap for 7552724 from 237505836d7cdb7b89008910620e00cb35f58623 to sc-d2-release
Change-Id: Ia5a8be5b69a38308a7ba165a6b9b45d5e1e0164c
-rw-r--r--profcollectd/libprofcollectd/bindings/libbase/Android.bp17
-rw-r--r--profcollectd/libprofcollectd/bindings/libbase/lib.rs31
-rw-r--r--profcollectd/libprofcollectd/bindings/libbase/properties.cpp7
-rw-r--r--profcollectd/libprofcollectd/bindings/libbase/properties.hpp9
-rw-r--r--profcollectd/libprofcollectd/config.rs4
5 files changed, 35 insertions, 33 deletions
diff --git a/profcollectd/libprofcollectd/bindings/libbase/Android.bp b/profcollectd/libprofcollectd/bindings/libbase/Android.bp
index b99d41d8..8e5fb1e0 100644
--- a/profcollectd/libprofcollectd/bindings/libbase/Android.bp
+++ b/profcollectd/libprofcollectd/bindings/libbase/Android.bp
@@ -26,20 +26,25 @@ package {
cc_library_static {
name: "libprofcollect_libbase",
srcs: ["properties.cpp"],
+ generated_headers: ["cxx-bridge-header"],
+ generated_sources: ["libprofcollect_libbase_bridge_code"],
}
-rust_bindgen {
- name: "libprofcollect_libbase_bindgen",
- wrapper_src: "properties.hpp",
- crate_name: "profcollect_libbase_bindgen",
- source_stem: "bindings",
+genrule {
+ name: "libprofcollect_libbase_bridge_code",
+ tools: ["cxxbridge"],
+ cmd: "$(location cxxbridge) $(in) >> $(out)",
+ srcs: ["lib.rs"],
+ out: ["libprofcollect_libbase_cxx_generated.cc"],
}
rust_library {
name: "libprofcollect_libbase_rust",
crate_name: "profcollect_libbase_rust",
srcs: ["lib.rs"],
- rlibs: ["libprofcollect_libbase_bindgen"],
+ rustlibs: [
+ "libcxx",
+ ],
static_libs: ["libprofcollect_libbase"],
shared_libs: [
"libc++",
diff --git a/profcollectd/libprofcollectd/bindings/libbase/lib.rs b/profcollectd/libprofcollectd/bindings/libbase/lib.rs
index 6ac34d58..bdd99a02 100644
--- a/profcollectd/libprofcollectd/bindings/libbase/lib.rs
+++ b/profcollectd/libprofcollectd/bindings/libbase/lib.rs
@@ -14,26 +14,21 @@
// limitations under the License.
//
-//! This module implements safe wrappers for GetProperty method from libbase.
+//! This module implements safe wrappers for GetProperty and SetProperty from libbase.
-use std::ffi::{CStr, CString};
+pub use ffi::{GetProperty, SetProperty};
-/// Returns the current value of the system property `key`,
-/// or `default_value` if the property is empty or doesn't exist.
-pub fn get_property<'a>(key: &str, default_value: &'a str) -> &'a str {
- let key = CString::new(key).unwrap();
- let default_value = CString::new(default_value).unwrap();
- unsafe {
- let cstr = profcollect_libbase_bindgen::GetProperty(key.as_ptr(), default_value.as_ptr());
- CStr::from_ptr(cstr).to_str().unwrap()
- }
-}
+/// Safe wrappers for the GetProperty and SetProperty methods from libbase.
+#[cxx::bridge]
+mod ffi {
+ unsafe extern "C++" {
+ include!("properties.hpp");
+
+ /// Returns the current value of the system property `key`,
+ /// or `default_value` if the property is empty or doesn't exist.
+ fn GetProperty(key: &str, default_value: &str) -> String;
-/// Sets the system property `key` to `value`.
-pub fn set_property(key: &str, value: &str) {
- let key = CString::new(key).unwrap();
- let value = CString::new(value).unwrap();
- unsafe {
- profcollect_libbase_bindgen::SetProperty(key.as_ptr(), value.as_ptr());
+ /// Sets the system property `key` to `value`.
+ fn SetProperty(key: &str, value: &str);
}
}
diff --git a/profcollectd/libprofcollectd/bindings/libbase/properties.cpp b/profcollectd/libprofcollectd/bindings/libbase/properties.cpp
index 01be7c73..908f19d0 100644
--- a/profcollectd/libprofcollectd/bindings/libbase/properties.cpp
+++ b/profcollectd/libprofcollectd/bindings/libbase/properties.cpp
@@ -17,11 +17,10 @@
#include "../../../../../libbase/include/android-base/properties.h"
#include "properties.hpp"
-const char* GetProperty(const char* key, const char* default_value) {
- auto v = android::base::GetProperty(std::string(key), std::string(default_value));
- return strdup(v.c_str());
+rust::String GetProperty(rust::Str key, rust::Str default_value) {
+ return android::base::GetProperty(std::string(key), std::string(default_value));
}
-void SetProperty(const char* key, const char* value) {
+void SetProperty(rust::Str key, rust::Str value) {
android::base::SetProperty(std::string(key), std::string(value));
}
diff --git a/profcollectd/libprofcollectd/bindings/libbase/properties.hpp b/profcollectd/libprofcollectd/bindings/libbase/properties.hpp
index d6785a28..c8ef1f64 100644
--- a/profcollectd/libprofcollectd/bindings/libbase/properties.hpp
+++ b/profcollectd/libprofcollectd/bindings/libbase/properties.hpp
@@ -14,6 +14,9 @@
* limitations under the License.
*/
-// C declaration for bindgen.
-const char* GetProperty(const char*, const char*);
-void SetProperty(const char*, const char*);
+#pragma once
+
+#include "rust/cxx.h"
+
+rust::String GetProperty(rust::Str, rust::Str);
+void SetProperty(rust::Str, rust::Str);
diff --git a/profcollectd/libprofcollectd/config.rs b/profcollectd/libprofcollectd/config.rs
index d2850e82..c3ab45cf 100644
--- a/profcollectd/libprofcollectd/config.rs
+++ b/profcollectd/libprofcollectd/config.rs
@@ -119,7 +119,7 @@ where
T::Err: Error + Send + Sync + 'static,
{
let default_value = default_value.to_string();
- let value = profcollect_libbase_rust::get_property(&key, &default_value);
+ let value = profcollect_libbase_rust::GetProperty(&key, &default_value);
Ok(T::from_str(&value)?)
}
@@ -128,7 +128,7 @@ where
T: ToString,
{
let value = value.to_string();
- profcollect_libbase_rust::set_property(&key, &value);
+ profcollect_libbase_rust::SetProperty(&key, &value);
}
fn generate_random_node_id() -> MacAddr6 {