22
33use std:: { io, os:: raw:: c_char, path:: PathBuf , ptr} ;
44
5- use crate :: { translate:: * , ConvertError , Error , GStr , GString , NormalizeMode , Slice } ;
5+ use crate :: {
6+ translate:: * , ConvertError , Error , GString , IntoGStr , IntoOptionalGStr , NormalizeMode , Slice ,
7+ } ;
68
79// rustdoc-stripper-ignore-next
810/// A wrapper for [`ConvertError`](crate::ConvertError) that can hold an offset into the input
@@ -36,24 +38,26 @@ impl CvtError {
3638#[ doc( alias = "g_convert" ) ]
3739pub fn convert (
3840 str_ : & [ u8 ] ,
39- to_codeset : & str ,
40- from_codeset : & str ,
41+ to_codeset : impl IntoGStr ,
42+ from_codeset : impl IntoGStr ,
4143) -> Result < ( Slice < u8 > , usize ) , CvtError > {
4244 assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
4345 let mut bytes_read = 0 ;
4446 let mut bytes_written = 0 ;
4547 let mut error = ptr:: null_mut ( ) ;
46- let result = 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- } ;
48+ let result = to_codeset. run_with_gstr ( |to_codeset| {
49+ from_codeset. run_with_gstr ( |from_codeset| unsafe {
50+ ffi:: g_convert (
51+ str_. as_ptr ( ) ,
52+ str_. len ( ) as isize ,
53+ to_codeset. to_glib_none ( ) . 0 ,
54+ from_codeset. to_glib_none ( ) . 0 ,
55+ & mut bytes_read,
56+ & mut bytes_written,
57+ & mut error,
58+ )
59+ } )
60+ } ) ;
5761 if result. is_null ( ) {
5862 Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
5963 } else {
@@ -65,26 +69,30 @@ pub fn convert(
6569#[ doc( alias = "g_convert_with_fallback" ) ]
6670pub fn convert_with_fallback (
6771 str_ : & [ u8 ] ,
68- to_codeset : & str ,
69- from_codeset : & str ,
70- fallback : Option < & str > ,
72+ to_codeset : impl IntoGStr ,
73+ from_codeset : impl IntoGStr ,
74+ fallback : Option < impl IntoGStr > ,
7175) -> Result < ( Slice < u8 > , usize ) , CvtError > {
7276 assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
7377 let mut bytes_read = 0 ;
7478 let mut bytes_written = 0 ;
7579 let mut error = ptr:: null_mut ( ) ;
76- let result = unsafe {
77- ffi:: g_convert_with_fallback (
78- str_. as_ptr ( ) ,
79- str_. len ( ) as isize ,
80- to_codeset. to_glib_none ( ) . 0 ,
81- from_codeset. to_glib_none ( ) . 0 ,
82- fallback. to_glib_none ( ) . 0 ,
83- & mut bytes_read,
84- & mut bytes_written,
85- & mut error,
86- )
87- } ;
80+ let result = to_codeset. run_with_gstr ( |to_codeset| {
81+ from_codeset. run_with_gstr ( |from_codeset| {
82+ fallback. run_with_gstr ( |fallback| unsafe {
83+ ffi:: g_convert_with_fallback (
84+ str_. as_ptr ( ) ,
85+ str_. len ( ) as isize ,
86+ to_codeset. to_glib_none ( ) . 0 ,
87+ from_codeset. to_glib_none ( ) . 0 ,
88+ fallback. to_glib_none ( ) . 0 ,
89+ & mut bytes_read,
90+ & mut bytes_written,
91+ & mut error,
92+ )
93+ } )
94+ } )
95+ } ) ;
8896 if result. is_null ( ) {
8997 Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
9098 } else {
@@ -117,10 +125,12 @@ unsafe impl Send for IConv {}
117125impl IConv {
118126 #[ doc( alias = "g_iconv_open" ) ]
119127 #[ allow( clippy:: unnecessary_lazy_evaluations) ]
120- pub fn new ( to_codeset : & str , from_codeset : & str ) -> Option < Self > {
121- let iconv = unsafe {
122- ffi:: g_iconv_open ( to_codeset. to_glib_none ( ) . 0 , from_codeset. to_glib_none ( ) . 0 )
123- } ;
128+ pub fn new ( to_codeset : impl IntoGStr , from_codeset : impl IntoGStr ) -> Option < Self > {
129+ let iconv = to_codeset. run_with_gstr ( |to_codeset| {
130+ from_codeset. run_with_gstr ( |from_codeset| unsafe {
131+ ffi:: g_iconv_open ( to_codeset. to_glib_none ( ) . 0 , from_codeset. to_glib_none ( ) . 0 )
132+ } )
133+ } ) ;
124134 ( iconv as isize != -1 ) . then ( || Self ( iconv) )
125135 }
126136 #[ doc( alias = "g_convert_with_iconv" ) ]
@@ -209,20 +219,23 @@ pub fn filename_charsets() -> (bool, Vec<GString>) {
209219}
210220
211221#[ doc( alias = "g_filename_from_utf8" ) ]
212- pub fn filename_from_utf8 ( utf8string : & str ) -> Result < ( PathBuf , usize ) , CvtError > {
213- let len = utf8string. len ( ) as isize ;
222+ pub fn filename_from_utf8 ( utf8string : impl IntoGStr ) -> Result < ( PathBuf , usize ) , CvtError > {
214223 let mut bytes_read = 0 ;
215224 let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
216225 let mut error = ptr:: null_mut ( ) ;
217- let ret = unsafe {
218- ffi:: g_filename_from_utf8 (
219- utf8string. to_glib_none ( ) . 0 ,
220- len,
221- & mut bytes_read,
222- bytes_written. as_mut_ptr ( ) ,
223- & mut error,
224- )
225- } ;
226+ let ret = utf8string. run_with_gstr ( |utf8string| {
227+ assert ! ( utf8string. len( ) <= isize :: MAX as usize ) ;
228+ let len = utf8string. len ( ) as isize ;
229+ unsafe {
230+ ffi:: g_filename_from_utf8 (
231+ utf8string. to_glib_none ( ) . 0 ,
232+ len,
233+ & mut bytes_read,
234+ bytes_written. as_mut_ptr ( ) ,
235+ & mut error,
236+ )
237+ }
238+ } ) ;
226239 if error. is_null ( ) {
227240 Ok ( unsafe {
228241 (
@@ -265,20 +278,22 @@ pub fn filename_to_utf8(
265278}
266279
267280#[ doc( alias = "g_locale_from_utf8" ) ]
268- pub fn locale_from_utf8 ( utf8string : & GStr ) -> Result < ( Slice < u8 > , usize ) , CvtError > {
269- assert ! ( utf8string. len( ) <= isize :: MAX as usize ) ;
281+ pub fn locale_from_utf8 ( utf8string : impl IntoGStr ) -> Result < ( Slice < u8 > , usize ) , CvtError > {
270282 let mut bytes_read = 0 ;
271283 let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
272284 let mut error = ptr:: null_mut ( ) ;
273- let ret = unsafe {
274- ffi:: g_locale_from_utf8 (
275- utf8string. as_ptr ( ) ,
276- utf8string. len ( ) as isize ,
277- & mut bytes_read,
278- bytes_written. as_mut_ptr ( ) ,
279- & mut error,
280- )
281- } ;
285+ let ret = utf8string. run_with_gstr ( |utf8string| {
286+ assert ! ( utf8string. len( ) <= isize :: MAX as usize ) ;
287+ unsafe {
288+ ffi:: g_locale_from_utf8 (
289+ utf8string. as_ptr ( ) ,
290+ utf8string. len ( ) as isize ,
291+ & mut bytes_read,
292+ bytes_written. as_mut_ptr ( ) ,
293+ & mut error,
294+ )
295+ }
296+ } ) ;
282297 if error. is_null ( ) {
283298 Ok ( unsafe {
284299 (
@@ -393,7 +408,7 @@ mod tests {
393408 assert ! ( super :: convert( b"Hello" , "utf-8" , "ascii" ) . is_ok( ) ) ;
394409 assert ! ( super :: convert( b"He\xaa llo" , "utf-8" , "ascii" ) . is_err( ) ) ;
395410 assert_eq ! (
396- super :: convert_with_fallback( b"H\xc3 \xa9 llo" , "ascii" , "utf-8" , None )
411+ super :: convert_with_fallback( b"H\xc3 \xa9 llo" , "ascii" , "utf-8" , crate :: NONE_STR )
397412 . unwrap( )
398413 . 0
399414 . as_slice( ) ,
0 commit comments