File tree Expand file tree Collapse file tree 3 files changed +14
-47
lines changed
Expand file tree Collapse file tree 3 files changed +14
-47
lines changed Original file line number Diff line number Diff line change @@ -98,8 +98,8 @@ pub struct OfflineRecognizerResult {
9898
9999impl OfflineRecognizerResult {
100100 fn new ( result : & sherpa_rs_sys:: SherpaOnnxOfflineRecognizerResult ) -> Self {
101- let lang = cstr_to_string ( result. lang ) ;
102- let text = cstr_to_string ( result. text ) ;
101+ let lang = unsafe { cstr_to_string ( result. lang ) } ;
102+ let text = unsafe { cstr_to_string ( result. text ) } ;
103103 let count = result. count . try_into ( ) . unwrap ( ) ;
104104 let timestamps = if result. timestamps . is_null ( ) {
105105 Vec :: new ( )
Original file line number Diff line number Diff line change @@ -2,13 +2,15 @@ mod kokoro;
22mod matcha;
33mod vits;
44
5+ use std:: ffi:: CString ;
6+
57use eyre:: { bail, Result } ;
68
79pub use kokoro:: { KokoroTts , KokoroTtsConfig } ;
810pub use matcha:: { MatchaTts , MatchaTtsConfig } ;
911pub use vits:: { VitsTts , VitsTtsConfig } ;
1012
11- use crate :: utils:: { cstring_from_str, RawCStr } ;
13+ use crate :: utils:: cstring_from_str;
1214
1315#[ derive( Debug ) ]
1416pub struct TtsAudio {
@@ -26,8 +28,8 @@ pub struct CommonTtsConfig {
2628}
2729
2830pub struct CommonTtsRaw {
29- pub rule_fars : Option < RawCStr > ,
30- pub rule_fsts : Option < RawCStr > ,
31+ pub rule_fars : Option < CString > ,
32+ pub rule_fsts : Option < CString > ,
3133 pub max_num_sentences : i32 ,
3234}
3335
Original file line number Diff line number Diff line change 11use std:: ffi:: { c_char, CString } ;
22
3- pub fn cstring_from_str ( s : & str ) -> RawCStr {
4- RawCStr :: new ( s)
3+ pub fn cstring_from_str ( s : & str ) -> CString {
4+ CString :: new ( s) . expect ( "CString::new failed" )
55}
66
7- pub fn cstr_to_string ( ptr : * const c_char ) -> String {
8- unsafe {
9- if ptr. is_null ( ) {
10- String :: new ( )
11- } else {
12- std:: ffi:: CStr :: from_ptr ( ptr) . to_string_lossy ( ) . into_owned ( )
13- }
14- }
15- }
16-
17- pub struct RawCStr {
18- ptr : * mut std:: ffi:: c_char ,
19- }
20-
21- impl RawCStr {
22- /// Creates a new `CStr` from a given Rust string slice.
23- pub fn new ( s : & str ) -> Self {
24- let cstr = CString :: new ( s) . expect ( "CString::new failed" ) ;
25- let ptr = cstr. into_raw ( ) ;
26- Self { ptr }
27- }
28-
29- /// Returns the raw pointer to the internal C string.
30- ///
31- /// # Safety
32- /// This function only returns the raw pointer and does not transfer ownership.
33- /// The pointer remains valid as long as the `CStr` instance exists.
34- /// Be cautious not to deallocate or modify the pointer after using `CStr::new`.
35- pub fn as_ptr ( & self ) -> * const c_char {
36- self . ptr
37- }
38- }
39-
40- impl Drop for RawCStr {
41- fn drop ( & mut self ) {
42- if !self . ptr . is_null ( ) {
43- unsafe {
44- let _ = CString :: from_raw ( self . ptr ) ;
45- }
46- }
7+ pub unsafe fn cstr_to_string ( ptr : * const c_char ) -> String {
8+ if ptr. is_null ( ) {
9+ String :: new ( )
10+ } else {
11+ std:: ffi:: CStr :: from_ptr ( ptr) . to_string_lossy ( ) . into_owned ( )
4712 }
4813}
You can’t perform that action at this time.
0 commit comments