Use Into<String> to work with any string type.

This commit is contained in:
Nolan Darilek 2018-06-13 01:35:51 +00:00
parent 7cf0efc833
commit 42b5ede300
2 changed files with 42 additions and 43 deletions

View File

@ -3,15 +3,15 @@ extern crate speech_dispatcher;
use speech_dispatcher::*; use speech_dispatcher::*;
fn main() { fn main() {
let connection = speech_dispatcher::Connection::open("hello_world".to_string(), "hello_world".to_string(), "hello_world".to_string(), Mode::Single); let connection = speech_dispatcher::Connection::open("hello_world", "hello_world", "hello_world", Mode::Single);
connection.say(Priority::Important, format!("Hello, world at rate {}.", connection.get_voice_rate())); connection.say(Priority::Important, format!("Hello, world at rate {}.", connection.get_voice_rate()));
connection.set_voice_rate(100); connection.set_voice_rate(100);
connection.say(Priority::Important, "This is faster.".to_string()); connection.say(Priority::Important, "This is faster.");
connection.set_voice_rate(0); connection.set_voice_rate(0);
connection.set_spelling(true); connection.set_spelling(true);
connection.say(Priority::Important, "This is spelled.".to_string()); connection.say(Priority::Important, "This is spelled.");
connection.set_spelling(false); connection.set_spelling(false);
connection.set_punctuation(Punctuation::All); connection.set_punctuation(Punctuation::All);
connection.say(Priority::Important, "This statement, unlike others, has punctuation that is spoken!".to_string()); connection.say(Priority::Important, "This statement, unlike others, has punctuation that is spoken!");
connection.set_punctuation(Punctuation::None); connection.set_punctuation(Punctuation::None);
} }

View File

