File tree Expand file tree Collapse file tree 1 file changed +20
-8
lines changed Expand file tree Collapse file tree 1 file changed +20
-8
lines changed Original file line number Diff line number Diff line change 22
33use std:: { ffi:: CString , fmt:: Debug , path:: Path } ;
44
5+ #[ cfg( not( target_family = "windows" ) ) ]
6+ use std:: os:: unix:: ffi:: OsStrExt ;
7+ #[ cfg( target_family = "windows" ) ]
8+ use std:: os:: windows:: ffi:: OsStrExt ;
9+
510#[ cfg( feature = "model-fetching" ) ]
611use std:: env;
712
@@ -170,14 +175,21 @@ impl SessionBuilder {
170175 filename : model_filepath. to_path_buf ( ) ,
171176 } ) ;
172177 }
173- let model_path: CString =
174- CString :: new (
175- model_filepath
176- . to_str ( )
177- . ok_or_else ( || OrtError :: NonUtf8Path {
178- path : model_filepath. to_path_buf ( ) ,
179- } ) ?,
180- ) ?;
178+
179+ // Build an OsString than a vector of bytes to pass to C
180+ let model_path = std:: ffi:: OsString :: from ( model_filepath) ;
181+ #[ cfg( target_family = "windows" ) ]
182+ let model_path: Vec < u16 > = model_path
183+ . encode_wide ( )
184+ . chain ( std:: iter:: once ( 0 ) ) // Make sure we have a null terminated string
185+ . collect ( ) ;
186+ #[ cfg( not( target_family = "windows" ) ) ]
187+ let model_path: Vec < std:: os:: raw:: c_char > = model_path
188+ . as_bytes ( )
189+ . iter ( )
190+ . chain ( std:: iter:: once ( & b'\0' ) ) // Make sure we have a null terminated string
191+ . map ( |b| * b as std:: os:: raw:: c_char )
192+ . collect ( ) ;
181193
182194 let env_ptr: * const sys:: OrtEnv = self . env . env_ptr ( ) ;
183195
You can’t perform that action at this time.
0 commit comments