None working attempt at using cxx.

This commit is contained in:
James Pace 2026-07-19 20:24:22 -04:00
parent 4f509b4ad6
commit 93a9f1baba
5 changed files with 227 additions and 148 deletions

View File

@ -20,7 +20,7 @@ corrosion_add_cxxbridge(j7s_diagnostics_cxx_bridge
FILES lib.rs)
add_executable(test_bin src/test.cpp)
target_link_libraries(test_bin PRIVATE j7s_diagnostics_cxx_bridge)
target_link_libraries(test_bin PRIVATE j7s_diagnostics_cxx_bridge j7s_diagnostics_cxx)
corrosion_install(TARGETS j7s_diagnostics_cxx
EXPORT export_${PROJECT_NAME}

View File

@ -9,10 +9,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
[[package]]
name = "cc"
version = "1.2.65"
name = "anyhow"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e228eec9be7c17ccb640b59b36a5cd805ea2a564a4c5e162c2f659fea30d3b96"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "cc"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c89588d05638b5b4594a3348a2d6c20277e43a7f5c5202b05cc56888475a47b8"
dependencies = [
"find-msvc-tools",
"shlex",
@ -20,18 +26,18 @@ dependencies = [
[[package]]
name = "clap"
version = "4.6.1"
version = "4.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
checksum = "dd059f9da4f5c36b3787f65d38ccaab1cc315f07b01f89abc8359ee6a8205011"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.6.0"
version = "4.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
checksum = "f09628afdcc538b57f3c6341e9c8e9970f18e4a481690a64974d7023bd33548b"
dependencies = [
"anstyle",
"clap_lex",
@ -57,9 +63,9 @@ dependencies = [
[[package]]
name = "cxx"
version = "1.0.194"
version = "1.0.198"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "747d8437319e3a2f43d93b341c137927ca70c0f5dabeea7a005a73665e247c7e"
checksum = "6fe442a792c7c736eea18b32a7f8a3b63cf8aafabda6760042dc2fdeda456291"
dependencies = [
"cc",
"cxx-build",
@ -72,9 +78,9 @@ dependencies = [
[[package]]
name = "cxx-build"
version = "1.0.194"
version = "1.0.198"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0f4697d190a142477b16aef7da8a99bfdc41e7e8b1687583c0d23a79c7afc1e"
checksum = "e3184a94384c663718698311a78a51ac00c484c10b4eeac06fb0a068c5f64fa2"
dependencies = [
"cc",
"codespan-reporting",
@ -87,9 +93,9 @@ dependencies = [
[[package]]
name = "cxxbridge-cmd"
version = "1.0.194"
version = "1.0.198"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0956799fa8678d4c50eed028f2de1c0552ae183c76e976cf7ca8c4e36a7c328"
checksum = "0148d8fd1199329ddf1d157a5e134e51ceff37c6a7ddd38615c399d81cb05d8d"
dependencies = [
"clap",
"codespan-reporting",
@ -101,15 +107,15 @@ dependencies = [
[[package]]
name = "cxxbridge-flags"
version = "1.0.194"
version = "1.0.198"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23384a836ab4f0ad98ace7e3955ad2de39de42378ab487dc28d3990392cb283a"
checksum = "52850339faed2eaadd24e286dc1d8268cc6f8a7bd9524d713adc9099566b4c89"
[[package]]
name = "cxxbridge-macro"
version = "1.0.194"
version = "1.0.198"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6acc6b5822b9526adfb4fc377b67128fdd60aac757cc4a741a6278603f763cf"
checksum = "2c77c856545d886c9bd5215409ebb63b925e262135248b50c79e5a5f194ee47c"
dependencies = [
"indexmap",
"proc-macro2",
@ -151,14 +157,30 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "j7s_diagnostics"
version = "0.1.0"
source = "git+https://git.jpace121.net/public/j7s_diagnostics.git?branch=main#f40b64151807cbca03a021e66cb3bf3558e4619d"
dependencies = [
"limbo_graph",
"thiserror",
]
[[package]]
name = "j7s_diagnostics_cxx"
version = "0.1.0"
dependencies = [
"anyhow",
"cxx",
"cxx-build",
"j7s_diagnostics",
]
[[package]]
name = "limbo_graph"
version = "0.1.0"
source = "git+https://git.jpace121.net/public/limbo_graph.git?branch=main#2d05aed05750fdde52424404001315a967e0d696"
[[package]]
name = "link-cplusplus"
version = "1.0.12"
@ -170,18 +192,18 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.106"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.46"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
"proc-macro2",
]
@ -194,9 +216,9 @@ checksum = "d68f2ec51b097e4c1a75b681a8bec621909b5e91f15bb7b840c4f2f7b01148b2"
[[package]]
name = "serde"
version = "1.0.228"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
dependencies = [
"serde_core",
"serde_derive",
@ -204,18 +226,18 @@ dependencies = [
[[package]]
name = "serde_core"
version = "1.0.228"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
dependencies = [
"proc-macro2",
"quote",
@ -236,9 +258,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.118"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422"
checksum = "5edbec4ed188954a10c12c038215f8ce7606b2d5c973cd8dc43e8795065c5f2f"
dependencies = [
"proc-macro2",
"quote",
@ -254,6 +276,26 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "2.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09a43598840e33d5b0331f38c5e30d13bb11c11210a4b58f0d9b18a5a5eefcd9"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "2.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43cbfe0cf76104d42a574802844187e84a305e531ed54455f11fbde0f10541cd"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.24"
@ -289,99 +331,3 @@ checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[patch.unused]]
name = "action_msgs"
version = "2.4.5"
[[patch.unused]]
name = "builtin_interfaces"
version = "2.4.5"
[[patch.unused]]
name = "composition_interfaces"
version = "2.4.5"
[[patch.unused]]
name = "diagnostic_msgs"
version = "5.9.2"
[[patch.unused]]
name = "geometry_msgs"
version = "5.9.2"
[[patch.unused]]
name = "lifecycle_msgs"
version = "2.4.5"
[[patch.unused]]
name = "limbo_graph"
version = "0.1.0"
[[patch.unused]]
name = "nav_msgs"
version = "5.9.2"
[[patch.unused]]
name = "rcl_interfaces"
version = "2.4.5"
[[patch.unused]]
name = "rmw_dds_common"
version = "6.0.0"
[[patch.unused]]
name = "rosbag2_interfaces"
version = "0.33.3"
[[patch.unused]]
name = "rosgraph_msgs"
version = "2.4.5"
[[patch.unused]]
name = "sensor_msgs"
version = "5.9.2"
[[patch.unused]]
name = "service_msgs"
version = "2.4.5"
[[patch.unused]]
name = "shape_msgs"
version = "5.9.2"
[[patch.unused]]
name = "statistics_msgs"
version = "2.4.5"
[[patch.unused]]
name = "std_msgs"
version = "5.9.2"
[[patch.unused]]
name = "std_srvs"
version = "5.9.2"
[[patch.unused]]
name = "stereo_msgs"
version = "5.9.2"
[[patch.unused]]
name = "tf2_msgs"
version = "0.45.7"
[[patch.unused]]
name = "trajectory_msgs"
version = "5.9.2"
[[patch.unused]]
name = "type_description_interfaces"
version = "2.4.5"
[[patch.unused]]
name = "unique_identifier_msgs"
version = "2.8.1"
[[patch.unused]]
name = "visualization_msgs"
version = "5.9.2"

View File

@ -4,10 +4,14 @@ version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["cdylib"]
crate-type = ["staticlib"]
[dependencies]
anyhow = "1.0.103"
cxx = "1.0"
j7s_diagnostics = { branch = "main", git="https://git.jpace121.net/public/j7s_diagnostics.git" }
[build-dependencies]
cxx-build = "1.0"
[patch.crates-io]

View File

@ -1,35 +1,159 @@
#![allow(non_snake_case)]
use anyhow::anyhow;
use std::collections::BTreeMap;
#[cxx::bridge]
mod ffi {
// Shared structs whose fields can be seen in both C++ and Rust.
#[derive(Clone, PartialOrd, PartialEq, Default)]
pub enum DiagnosticLevel {
#[default]
UNSET,
OK,
WARN,
ERROR,
STALE,
}
#[derive(Clone)]
pub struct DiagnosticStatus {
level: DiagnosticLevel,
name: String,
message: String,
hardware_id: String,
values: Box<StringMap>
}
extern "Rust" {
fn add(a: &f64, b: &f64) -> f64;
// Functions in here can be seen on both sides, but are
// defined in rust.
// types can be seen in both languages, but only opaquely in C++.
type OptionalString;
#[Self = "OptionalString"]
fn some(val: String) -> Box<OptionalString>;
#[Self = "OptionalString"]
fn none() -> Box<OptionalString>;
fn is_some(self: &OptionalString) -> bool;
fn value(self: &OptionalString) -> Result<String>;
type StringMap;
#[Self = "StringMap"]
fn build() -> Box<StringMap>;
fn get(self: &StringMap, key: &String) -> Box<OptionalString>;
fn insert(self: &mut StringMap, key: String, value: String);
type DiagnosticTree;
#[Self = "DiagnosticTree"]
fn build() -> Box<DiagnosticTree>;
fn add_statuses(self: &mut DiagnosticTree, statuses: &Vec<DiagnosticStatus>) -> Result<()> ;
fn reconcile(self: &mut DiagnosticTree) -> Result<()>;
fn aggregate(self: &DiagnosticTree) -> Result<Vec<DiagnosticStatus>>;
type MyStruct;
fn MyStruct_build(a: f64, b: f64) -> Box<MyStruct>;
fn sum(self: &MyStruct) -> f64;
}
}
pub struct MyStruct {
a: f64,
b: f64
fn diagnostic_status_to_rust(cpp: &crate::ffi::DiagnosticStatus) -> j7s_diagnostics::DiagnosticStatus {
todo!()
}
impl MyStruct {
pub fn new(a: f64, b: f64) -> Self {
MyStruct {a: a, b: b}
fn diagnostic_status_from_rust(rust: &j7s_diagnostics::DiagnosticStatus) -> crate::ffi::DiagnosticStatus {
todo!()
}
pub fn sum(&self) -> f64 {
self.a + self.b
#[derive(Clone)]
pub struct DiagnosticTree {
graph: j7s_diagnostics::DiagnosticGraph,
}
impl DiagnosticTree {
pub fn build() -> Box<DiagnosticTree> {
let graph = j7s_diagnostics::DiagnosticGraph::new();
let tree = DiagnosticTree {
graph: graph
};
Box::new(tree)
}
pub fn add_statuses(self: &mut DiagnosticTree, statuses: &Vec<crate::ffi::DiagnosticStatus>) -> anyhow::Result<()> {
let mut converted_statuses = Vec::new();
for status in statuses.iter() {
converted_statuses.push(diagnostic_status_to_rust(&status));
}
Ok(self.graph.add_status_vec(&converted_statuses)?)
}
pub fn reconcile(self: &mut DiagnosticTree) -> anyhow::Result<()> {
Ok(self.graph.reconcile_levels()?)
}
fn aggregate(self: &DiagnosticTree) -> anyhow::Result<Vec<crate::ffi::DiagnosticStatus>> {
let statuses = self.graph.export_graph()?;
let mut converted_statuses = Vec::new();
for status in statuses.iter() {
converted_statuses.push(diagnostic_status_from_rust(&status));
}
Ok(converted_statuses)
}
}
fn MyStruct_build(a: f64, b: f64) -> Box<MyStruct> {
Box::new(MyStruct::new(a, b))
#[derive(Clone)]
pub struct OptionalString {
val: Option<String>
}
pub fn add(a: &f64, b: &f64) -> f64 {
a + b
impl OptionalString {
pub fn some(val: String) -> Box<Self> {
let raw = Self {
val: Some(val)
};
Box::new(raw)
}
pub fn none() -> Box<Self> {
let raw = Self {
val: None
};
Box::new(raw)
}
pub fn is_some(&self) -> bool {
self.val.is_some()
}
pub fn value(&self) -> anyhow::Result<String> {
match &self.val {
None => Err(anyhow!("Option not valid.")),
Some(val) => Ok(val.clone()),
}
}
}
#[derive(Clone)]
pub struct StringMap {
map: BTreeMap<String, String>
}
impl StringMap {
pub fn build() -> Box<Self> {
let raw = Self {
map: BTreeMap::<String, String>::new()
};
Box::new(raw)
}
pub fn get(&self, key: &String) -> Box<OptionalString> {
match self.map.get(key) {
None => OptionalString::none(),
Some(val) => OptionalString::some(val.clone()),
}
}
pub fn insert(&mut self, key: String, value: String) {
let _ = self.map.insert(key, value);
}
pub fn to_box(self) -> Box<StringMap> {
Box::new(self)
}
}

View File

@ -4,10 +4,15 @@
int main(int argc, char *argv[])
{
double result = add(1.0, 2.0);
std::cout << result << std::endl;
const auto my_string = OptionalString::some(rust::String("test"));
if(my_string->is_some())
{
auto as_string = my_string->value();
std::cout << as_string.c_str() << std::endl;
}
auto my_map = StringMap::build();
my_map->insert(rust::String("a"), rust::String("b"));
const auto my_struct = MyStruct_build(3.0, 4.0);
std::cout << my_struct->sum() << std::endl;
return 0;
}