@ -68,26 +68,26 @@ fn i32_to_bool(v: i32) -> bool {
impl Connection { impl Connection {
pub fn open(client_name: String, connection_name: String, user_name: String, mode: Mode) -> Connection { pub fn open<S: Into<String>>(client_name: S, connection_name: S, user_name: S, mode: Mode) -> Connection {
let connection = unsafe { let connection = unsafe {
spd_open( spd_open(
CString::new(client_name).unwrap().as_ptr(), CString::new(client_name.into()).unwrap().as_ptr(),
CString::new(connection_name).unwrap().as_ptr(), CString::new(connection_name.into()).unwrap().as_ptr(),
CString::new(user_name).unwrap().as_ptr(), CString::new(user_name.into()).unwrap().as_ptr(),
mode as u32 mode as u32
) )
}; };
Connection {connection: connection } Connection {connection: connection }
} }
pub fn open2(client_name: String, connection_name: String, user_name: String, mode: Mode, address: *mut Address, autospawn: bool) -> Connection { pub fn open2<S: Into<String>>(client_name: S, connection_name: S, user_name: S, mode: Mode, address: *mut Address, autospawn: bool) -> Connection {
let auto_spawn = if autospawn { 1 } else { 0 }; let auto_spawn = if autospawn { 1 } else { 0 };
let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr(); let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr();
let connection = unsafe { let connection = unsafe {
spd_open2( spd_open2(
CString::new(client_name).unwrap().as_ptr(), CString::new(client_name.into()).unwrap().as_ptr(),
CString::new(connection_name).unwrap().as_ptr(), CString::new(connection_name.into()).unwrap().as_ptr(),
CString::new(user_name).unwrap().as_ptr(), CString::new(user_name.into()).unwrap().as_ptr(),
mode as u32, mode as u32,
address, address,
auto_spawn, auto_spawn,
@ -101,23 +101,23 @@ impl Connection {
unsafe { spd_close(self.connection) }; unsafe { spd_close(self.connection) };
} }
pub fn say(&self, priority: Priority, text: String) -> bool { pub fn say<S: Into<String>>(&self, priority: Priority, text: S) -> bool {
let v = unsafe { let v = unsafe {
spd_say( spd_say(
self.connection, self.connection,
priority as u32, priority as u32,
CString::new(text).unwrap().as_ptr() CString::new(text.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn sayf(&self, priority: Priority, format: String) -> bool { pub fn sayf<S: Into<String>>(&self, priority: Priority, format: S) -> bool {
let v = unsafe { let v = unsafe {
spd_sayf( spd_sayf(
self.connection, self.connection,
priority as u32, priority as u32,
CString::new(format).unwrap().as_ptr() CString::new(format.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
@ -183,23 +183,23 @@ impl Connection {
i32_to_bool(v) i32_to_bool(v)
} }
pub fn key(&self, priority: Priority, key_name: String) -> bool { pub fn key<S: Into<String>>(&self, priority: Priority, key_name: S) -> bool {
let v = unsafe { let v = unsafe {
spd_key( spd_key(
self.connection, self.connection,
priority as u32, priority as u32,
CString::new(key_name).unwrap().as_ptr() CString::new(key_name.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn char(&self, priority: Priority, char: String) -> bool { pub fn char<S: Into<String>>(&self, priority: Priority, char: S) -> bool {
let v = unsafe { let v = unsafe {
spd_char( spd_char(
self.connection, self.connection,
priority as u32, priority as u32,
CString::new(char).unwrap().as_ptr() CString::new(char.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
@ -216,12 +216,12 @@ impl Connection {
i32_to_bool(v) i32_to_bool(v)
} }
pub fn sound_icon(&self, priority: Priority, icon_name: String) -> bool { pub fn sound_icon<S: Into<String>>(&self, priority: Priority, icon_name: S) -> bool {
let v = unsafe { let v = unsafe {
spd_char( spd_char(
self.connection, self.connection,
priority as u32, priority as u32,
CString::new(icon_name).unwrap().as_ptr() CString::new(icon_name.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
@ -257,31 +257,31 @@ impl Connection {
} }
} }
pub fn set_synthesis_voice(&self, voice_name: String) -> bool { pub fn set_synthesis_voice<S: Into<String>>(&self, voice_name: S) -> bool {
let v = unsafe { let v = unsafe {
spd_set_synthesis_voice( spd_set_synthesis_voice(
self.connection, self.connection,
CString::new(voice_name).unwrap().as_ptr() CString::new(voice_name.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn set_synthesis_voice_all(&self, voice_name: String) -> bool { pub fn set_synthesis_voice_all<S: Into<String>>(&self, voice_name: S) -> bool {
let v = unsafe { let v = unsafe {
spd_set_synthesis_voice_all( spd_set_synthesis_voice_all(
self.connection, self.connection,
CString::new(voice_name).unwrap().as_ptr() CString::new(voice_name.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn set_synthesis_voice_uid(&self, voice_name: String, target_uid: u32) -> bool { pub fn set_synthesis_voice_uid<S: Into<String>>(&self, voice_name: S, target_uid: u32) -> bool {
let v = unsafe { let v = unsafe {
spd_set_synthesis_voice_uid( spd_set_synthesis_voice_uid(
self.connection, self.connection,
CString::new(voice_name).unwrap().as_ptr(), CString::new(voice_name.into()).unwrap().as_ptr(),
target_uid target_uid
) )
}; };
@ -318,12 +318,12 @@ impl Connection {
i32_to_bool(v) i32_to_bool(v)
} }
pub fn set_notification(&self, notification: Notification, state: String) -> bool { pub fn set_notification<S: Into<String>>(&self, notification: Notification, state: S) -> bool {
let v = unsafe { let v = unsafe {
spd_set_notification( spd_set_notification(
self.connection, self.connection,
notification as u32, notification as u32,
CString::new(state).unwrap().as_ptr() CString::new(state.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
@ -434,31 +434,31 @@ impl Connection {
i32_to_bool(v) i32_to_bool(v)
} }
pub fn set_language(&self, language: String) -> bool { pub fn set_language<S: Into<String>>(&self, language: S) -> bool {
let v = unsafe { let v = unsafe {
spd_set_language( spd_set_language(
self.connection, self.connection,
CString::new(language).unwrap().as_ptr() CString::new(language.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn set_language_all(&self, language: String) -> bool { pub fn set_language_all<S: Into<String>>(&self, language: S) -> bool {
let v = unsafe { let v = unsafe {
spd_set_language_all( spd_set_language_all(
self.connection, self.connection,
CString::new(language).unwrap().as_ptr() CString::new(language.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn set_language_uid(&self, language: String, target_uid: u32) -> bool { pub fn set_language_uid<S: Into<String>>(&self, language: S, target_uid: u32) -> bool {
let v = unsafe { let v = unsafe {
spd_set_language_uid( spd_set_language_uid(
self.connection, self.connection,
CString::new(language).unwrap().as_ptr(), CString::new(language.into()).unwrap().as_ptr(),
target_uid target_uid
) )
}; };
@ -470,38 +470,37 @@ impl Connection {
v.to_str().unwrap() v.to_str().unwrap()
} }
pub fn set_output_module(&self, output_module: String) -> bool { pub fn set_output_module<S: Into<String>>(&self, output_module: S) -> bool {
let v = unsafe { let v = unsafe {
spd_set_output_module( spd_set_output_module(
self.connection, self.connection,
CString::new(output_module).unwrap().as_ptr() CString::new(output_module.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn set_output_module_all(&self, output_module: String) -> bool { pub fn set_output_module_all<S: Into<String>>(&self, output_module: S) -> bool {
let v = unsafe { let v = unsafe {
spd_set_output_module_all( spd_set_output_module_all(
self.connection, self.connection,
CString::new(output_module).unwrap().as_ptr() CString::new(output_module.into()).unwrap().as_ptr()
) )
}; };
i32_to_bool(v) i32_to_bool(v)
} }
pub fn set_output_module_uid(&self, output_module: String, target_uid: u32) -> bool { pub fn set_output_module_uid<S: Into<String>>(&self, output_module: S, target_uid: u32) -> bool {
let v = unsafe { let v = unsafe {
spd_set_output_module_uid( spd_set_output_module_uid(
self.connection, self.connection,
CString::new(output_module).unwrap().as_ptr(), CString::new(output_module.into()).unwrap().as_ptr(),
target_uid target_uid
) )
}; };
i32_to_bool(v) i32_to_bool(v)
} }
} }
impl Drop for Connection { impl Drop for Connection {