From ea42b791c244c5efe8163bdf435a04bf4b6ba7ac Mon Sep 17 00:00:00 2001 From: Dorian Eikenberg Date: Thu, 21 Mar 2024 11:47:15 +0100 Subject: [PATCH 1/2] Use lossy conversion for string fields --- vmicore/rust_src/src/bridge.rs | 2 +- vmicore/rust_src/src/logging.rs | 4 ++-- vmicore/src/lib/io/RustHelper.h | 11 ++++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/vmicore/rust_src/src/bridge.rs b/vmicore/rust_src/src/bridge.rs index f84f5b75..7d561bdb 100644 --- a/vmicore/rust_src/src/bridge.rs +++ b/vmicore/rust_src/src/bridge.rs @@ -28,7 +28,7 @@ pub mod ffi { fn convert_to_log_level(level: &str) -> Result; type LogField; - fn add_field_str(fields: &mut Vec, name: &str, val: &str); + fn add_field_str(fields: &mut Vec, name: &str, val: &[u8]); fn add_field_i64(fields: &mut Vec, name: &str, val: i64); fn add_field_float64(fields: &mut Vec, name: &str, val: f64); fn add_field_uint64(fields: &mut Vec, name: &str, val: u64); diff --git a/vmicore/rust_src/src/logging.rs b/vmicore/rust_src/src/logging.rs index 571fbb58..2b606e9b 100644 --- a/vmicore/rust_src/src/logging.rs +++ b/vmicore/rust_src/src/logging.rs @@ -21,10 +21,10 @@ pub fn convert_to_log_level(level: &str) -> Result, name: &str, val: &str) { +pub fn add_field_str(fields: &mut Vec, name: &str, val: &[u8]) { fields.push(LogField { name: name.to_string(), - field: Some(Field::StrField(val.to_string())), + field: Some(Field::StrField(String::from_utf8_lossy(val).into_owned())), }); } diff --git a/vmicore/src/lib/io/RustHelper.h b/vmicore/src/lib/io/RustHelper.h index c92722ed..d5ce2dd7 100644 --- a/vmicore/src/lib/io/RustHelper.h +++ b/vmicore/src/lib/io/RustHelper.h @@ -1,13 +1,13 @@ #ifndef VMICORE_RUSTHELPER_H #define VMICORE_RUSTHELPER_H +#include #include #include -#include #include #include #include -#include +#include namespace VmiCore { @@ -16,6 +16,11 @@ namespace VmiCore return {stringView.data(), stringView.size()}; } + inline ::rust::Slice toRustSlice(std::string_view stringView) + { + return {std::bit_cast(stringView.data()), stringView.size()}; + } + // Helper for overload pattern template struct overload : Ts... { @@ -27,7 +32,7 @@ namespace VmiCore { std::visit( overload{[&vec, &field](std::string_view arg) - { ::logging::add_field_str(vec, toRustStr(field.first), toRustStr(arg)); }, + { ::logging::add_field_str(vec, toRustStr(field.first), toRustSlice(arg)); }, [&vec, &field](bool arg) { ::logging::add_field_bool(vec, toRustStr(field.first), arg); }, [&vec, &field](int64_t arg) { ::logging::add_field_i64(vec, toRustStr(field.first), arg); }, [&vec, &field](uint64_t arg) { ::logging::add_field_uint64(vec, toRustStr(field.first), arg); }, From a8ff123a1eacc45a5ab58ceaa901f75f7121366e Mon Sep 17 00:00:00 2001 From: Dorian Eikenberg Date: Thu, 21 Mar 2024 12:01:45 +0100 Subject: [PATCH 2/2] Apply clippy hints and update deps --- vmicore/rust_src/Cargo.toml | 8 ++++---- vmicore/rust_src/src/console_logger.rs | 2 +- vmicore/rust_src/src/grpc_logger.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/vmicore/rust_src/Cargo.toml b/vmicore/rust_src/Cargo.toml index 6c1bc2d2..2a8a03d8 100644 --- a/vmicore/rust_src/Cargo.toml +++ b/vmicore/rust_src/Cargo.toml @@ -7,9 +7,9 @@ edition = "2021" crate-type = ["lib", "staticlib"] [dependencies] -tonic = "0.9.2" -prost = "0.11.2" -prost-types = "0.11.2" +tonic = "0.11.0" +prost = "0.12.3" +prost-types = "0.12.3" tokio = { version = "1.22", features = ["macros", "rt-multi-thread", "net"] } cxx = "1.0" triggered = "0.1.2" @@ -25,4 +25,4 @@ tower = "0.4.13" ctrlc = "3.4.0" [build-dependencies] -tonic-build = "0.9.2" +tonic-build = "0.11.0" diff --git a/vmicore/rust_src/src/console_logger.rs b/vmicore/rust_src/src/console_logger.rs index 71e60d8e..76c1062f 100644 --- a/vmicore/rust_src/src/console_logger.rs +++ b/vmicore/rust_src/src/console_logger.rs @@ -44,7 +44,7 @@ pub struct ConsoleLogger { impl ConsoleLogger { pub fn bind(&mut self, fields: Vec) { - self.base_fields.extend(fields.into_iter()); + self.base_fields.extend(fields); } pub fn clone_base_fields(&self, capacity: usize) -> Vec { diff --git a/vmicore/rust_src/src/grpc_logger.rs b/vmicore/rust_src/src/grpc_logger.rs index a37be95e..fd150418 100644 --- a/vmicore/rust_src/src/grpc_logger.rs +++ b/vmicore/rust_src/src/grpc_logger.rs @@ -31,7 +31,7 @@ impl GrpcLogger { } pub fn bind(&mut self, fields: Vec) { - self.base_fields.extend(fields.into_iter()); + self.base_fields.extend(fields); } pub fn clone_base_fields(&self, capacity: usize) -> Vec {