dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.68k stars 320 forks source link

Question: How to Expose Function associated with Trait in Rust to C++ #1313

Open ankur19030 opened 4 months ago

ankur19030 commented 4 months ago

How to expose the function summarize to C++ via CXX ?

pub trait Summary {
    fn summarize(&self) -> String;
}

pub struct NewsArticle {
    pub headline: String,
    pub location: String,
    pub author: String,
    pub content: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("{}, by {} ({})", self.headline, self.author, self.location)
    }
}
volitank commented 2 months ago

https://github.com/dtolnay/cxx/pull/672/files

This can probably help you. It's not the most graceful way, but it works.