diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-07-17 12:07:47 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-07-17 12:07:47 +0000 |
commit | 27e8543c279bf24ac0671a8313a49c62e195e2ee (patch) | |
tree | 267b4b32eec1a9c1ee6b136866a9352f2d782157 /profcollectd/libprofcollectd | |
parent | a1c63fd79c0bbab02c20a1c83a3d184af4488df6 (diff) | |
parent | 47b3c09c451749824a70a1d392097670a7a4bf95 (diff) | |
download | extras-27e8543c279bf24ac0671a8313a49c62e195e2ee.tar.gz |
Snap for 7558977 from 47b3c09c451749824a70a1d392097670a7a4bf95 to mainline-media-swcodec-release
Change-Id: If39de2cdc2b029cfed5d9b2c0fe9a8729a59c00f
Diffstat (limited to 'profcollectd/libprofcollectd')
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 { |