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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! This module exists purely for other languages to interface with mwalib.
use crate::*;
use ;
use ;
pub
/// mwalib FFI interface return codes
pub const MWALIB_SUCCESS: i32 = 0;
pub const MWALIB_FAILURE: i32 = 1;
pub const MWALIB_NO_DATA_FOR_TIMESTEP_COARSECHAN: i32 = -1;
/// Generic helper function for all FFI modules to take an already allocated C string buffer
/// and populate it with a string. This is primarily used to pass messages back to C from Rust.
///
/// # Arguments
///
/// * `in_message` - A Rust string holing the message you want to pass back to C
///
/// * `buffer_ptr` - Pointer to a char* buffer which has already been allocated, for storing the message.
///
/// * `buffer_len` - Length of char* buffer allocated by caller in C.
///
///
/// # Returns
///
/// * Number of bytes written including the NUL terminator
///
///
/// # Safety
/// It is up to the caller to:
/// - Allocate `buffer_len` bytes as a `char*` on the heap
/// - Free `buffer_ptr` (in C) once finished with the buffer
///
pub
/// Return the MAJOR version number of mwalib
///
/// Uses the built crate in build.rs to generate at build time a built.rs module
///
/// # Arguments
///
/// * None
///
/// # Returns
///
/// * u16 representing the major version number of mwalib
///
pub extern "C"
/// Return the MINOR version number of mwalib
///
/// Uses the built crate in build.rs to generate at build time a built.rs module
///
/// # Arguments
///
/// * None
///
/// # Returns
///
/// * u16 representing the minor version number of mwalib
///
pub extern "C"
/// Return the PATCH version number of mwalib
///
/// Uses the built crate in build.rs to generate at build time a built.rs module
///
/// # Arguments
///
/// * None
///
/// # Returns
///
/// * u16 representing the patch version number of mwalib
///
pub extern "C"
/// Free a rust-allocated CString.
///
/// mwalib uses error strings to detail the caller with anything that went
/// wrong. Non-rust languages cannot deallocate these strings; so, call this
/// function with the pointer to do that.
///
/// # Arguments
///
/// * `rust_cstring` - pointer to a `char*` of a Rust string
///
///
/// # Returns
///
/// * MWALIB_SUCCESS on success, non-zero on failure
///
/// # Safety
/// * rust_cstring must not have already been freed and must point to a Rust string.
pub unsafe extern "C"
/// Utility: Create a C String from a Rust string
///
/// Take a reference to a Rust string and return a C string pointer, transferring ownership to the caller (C).
///
/// # Arguments
///
/// * `rust_cstring` - pointer to a `char*` of a Rust string
///
///
/// # Returns
///
/// * A mutable pointer to c_char. This is C owned, and if there was an error with the string, e.g. an
/// extra NUL terminator for some reason, then the function will return a NULL pointer.
///
/// # Safety
/// * It is up to the caller to free the string, using `ffi_free_rust_c_string`.
/// Utility: free a Rust-allocated C string returned by this API.
///
/// # Arguments
///
/// * `c_string_ptr` - pointer to a C-owned `char*`
///
/// # Returns
///
/// * Nothing
///
/// # Safety
/// * c_string_ptr must not have already been freed and must point to a C string.
/// Utility: Create a C Array from a Rust Vector
///
/// # Arguments
///
/// * `rust_vec` - A Rust owned vector of type T
///
/// # Returns
///
/// * Pointer to a C owned array of type T*
///
/// * Length of the C owned array of type T*
///
/// # Safety
/// * You must call `ffi_free_c_array` passing the pointer and len to properly free the object.
/// Utility: free a Rust-allocated C struct returned by this API
///
/// # Arguments
///
/// * `c_vec_ptr` - C pointer to array of T
///
/// * `c_vec_len` - Length of array
///
/// # Returns
///
/// * Nothing
///
/// # Safety
/// * c_vec_ptr must not have already been freed and must point to a populate T*.
/// * If the T contains members which also have vectors or other objects that need freeing, you'll need to do that first
/// before calling this.