mirror of
https://github.com/ndarilek/tts-rs.git
synced 2024-11-25 13:19:36 +00:00
FFI: Implement tts_default() constructor and tts_free() destructor
* tts_default() allocates a new TTS struct via it's default() constructor, returning a pointer to it or NULL on error. * tts_free(tts) destroys the TTS pointed to by tts. If tts is NULL, this function does nothing.
This commit is contained in:
parent
c5bbbfcfd7
commit
5e7ab42f59
28
src/ffi.rs
28
src/ffi.rs
|
@ -5,6 +5,8 @@ use std::{
|
|||
ptr,
|
||||
};
|
||||
|
||||
use crate::TTS;
|
||||
|
||||
thread_local! {
|
||||
/// Stores the last reported error, so it can be retrieved at will from C
|
||||
static LAST_ERROR: RefCell<Option<CString>> = RefCell::new(None);
|
||||
|
@ -35,3 +37,29 @@ pub extern "C" fn tts_clear_error() {
|
|||
*err.borrow_mut() = None;
|
||||
});
|
||||
}
|
||||
|
||||
/// Creates a new TTS object with the default backend and returns a pointer to it.
|
||||
/// If an error occured, a null pointer is returned,
|
||||
/// Call `tts_get_error()` for more information about the specific error.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn tts_default() -> *mut TTS {
|
||||
match TTS::default() {
|
||||
Ok(tts) => Box::into_raw(Box::new(tts)),
|
||||
Err(e) => {
|
||||
set_last_error(e.to_string()).unwrap();
|
||||
ptr::null_mut()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Frees the memory associated with a TTS object.
|
||||
/// If `tts` is a null pointer, this function does nothing.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn tts_free(tts: *mut TTS) {
|
||||
if tts.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
Box::from_raw(tts); // Goes out of scope and is dropped
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user