rustfst_ffi/
string_path.rs

1use ffi_convert::{CReprOf, RawPointerConverter};
2use std::ffi::CString;
3
4use rustfst::semirings::TropicalWeight;
5use rustfst::{Semiring, StringPath};
6
7use crate::{get, wrap, RUSTFST_FFI_RESULT};
8
9#[derive(RawPointerConverter)]
10pub struct CStringPath(pub(crate) StringPath<TropicalWeight>);
11
12/// # Safety
13///
14/// The pointers should be valid.
15#[no_mangle]
16pub unsafe extern "C" fn string_path_destroy(iter_ptr: *mut CStringPath) -> RUSTFST_FFI_RESULT {
17    wrap(|| {
18        if iter_ptr.is_null() {
19            return Ok(());
20        }
21
22        drop(unsafe { Box::from_raw(iter_ptr) });
23        Ok(())
24    })
25}
26
27/// # Safety
28///
29/// The pointers should be valid.
30#[no_mangle]
31pub unsafe extern "C" fn string_path_weight(
32    c_string_path: *const CStringPath,
33    weight: *mut libc::c_float,
34) -> RUSTFST_FFI_RESULT {
35    wrap(|| {
36        let string_path = get!(CStringPath, c_string_path);
37        let weight_val = *string_path.weight().value();
38        unsafe { *weight = weight_val }
39        Ok(())
40    })
41}
42
43/// # Safety
44///
45/// The pointers should be valid.
46#[no_mangle]
47pub unsafe extern "C" fn string_path_istring(
48    c_string_path: *const CStringPath,
49    c_istring: *mut *const libc::c_char,
50) -> RUSTFST_FFI_RESULT {
51    wrap(|| {
52        let string_path = get!(CStringPath, c_string_path);
53        let istring = string_path.istring()?;
54        unsafe {
55            *c_istring = CString::c_repr_of(istring)?.into_raw_pointer() as *const libc::c_char
56        }
57
58        Ok(())
59    })
60}
61
62/// # Safety
63///
64/// The pointers should be valid.
65#[no_mangle]
66pub unsafe extern "C" fn string_path_ostring(
67    c_string_path: *const CStringPath,
68    c_ostring: *mut *const libc::c_char,
69) -> RUSTFST_FFI_RESULT {
70    wrap(|| {
71        let string_path = get!(CStringPath, c_string_path);
72        let ostring = string_path.ostring()?;
73        unsafe {
74            *c_ostring = CString::c_repr_of(ostring)?.into_raw_pointer() as *const libc::c_char
75        }
76
77        Ok(())
78    })
79}