rustfst_ffi/algorithms/
reverse.rs

1use anyhow::anyhow;
2
3use crate::fst::CFst;
4use crate::{get, wrap, RUSTFST_FFI_RESULT};
5
6use ffi_convert::RawPointerConverter;
7use rustfst::algorithms::reverse;
8use rustfst::fst_impls::VectorFst;
9use rustfst::semirings::TropicalWeight;
10
11/// # Safety
12///
13/// The pointers should be valid.
14#[no_mangle]
15pub unsafe extern "C" fn fst_reverse(
16    ptr: *const CFst,
17    res_ptr: *mut *const CFst,
18) -> RUSTFST_FFI_RESULT {
19    wrap(|| {
20        let fst = get!(CFst, ptr);
21        let vec_fst: &VectorFst<TropicalWeight> = fst
22            .downcast_ref()
23            .ok_or_else(|| anyhow!("Could not downcast to vector FST"))?;
24        let res_fst: VectorFst<TropicalWeight> = reverse(vec_fst)?;
25        unsafe { *res_ptr = CFst(Box::new(res_fst)).into_raw_pointer() };
26        Ok(())
27    })
28}