Skip to content

Commit

Permalink
Use lossy conversion for string fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorian Eikenberg committed Mar 21, 2024
1 parent 5090ad3 commit ea42b79
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
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
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

0 comments on commit ea42b79

Please sign in to comment.