Refactor to FnMut and bump version.

This commit is contained in:
Nolan Darilek 2020-09-25 10:36:00 -05:00
parent 7f952f2f34
commit 5349a0fc3d
3 changed files with 33 additions and 44 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "speech-dispatcher" name = "speech-dispatcher"
version = "0.6.0" version = "0.7.0"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"] authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs" repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs"
description = "Rusty interface to the speech-dispatcher speech synthesis library" description = "Rusty interface to the speech-dispatcher speech synthesis library"

View File

@ -1,5 +1,3 @@
extern crate speech_dispatcher;
use speech_dispatcher::*; use speech_dispatcher::*;
use std::io; use std::io;
@ -10,12 +8,12 @@ fn main() {
"hello_world", "hello_world",
Mode::Threaded, Mode::Threaded,
); );
connection.on_begin(Some(|msg_id, client_id| { connection.on_begin(Some(Box::new(|msg_id, client_id| {
println!("Beginning {} from {}", msg_id, client_id) println!("Beginning {} from {}", msg_id, client_id)
})); })));
connection.on_end(Some(|msg_id, client_id| { connection.on_end(Some(Box::new(|msg_id, client_id| {
println!("Ending {} from {}", msg_id, client_id) println!("Ending {} from {}", msg_id, client_id)
})); })));
connection.say( connection.say(
Priority::Important, Priority::Important,
format!( format!(

View File

@ -82,28 +82,19 @@ fn i32_to_bool(v: i32) -> bool {
v == 1 v == 1
} }
#[derive(Clone, Copy)] #[derive(Default)]
struct Callbacks { struct Callbacks {
begin: Option<fn(u64, u64)>, begin: Option<Box<dyn FnMut(u64, u64)>>,
end: Option<fn(u64, u64)>, end: Option<Box<dyn FnMut(u64, u64)>>,
index_mark: Option<fn(u64, u64, String)>, index_mark: Option<Box<dyn FnMut(u64, u64, String)>>,
cancel: Option<fn(u64, u64)>, cancel: Option<Box<dyn FnMut(u64, u64)>>,
pause: Option<fn(u64, u64)>, pause: Option<Box<dyn FnMut(u64, u64)>>,
resume: Option<fn(u64, u64)>, resume: Option<Box<dyn FnMut(u64, u64)>>,
} }
impl Default for Callbacks { unsafe impl Send for Callbacks {}
fn default() -> Self {
Callbacks { unsafe impl Sync for Callbacks {}
begin: None,
end: None,
index_mark: None,
cancel: None,
pause: None,
resume: None,
}
}
}
lazy_static! { lazy_static! {
static ref callbacks: Mutex<HashMap<u64, Callbacks>> = { static ref callbacks: Mutex<HashMap<u64, Callbacks>> = {
@ -121,16 +112,16 @@ unsafe extern "C" fn cb(msg_id: u64, client_id: u64, state: u32) {
SPDNotificationType_SPD_EVENT_RESUME => Notification::Resume, SPDNotificationType_SPD_EVENT_RESUME => Notification::Resume,
_ => panic!("Unknown notification received in callback: {}", state), _ => panic!("Unknown notification received in callback: {}", state),
}; };
if let Some(c) = callbacks.lock().unwrap().get(&client_id) { if let Some(c) = callbacks.lock().unwrap().get_mut(&client_id) {
let f = match state { let f = match state {
Notification::Begin => c.begin, Notification::Begin => &mut c.begin,
Notification::End => c.end, Notification::End => &mut c.end,
Notification::Cancel => c.cancel, Notification::Cancel => &mut c.cancel,
Notification::Pause => c.pause, Notification::Pause => &mut c.pause,
Notification::Resume => c.resume, Notification::Resume => &mut c.resume,
_ => panic!("Unknown notification type"), _ => panic!("Unknown notification type"),
}; };
if let Some(f) = f { if let Some(f) = f.as_mut() {
f(msg_id, client_id); f(msg_id, client_id);
} }
} }
@ -143,12 +134,12 @@ unsafe extern "C" fn cb_im(msg_id: u64, client_id: u64, state: u32, index_mark:
SPDNotificationType_SPD_EVENT_INDEX_MARK => Notification::IndexMarks, SPDNotificationType_SPD_EVENT_INDEX_MARK => Notification::IndexMarks,
_ => panic!("Unknown notification received in IM callback: {}", state), _ => panic!("Unknown notification received in IM callback: {}", state),
}; };
if let Some(c) = callbacks.lock().unwrap().get(&client_id) { if let Some(c) = callbacks.lock().unwrap().get_mut(&client_id) {
let f = match state { let f = match state {
Notification::IndexMarks => c.index_mark, Notification::IndexMarks => &mut c.index_mark,
_ => panic!("Unknown notification type"), _ => panic!("Unknown notification type"),
}; };
if let Some(f) = f { if let Some(f) = f.as_mut() {
f(msg_id, client_id, index_mark); f(msg_id, client_id, index_mark);
} }
} }
@ -240,12 +231,12 @@ impl Connection {
unsafe { spd_close(self.0) }; unsafe { spd_close(self.0) };
} }
pub fn say<S: Into<String>>(&self, priority: Priority, text: S) -> Option<i32> { pub fn say<S: Into<String>>(&self, priority: Priority, text: S) -> Option<u64> {
let text: String = text.into(); let text: String = text.into();
let param = CString::new(text).unwrap(); let param = CString::new(text).unwrap();
let rv = unsafe { spd_say(self.0, priority as u32, param.as_ptr()) }; let rv = unsafe { spd_say(self.0, priority as u32, param.as_ptr()) };
if rv != -1 { if rv != -1 {
Some(rv) Some(rv as u64)
} else { } else {
None None
} }
@ -596,7 +587,7 @@ impl Connection {
} }
} }
pub fn on_begin(&self, f: Option<fn(u64, u64)>) { pub fn on_begin(&self, f: Option<Box<dyn FnMut(u64, u64)>>) {
if let Ok(mut cbs) = callbacks.lock() { if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1); let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb { if let Some(cb) = cb {
@ -605,7 +596,7 @@ impl Connection {
} }
} }
pub fn on_end(&self, f: Option<fn(u64, u64)>) { pub fn on_end(&self, f: Option<Box<dyn FnMut(u64, u64)>>) {
if let Ok(mut cbs) = callbacks.lock() { if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1); let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb { if let Some(cb) = cb {
@ -614,7 +605,7 @@ impl Connection {
} }
} }
pub fn on_cancel(&self, f: Option<fn(u64, u64)>) { pub fn on_cancel(&self, f: Option<Box<dyn FnMut(u64, u64)>>) {
if let Ok(mut cbs) = callbacks.lock() { if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1); let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb { if let Some(cb) = cb {
@ -623,7 +614,7 @@ impl Connection {
} }
} }
pub fn on_pause(&self, f: Option<fn(u64, u64)>) { pub fn on_pause(&self, f: Option<Box<dyn FnMut(u64, u64)>>) {
if let Ok(mut cbs) = callbacks.lock() { if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1); let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb { if let Some(cb) = cb {
@ -632,7 +623,7 @@ impl Connection {
} }
} }
pub fn on_resume(&self, f: Option<fn(u64, u64)>) { pub fn on_resume(&self, f: Option<Box<dyn FnMut(u64, u64)>>) {
if let Ok(mut cbs) = callbacks.lock() { if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1); let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb { if let Some(cb) = cb {
@ -641,7 +632,7 @@ impl Connection {
} }
} }
pub fn on_index_mark(&self, f: Option<fn(u64, u64, String)>) { pub fn on_index_mark(&self, f: Option<Box<dyn FnMut(u64, u64, String)>>) {
if let Ok(mut cbs) = callbacks.lock() { if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1); let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb { if let Some(cb) = cb {