22
33use std:: { io, os:: raw:: c_char, path:: PathBuf , ptr} ;
44
5- use crate :: { translate:: * , ConvertError , Error , GStr , GString , Slice } ;
5+ use crate :: { translate:: * , ConvertError , Error , GString , IntoGStr , IntoOptionalGStr , Slice } ;
66
77// rustdoc-stripper-ignore-next
88/// A wrapper for [`ConvertError`](crate::ConvertError) that can hold an offset into the input
@@ -36,24 +36,26 @@ impl CvtError {
3636#[ doc( alias = "g_convert" ) ]
3737pub fn convert (
3838 str_ : & [ u8 ] ,
39- to_codeset : & str ,
40- from_codeset : & str ,
39+ to_codeset : impl IntoGStr ,
40+ from_codeset : impl IntoGStr ,
4141) -> Result < ( Slice < u8 > , usize ) , CvtError > {
4242 assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
4343 let mut bytes_read = 0 ;
4444 let mut bytes_written = 0 ;
4545 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- } ;
46+ let result = to_codeset. run_with_gstr ( |to_codeset| {
47+ from_codeset. run_with_gstr ( |from_codeset| unsafe {
48+ ffi:: g_convert (
49+ str_. as_ptr ( ) ,
50+ str_. len ( ) as isize ,
51+ to_codeset. to_glib_none ( ) . 0 ,
52+ from_codeset. to_glib_none ( ) . 0 ,
53+ & mut bytes_read,
54+ & mut bytes_written,
55+ & mut error,
56+ )
57+ } )
58+ } ) ;
5759 if result. is_null ( ) {
5860 Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
5961 } else {
@@ -65,26 +67,30 @@ pub fn convert(
6567#[ doc( alias = "g_convert_with_fallback" ) ]
6668pub fn convert_with_fallback (
6769 str_ : & [ u8 ] ,
68- to_codeset : & str ,
69- from_codeset : & str ,
70- fallback : Option < & str > ,
70+ to_codeset : impl IntoGStr ,
71+ from_codeset : impl IntoGStr ,
72+ fallback : Option < impl IntoGStr > ,
7173) -> Result < ( Slice < u8 > , usize ) , CvtError > {
7274 assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
7375 let mut bytes_read = 0 ;
7476 let mut bytes_written = 0 ;
7577 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- } ;
78+ let result = to_codeset. run_with_gstr ( |to_codeset| {
79+ from_codeset. run_with_gstr ( |from_codeset| {
80+ fallback. run_with_gstr ( |fallback| unsafe {
81+ ffi:: g_convert_with_fallback (
82+ str_. as_ptr ( ) ,
83+ str_. len ( ) as isize ,
84+ to_codeset. to_glib_none ( ) . 0 ,
85+ from_codeset. to_glib_none ( ) . 0 ,
86+ fallback. to_glib_none ( ) . 0 ,
87+ & mut bytes_read,
88+ & mut bytes_written,
89+ & mut error,
90+ )
91+ } )
92+ } )
93+ } ) ;
8894 if result. is_null ( ) {
8995 Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
9096 } else {
@@ -117,10 +123,12 @@ unsafe impl Send for IConv {}
117123impl IConv {
118124 #[ doc( alias = "g_iconv_open" ) ]
119125 #[ 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- } ;
126+ pub fn new ( to_codeset : impl IntoGStr , from_codeset : impl IntoGStr ) -> Option < Self > {
127+ let iconv = to_codeset. run_with_gstr ( |to_codeset| {
128+ from_codeset. run_with_gstr ( |from_codeset| unsafe {
129+ ffi:: g_iconv_open ( to_codeset. to_glib_none ( ) . 0 , from_codeset. to_glib_none ( ) . 0 )
130+ } )
131+ } ) ;
124132 ( iconv as isize != -1 ) . then ( || Self ( iconv) )
125133 }
126134 #[ doc( alias = "g_convert_with_iconv" ) ]
@@ -209,20 +217,23 @@ pub fn filename_charsets() -> (bool, Vec<GString>) {
209217}
210218
211219#[ 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 ;
220+ pub fn filename_from_utf8 ( utf8string : impl IntoGStr ) -> Result < ( PathBuf , usize ) , CvtError > {
214221 let mut bytes_read = 0 ;
215222 let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
216223 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- } ;
224+ let ret = utf8string. run_with_gstr ( |utf8string| {
225+ assert ! ( utf8string. len( ) <= isize :: MAX as usize ) ;
226+ let len = utf8string. len ( ) as isize ;
227+ unsafe {
228+ ffi:: g_filename_from_utf8 (
229+ utf8string. to_glib_none ( ) . 0 ,
230+ len,
231+ & mut bytes_read,
232+ bytes_written. as_mut_ptr ( ) ,
233+ & mut error,
234+ )
235+ }
236+ } ) ;
226237 if error. is_null ( ) {
227238 Ok ( unsafe {
228239 (
@@ -265,20 +276,22 @@ pub fn filename_to_utf8(
265276}
266277
267278#[ 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 ) ;
279+ pub fn locale_from_utf8 ( utf8string : impl IntoGStr ) -> Result < ( Slice < u8 > , usize ) , CvtError > {
270280 let mut bytes_read = 0 ;
271281 let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
272282 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- } ;
283+ let ret = utf8string. run_with_gstr ( |utf8string| {
284+ assert ! ( utf8string. len( ) <= isize :: MAX as usize ) ;
285+ unsafe {
286+ ffi:: g_locale_from_utf8 (
287+ utf8string. as_ptr ( ) ,
288+ utf8string. len ( ) as isize ,
289+ & mut bytes_read,
290+ bytes_written. as_mut_ptr ( ) ,
291+ & mut error,
292+ )
293+ }
294+ } ) ;
282295 if error. is_null ( ) {
283296 Ok ( unsafe {
284297 (
@@ -325,7 +338,7 @@ mod tests {
325338 assert ! ( super :: convert( b"Hello" , "utf-8" , "ascii" ) . is_ok( ) ) ;
326339 assert ! ( super :: convert( b"He\xaa llo" , "utf-8" , "ascii" ) . is_err( ) ) ;
327340 assert_eq ! (
328- super :: convert_with_fallback( b"H\xc3 \xa9 llo" , "ascii" , "utf-8" , None )
341+ super :: convert_with_fallback( b"H\xc3 \xa9 llo" , "ascii" , "utf-8" , crate :: NONE_STR )
329342 . unwrap( )
330343 . 0
331344 . as_slice( ) ,
0 commit comments