Hi, all guys. I'm new to Rust.
I have a function which its prototype is like:
pub fn fuzzy_match_v1(case_sensitive: bool, normalize: bool, forward: bool, text: &Rune, pattern: &Rune, with_pos: bool) -> Option<MatchResult>
#[derive(Debug)]
pub struct MatchResult {
start: usize,
end: usize,
score: i32,
pos: Vec<usize>,
}
pub struct Rune {
v: Vec<char>,
}
When I add #[no_mangle]
to the fuzzy_match_v1
, rustc
complains about the Option<MatchResult>
is not FFI-safe.
diff --git a/src/lib.rs b/src/lib.rs
index 2f03dde..d84a2ff 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -290,7 +290,8 @@ fn calculate_score(case_sensitive: bool, normalize: bool, text: &Rune, pattern:
(score, pos)
}
-pub fn fuzzy_match_v1(case_sensitive: bool, normalize: bool, forward: bool, text: &Rune, pattern: &Rune, with_pos: bool) -> Option<MatchResult> {
+#[no_mangle]
+pub extern "C" fn fuzzy_match_v1(case_sensitive: bool, normalize: bool, forward: bool, text: &Rune, pattern: &Rune, with_pos: bool) -> Option<MatchResult> {
if pattern.v.is_empty() {
return Some(MatchResult {
start: 0,
$ cargo build
warning: `extern` fn uses type `Option<MatchResult>`, which is not FFI-safe
--> src/lib.rs:294:136
|
294 | ...l, text: &Rune, pattern: &Rune, with_pos: bool) -> Option<MatchResult> {
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= note: `#[warn(improper_ctypes_definitions)]` on by default
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
warning: `fzf-fuzzy-match-rs` (lib) generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
I certainly misunderstood something, anyone can help with this?
Basically, I wanted to export the fuzzy_match_v1
to the corresponding C function signature so I can call this Rust library from C/C++.