Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lossy conversion for string fields #145

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions vmicore/rust_src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,4 +25,4 @@ tower = "0.4.13"
ctrlc = "3.4.0"

[build-dependencies]
tonic-build = "0.9.2"
tonic-build = "0.11.0"
2 changes: 1 addition & 1 deletion vmicore/rust_src/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub mod ffi {
fn convert_to_log_level(level: &str) -> Result<Level>;

type LogField;
fn add_field_str(fields: &mut Vec<LogField>, name: &str, val: &str);
fn add_field_str(fields: &mut Vec<LogField>, name: &str, val: &[u8]);
fn add_field_i64(fields: &mut Vec<LogField>, name: &str, val: i64);
fn add_field_float64(fields: &mut Vec<LogField>, name: &str, val: f64);
fn add_field_uint64(fields: &mut Vec<LogField>, name: &str, val: u64);
Expand Down
2 changes: 1 addition & 1 deletion vmicore/rust_src/src/console_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct ConsoleLogger {

impl ConsoleLogger {
pub fn bind(&mut self, fields: Vec<LogField>) {
self.base_fields.extend(fields.into_iter());
self.base_fields.extend(fields);
}

pub fn clone_base_fields(&self, capacity: usize) -> Vec<LogField> {
Expand Down
2 changes: 1 addition & 1 deletion vmicore/rust_src/src/grpc_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl GrpcLogger {
}

pub fn bind(&mut self, fields: Vec<LogField>) {
self.base_fields.extend(fields.into_iter());
self.base_fields.extend(fields);
}

pub fn clone_base_fields(&self, capacity: usize) -> Vec<LogField> {
Expand Down
4 changes: 2 additions & 2 deletions vmicore/rust_src/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub fn convert_to_log_level(level: &str) -> Result<Level, Box<dyn std::error::Er
}
}

pub fn add_field_str(fields: &mut Vec<LogField>, name: &str, val: &str) {
pub fn add_field_str(fields: &mut Vec<LogField>, 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())),
});
}

Expand Down
11 changes: 8 additions & 3 deletions vmicore/src/lib/io/RustHelper.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef VMICORE_RUSTHELPER_H
#define VMICORE_RUSTHELPER_H

#include <bit>
#include <cstdint>
#include <cxx_rust_part/bridge.h>
#include <fmt/core.h>
#include <initializer_list>
#include <rust/cxx.h>
#include <string_view>
#include <type_traits>
#include <variant>

namespace VmiCore
{
Expand All @@ -16,6 +16,11 @@ namespace VmiCore
return {stringView.data(), stringView.size()};
}

inline ::rust::Slice<const uint8_t> toRustSlice(std::string_view stringView)
{
return {std::bit_cast<const uint8_t*>(stringView.data()), stringView.size()};
}

// Helper for overload pattern
template <class... Ts> struct overload : Ts...
{
Expand All @@ -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); },
Expand Down
Loading