From 42b5ede3003766c3ce87bd0de1607bd49025ac45 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 13 Jun 2018 01:35:51 +0000 Subject: [PATCH] Use Into to work with any string type. --- examples/hello_world.rs | 8 ++--- src/lib.rs | 77 ++++++++++++++++++++--------------------- 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 8285957..34acdb5 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -3,15 +3,15 @@ extern crate speech_dispatcher; use speech_dispatcher::*; 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.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_spelling(true); - connection.say(Priority::Important, "This is spelled.".to_string()); + connection.say(Priority::Important, "This is spelled."); connection.set_spelling(false); 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); } diff --git a/src/lib.rs b/src/lib.rs index 5817a9c..8513726 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,26 +68,26 @@ fn i32_to_bool(v: i32) -> bool { impl Connection { - pub fn open(client_name: String, connection_name: String, user_name: String, mode: Mode) -> Connection { + pub fn open>(client_name: S, connection_name: S, user_name: S, mode: Mode) -> Connection { let connection = unsafe { spd_open( - CString::new(client_name).unwrap().as_ptr(), - CString::new(connection_name).unwrap().as_ptr(), - CString::new(user_name).unwrap().as_ptr(), + CString::new(client_name.into()).unwrap().as_ptr(), + CString::new(connection_name.into()).unwrap().as_ptr(), + CString::new(user_name.into()).unwrap().as_ptr(), mode as u32 ) }; 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>(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 error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr(); let connection = unsafe { spd_open2( - CString::new(client_name).unwrap().as_ptr(), - CString::new(connection_name).unwrap().as_ptr(), - CString::new(user_name).unwrap().as_ptr(), + CString::new(client_name.into()).unwrap().as_ptr(), + CString::new(connection_name.into()).unwrap().as_ptr(), + CString::new(user_name.into()).unwrap().as_ptr(), mode as u32, address, auto_spawn, @@ -101,23 +101,23 @@ impl Connection { unsafe { spd_close(self.connection) }; } - pub fn say(&self, priority: Priority, text: String) -> bool { + pub fn say>(&self, priority: Priority, text: S) -> bool { let v = unsafe { spd_say( self.connection, priority as u32, - CString::new(text).unwrap().as_ptr() + CString::new(text.into()).unwrap().as_ptr() ) }; i32_to_bool(v) } - pub fn sayf(&self, priority: Priority, format: String) -> bool { + pub fn sayf>(&self, priority: Priority, format: S) -> bool { let v = unsafe { spd_sayf( self.connection, priority as u32, - CString::new(format).unwrap().as_ptr() + CString::new(format.into()).unwrap().as_ptr() ) }; i32_to_bool(v) @@ -183,23 +183,23 @@ impl Connection { i32_to_bool(v) } - pub fn key(&self, priority: Priority, key_name: String) -> bool { + pub fn key>(&self, priority: Priority, key_name: S) -> bool { let v = unsafe { spd_key( self.connection, priority as u32, - CString::new(key_name).unwrap().as_ptr() + CString::new(key_name.into()).unwrap().as_ptr() ) }; i32_to_bool(v) } - pub fn char(&self, priority: Priority, char: String) -> bool { + pub fn char>(&self, priority: Priority, char: S) -> bool { let v = unsafe { spd_char( self.connection, priority as u32, - CString::new(char).unwrap().as_ptr() + CString::new(char.into()).unwrap().as_ptr() ) }; i32_to_bool(v) @@ -216,12 +216,12 @@ impl Connection { i32_to_bool(v) } - pub fn sound_icon(&self, priority: Priority, icon_name: String) -> bool { + pub fn sound_icon>(&self, priority: Priority, icon_name: S) -> bool { let v = unsafe { spd_char( self.connection, priority as u32, - CString::new(icon_name).unwrap().as_ptr() + CString::new(icon_name.into()).unwrap().as_ptr() ) }; 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>(&self, voice_name: S) -> bool { let v = unsafe { spd_set_synthesis_voice( self.connection, - CString::new(voice_name).unwrap().as_ptr() + CString::new(voice_name.into()).unwrap().as_ptr() ) }; i32_to_bool(v) } - pub fn set_synthesis_voice_all(&self, voice_name: String) -> bool { + pub fn set_synthesis_voice_all>(&self, voice_name: S) -> bool { let v = unsafe { spd_set_synthesis_voice_all( self.connection, - CString::new(voice_name).unwrap().as_ptr() + CString::new(voice_name.into()).unwrap().as_ptr() ) }; i32_to_bool(v) } - pub fn set_synthesis_voice_uid(&self, voice_name: String, target_uid: u32) -> bool { + pub fn set_synthesis_voice_uid>(&self, voice_name: S, target_uid: u32) -> bool { let v = unsafe { spd_set_synthesis_voice_uid( self.connection, - CString::new(voice_name).unwrap().as_ptr(), + CString::new(voice_name.into()).unwrap().as_ptr(), target_uid ) }; @@ -318,12 +318,12 @@ impl Connection { i32_to_bool(v) } - pub fn set_notification(&self, notification: Notification, state: String) -> bool { + pub fn set_notification>(&self, notification: Notification, state: S) -> bool { let v = unsafe { spd_set_notification( self.connection, notification as u32, - CString::new(state).unwrap().as_ptr() + CString::new(state.into()).unwrap().as_ptr() ) }; i32_to_bool(v) @@ -434,31 +434,31 @@ impl Connection { i32_to_bool(v) } - pub fn set_language(&self, language: String) -> bool { + pub fn set_language>(&self, language: S) -> bool { let v = unsafe { spd_set_language( self.connection, - CString::new(language).unwrap().as_ptr() + CString::new(language.into()).unwrap().as_ptr() ) }; i32_to_bool(v) } - pub fn set_language_all(&self, language: String) -> bool { + pub fn set_language_all>(&self, language: S) -> bool { let v = unsafe { spd_set_language_all( self.connection, - CString::new(language).unwrap().as_ptr() + CString::new(language.into()).unwrap().as_ptr() ) }; i32_to_bool(v) } - pub fn set_language_uid(&self, language: String, target_uid: u32) -> bool { + pub fn set_language_uid>(&self, language: S, target_uid: u32) -> bool { let v = unsafe { spd_set_language_uid( self.connection, - CString::new(language).unwrap().as_ptr(), + CString::new(language.into()).unwrap().as_ptr(), target_uid ) }; @@ -470,38 +470,37 @@ impl Connection { v.to_str().unwrap() } - pub fn set_output_module(&self, output_module: String) -> bool { + pub fn set_output_module>(&self, output_module: S) -> bool { let v = unsafe { spd_set_output_module( self.connection, - CString::new(output_module).unwrap().as_ptr() + CString::new(output_module.into()).unwrap().as_ptr() ) }; i32_to_bool(v) } - pub fn set_output_module_all(&self, output_module: String) -> bool { + pub fn set_output_module_all>(&self, output_module: S) -> bool { let v = unsafe { spd_set_output_module_all( self.connection, - CString::new(output_module).unwrap().as_ptr() + CString::new(output_module.into()).unwrap().as_ptr() ) }; i32_to_bool(v) } - pub fn set_output_module_uid(&self, output_module: String, target_uid: u32) -> bool { + pub fn set_output_module_uid>(&self, output_module: S, target_uid: u32) -> bool { let v = unsafe { spd_set_output_module_uid( self.connection, - CString::new(output_module).unwrap().as_ptr(), + CString::new(output_module.into()).unwrap().as_ptr(), target_uid ) }; i32_to_bool(v) } - } impl Drop for Connection {