1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::{wrap, CLabel, CStateId, RUSTFST_FFI_RESULT};

use ffi_convert::*;
use rustfst::prelude::{StateId, Tr};
use rustfst::semirings::TropicalWeight;
use rustfst::Semiring;

#[derive(Debug)]
#[repr(C)]
#[derive(CReprOf, AsRust, CDrop, RawPointerConverter)]
#[target_type(Tr::<TropicalWeight>)]
pub struct CTr {
    /// Input label.
    pub ilabel: CLabel,
    /// Output label.
    pub olabel: CLabel,
    /// Weight.
    pub weight: CTropicalWeight,
    /// ID of the next state.
    pub nextstate: CStateId,
}

#[derive(Debug)]
#[repr(C)]
#[derive(CDrop, RawPointerConverter)]
pub struct CTropicalWeight {
    value: libc::c_float,
}

impl CReprOf<TropicalWeight> for CTropicalWeight {
    fn c_repr_of(input: TropicalWeight) -> Result<Self, CReprOfError> {
        Ok(Self {
            value: input.take_value(),
        })
    }
}

impl AsRust<TropicalWeight> for CTropicalWeight {
    fn as_rust(&self) -> Result<TropicalWeight, AsRustError> {
        Ok(self.value.into())
    }
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_new(
    ilabel: CLabel,
    olabel: CLabel,
    weight: libc::c_float,
    nextstate: CStateId,
    new_struct: *mut *const CTr,
) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = CTr {
            ilabel,
            olabel,
            weight: CTropicalWeight { value: weight },
            nextstate,
        };
        let raw_pointer: *mut CTr = Box::into_raw(Box::new(tr));
        unsafe { *new_struct = raw_pointer };
        Ok(())
    })
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_ilabel(tr: *const CTr, ilabel: *mut CLabel) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = unsafe { <CTr as ffi_convert::RawBorrow<CTr>>::raw_borrow(tr)? };
        let ilabel_val = tr.ilabel;
        unsafe { *ilabel = ilabel_val }
        Ok(())
    })
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_set_ilabel(tr: *mut CTr, ilabel: *const CLabel) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = &mut unsafe { <CTr as ffi_convert::RawBorrowMut<CTr>>::raw_borrow_mut(tr)? };
        tr.ilabel = ilabel as CLabel;
        Ok(())
    })
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_olabel(tr: *const CTr, olabel: *mut CLabel) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = unsafe { <CTr as ffi_convert::RawBorrow<CTr>>::raw_borrow(tr)? };
        let olabel_val = tr.olabel;
        unsafe { *olabel = olabel_val }
        Ok(())
    })
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_set_olabel(tr: *mut CTr, olabel: *const CLabel) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = &mut unsafe { <CTr as ffi_convert::RawBorrowMut<CTr>>::raw_borrow_mut(tr)? };
        tr.olabel = olabel as CLabel;
        Ok(())
    })
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_weight(
    tr: *const CTr,
    weight: *mut libc::c_float,
) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = unsafe { <CTr as ffi_convert::RawBorrow<CTr>>::raw_borrow(tr)? };
        let weight_val = *tr.weight.as_rust()?.value();
        unsafe { *weight = weight_val }
        Ok(())
    })
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_set_weight(tr: *mut CTr, weight: libc::c_float) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = &mut unsafe { <CTr as ffi_convert::RawBorrowMut<CTr>>::raw_borrow_mut(tr)? };
        let tropical_weight = TropicalWeight::new(weight);
        tr.weight = CTropicalWeight::c_repr_of(tropical_weight)?;
        Ok(())
    })
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_next_state(
    tr: *const CTr,
    next_state: *mut CStateId,
) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = unsafe { <CTr as ffi_convert::RawBorrow<CTr>>::raw_borrow(tr)? };
        let next_state_val = tr.nextstate;
        unsafe { *next_state = next_state_val }
        Ok(())
    })
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_set_next_state(
    tr: *mut CTr,
    next_state: *const CStateId,
) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        let tr = &mut unsafe { <CTr as ffi_convert::RawBorrowMut<CTr>>::raw_borrow_mut(tr)? };
        tr.nextstate = next_state as StateId;
        Ok(())
    })
}

/// # Safety
///
/// The pointers should be valid.
#[no_mangle]
pub unsafe extern "C" fn tr_delete(tr_ptr: *mut CTr) -> RUSTFST_FFI_RESULT {
    wrap(|| {
        if tr_ptr.is_null() {
            return Ok(());
        }

        unsafe { drop(Box::from_raw(tr_ptr)) }
        Ok(())
    })
}