11// Take a look at the license at the top of the repository in the LICENSE file.
22
3- use crate :: { translate:: * , ConvertError , Error , GStr , GString , Slice } ;
3+ use crate :: { translate:: * , ConvertError , Error , GString , IntoGStr , IntoOptionalGStr , Slice } ;
44use std:: { io, os:: raw:: c_char, path:: PathBuf , ptr} ;
55
66// rustdoc-stripper-ignore-next
@@ -35,24 +35,26 @@ impl CvtError {
3535#[ doc( alias = "g_convert" ) ]
3636pub fn convert (
3737 str_ : & [ u8 ] ,
38- to_codeset : & str ,
39- from_codeset : & str ,
38+ to_codeset : impl IntoGStr ,
39+ from_codeset : impl IntoGStr ,
4040) -> Result < ( Slice < u8 > , usize ) , CvtError > {
4141 assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
4242 let mut bytes_read = 0 ;
4343 let mut bytes_written = 0 ;
4444 let mut error = ptr:: null_mut ( ) ;
45- let result = unsafe {
46- ffi:: g_convert (
47- str_. as_ptr ( ) ,
48- str_. len ( ) as isize ,
49- to_codeset. to_glib_none ( ) . 0 ,
50- from_codeset. to_glib_none ( ) . 0 ,
51- & mut bytes_read,
52- & mut bytes_written,
53- & mut error,
54- )
55- } ;
45+ let result = to_codeset. run_with_gstr ( |to_codeset| {
46+ from_codeset. run_with_gstr ( |from_codeset| unsafe {
47+ ffi:: g_convert (
48+ str_. as_ptr ( ) ,
49+ str_. len ( ) as isize ,
50+ to_codeset. to_glib_none ( ) . 0 ,
51+ from_codeset. to_glib_none ( ) . 0 ,
52+ & mut bytes_read,
53+ & mut bytes_written,
54+ & mut error,
55+ )
56+ } )
57+ } ) ;
5658 if result. is_null ( ) {
5759 Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
5860 } else {
@@ -64,26 +66,30 @@ pub fn convert(
6466#[ doc( alias = "g_convert_with_fallback" ) ]
6567pub fn convert_with_fallback (
6668 str_ : & [ u8 ] ,
67- to_codeset : & str ,
68- from_codeset : & str ,
69- fallback : Option < & str > ,
69+ to_codeset : impl IntoGStr ,
70+ from_codeset : impl IntoGStr ,
71+ fallback : Option < impl IntoGStr > ,
7072) -> Result < ( Slice < u8 > , usize ) , CvtError > {
7173 assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
7274 let mut bytes_read = 0 ;
7375 let mut bytes_written = 0 ;
7476 let mut error = ptr:: null_mut ( ) ;
75- let result = unsafe {
76- ffi:: g_convert_with_fallback (
77- str_. as_ptr ( ) ,
78- str_. len ( ) as isize ,
79- to_codeset. to_glib_none ( ) . 0 ,
80- from_codeset. to_glib_none ( ) . 0 ,
81- fallback. to_glib_none ( ) . 0 ,
82- & mut bytes_read,
83- & mut bytes_written,
84- & mut error,
85- )
86- } ;
77+ let result = to_codeset. run_with_gstr ( |to_codeset| {
78+ from_codeset. run_with_gstr ( |from_codeset| {
79+ fallback. run_with_gstr ( |fallback| unsafe {
80+ ffi:: g_convert_with_fallback (
81+ str_. as_ptr ( ) ,
82+ str_. len ( ) as isize ,
83+ to_codeset. to_glib_none ( ) . 0 ,
84+ from_codeset. to_glib_none ( ) . 0 ,
85+ fallback. to_glib_none ( ) . 0 ,
86+ & mut bytes_read,
87+ & mut bytes_written,
88+ & mut error,
89+ )
90+ } )
91+ } )
92+ } ) ;
8793 if result. is_null ( ) {
8894 Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
8995 } else {
@@ -116,10 +122,12 @@ unsafe impl Send for IConv {}
116122impl IConv {
117123 #[ doc( alias = "g_iconv_open" ) ]
118124 #[ allow( clippy:: unnecessary_lazy_evaluations) ]
119- pub fn new ( to_codeset : & str , from_codeset : & str ) -> Option < Self > {
120- let iconv = unsafe {
121- ffi:: g_iconv_open ( to_codeset. to_glib_none ( ) . 0 , from_codeset. to_glib_none ( ) . 0 )
122- } ;
125+ pub fn new ( to_codeset : impl IntoGStr , from_codeset : impl IntoGStr ) -> Option < Self > {
126+ let iconv = to_codeset. run_with_gstr ( |to_codeset| {
127+ from_codeset. run_with_gstr ( |from_codeset| unsafe {
128+ ffi:: g_iconv_open ( to_codeset. to_glib_none ( ) . 0 , from_codeset. to_glib_none ( ) . 0 )
129+ } )
130+ } ) ;
123131 ( iconv as isize != -1 ) . then ( || Self ( iconv) )
124132 }
125133 #[ doc( alias = "g_convert_with_iconv" ) ]
@@ -208,20 +216,23 @@ pub fn filename_charsets() -> (bool, Vec<GString>) {
208216}
209217
210218#[ doc( alias = "g_filename_from_utf8" ) ]
211- pub fn filename_from_utf8 ( utf8string : & str ) -> Result < ( PathBuf , usize ) , CvtError > {
212- let len = utf8string. len ( ) as isize ;
219+ pub fn filename_from_utf8 ( utf8string : impl IntoGStr ) -> Result < ( PathBuf , usize ) , CvtError > {
213220 let mut bytes_read = 0 ;
214221 let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
215222 let mut error = ptr:: null_mut ( ) ;
216- let ret = unsafe {
217- ffi:: g_filename_from_utf8 (
218- utf8string. to_glib_none ( ) . 0 ,
219- len,
220- & mut bytes_read,
221- bytes_written. as_mut_ptr ( ) ,
222- & mut error,
223- )
224- } ;
223+ let ret = utf8string. run_with_gstr ( |utf8string| {
224+ assert ! ( utf8string. len( ) <= isize :: MAX as usize ) ;
225+ let len = utf8string. len ( ) as isize ;
226+ unsafe {
227+ ffi:: g_filename_from_utf8 (
228+ utf8string. to_glib_none ( ) . 0 ,
229+ len,
230+ & mut bytes_read,
231+ bytes_written. as_mut_ptr ( ) ,
232+ & mut error,
233+ )
234+ }
235+ } ) ;
225236 if error. is_null ( ) {
226237 Ok ( unsafe {
227238 (
@@ -264,20 +275,22 @@ pub fn filename_to_utf8(
264275}
265276
266277#[ doc( alias = "g_locale_from_utf8" ) ]
267- pub fn locale_from_utf8 ( utf8string : & GStr ) -> Result < ( Slice < u8 > , usize ) , CvtError > {
268- assert ! ( utf8string. len( ) <= isize :: MAX as usize ) ;
278+ pub fn locale_from_utf8 ( utf8string : impl IntoGStr ) -> Result < ( Slice < u8 > , usize ) , CvtError > {
269279 let mut bytes_read = 0 ;
270280 let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
271281 let mut error = ptr:: null_mut ( ) ;
272- let ret = unsafe {
273- ffi:: g_locale_from_utf8 (
274- utf8string. as_ptr ( ) ,
275- utf8string. len ( ) as isize ,
276- & mut bytes_read,
277- bytes_written. as_mut_ptr ( ) ,
278- & mut error,
279- )
280- } ;
282+ let ret = utf8string. run_with_gstr ( |utf8string| {
283+ assert ! ( utf8string. len( ) <= isize :: MAX as usize ) ;
284+ unsafe {
285+ ffi:: g_locale_from_utf8 (
286+ utf8string. as_ptr ( ) ,
287+ utf8string. len ( ) as isize ,
288+ & mut bytes_read,
289+ bytes_written. as_mut_ptr ( ) ,
290+ & mut error,
291+ )
292+ }
293+ } ) ;
281294 if error. is_null ( ) {
282295 Ok ( unsafe {
283296 (
@@ -324,7 +337,7 @@ mod tests {
324337 assert ! ( super :: convert( b"Hello" , "utf-8" , "ascii" ) . is_ok( ) ) ;
325338 assert ! ( super :: convert( b"He\xaa llo" , "utf-8" , "ascii" ) . is_err( ) ) ;
326339 assert_eq ! (
327- super :: convert_with_fallback( b"H\xc3 \xa9 llo" , "ascii" , "utf-8" , None )
340+ super :: convert_with_fallback( b"H\xc3 \xa9 llo" , "ascii" , "utf-8" , crate :: NONE_STR )
328341 . unwrap( )
329342 . 0
330343 . as_slice( ) ,
0 commit comments