2
2
3
3
use std:: { io, os:: raw:: c_char, path:: PathBuf , ptr} ;
4
4
5
- use crate :: { translate:: * , ConvertError , Error , GStr , GString , Slice } ;
5
+ use crate :: { translate:: * , ConvertError , Error , GString , IntoGStr , IntoOptionalGStr , Slice } ;
6
6
7
7
// rustdoc-stripper-ignore-next
8
8
/// A wrapper for [`ConvertError`](crate::ConvertError) that can hold an offset into the input
@@ -36,24 +36,26 @@ impl CvtError {
36
36
#[ doc( alias = "g_convert" ) ]
37
37
pub fn convert (
38
38
str_ : & [ u8 ] ,
39
- to_codeset : & str ,
40
- from_codeset : & str ,
39
+ to_codeset : impl IntoGStr ,
40
+ from_codeset : impl IntoGStr ,
41
41
) -> Result < ( Slice < u8 > , usize ) , CvtError > {
42
42
assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
43
43
let mut bytes_read = 0 ;
44
44
let mut bytes_written = 0 ;
45
45
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
+ } ) ;
57
59
if result. is_null ( ) {
58
60
Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
59
61
} else {
@@ -65,26 +67,30 @@ pub fn convert(
65
67
#[ doc( alias = "g_convert_with_fallback" ) ]
66
68
pub fn convert_with_fallback (
67
69
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 > ,
71
73
) -> Result < ( Slice < u8 > , usize ) , CvtError > {
72
74
assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
73
75
let mut bytes_read = 0 ;
74
76
let mut bytes_written = 0 ;
75
77
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
+ } ) ;
88
94
if result. is_null ( ) {
89
95
Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
90
96
} else {
@@ -117,10 +123,12 @@ unsafe impl Send for IConv {}
117
123
impl IConv {
118
124
#[ doc( alias = "g_iconv_open" ) ]
119
125
#[ 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
+ } ) ;
124
132
( iconv as isize != -1 ) . then ( || Self ( iconv) )
125
133
}
126
134
#[ doc( alias = "g_convert_with_iconv" ) ]
@@ -209,20 +217,23 @@ pub fn filename_charsets() -> (bool, Vec<GString>) {
209
217
}
210
218
211
219
#[ 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 > {
214
221
let mut bytes_read = 0 ;
215
222
let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
216
223
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
+ } ) ;
226
237
if error. is_null ( ) {
227
238
Ok ( unsafe {
228
239
(
@@ -265,20 +276,22 @@ pub fn filename_to_utf8(
265
276
}
266
277
267
278
#[ 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 > {
270
280
let mut bytes_read = 0 ;
271
281
let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
272
282
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
+ } ) ;
282
295
if error. is_null ( ) {
283
296
Ok ( unsafe {
284
297
(
@@ -325,7 +338,7 @@ mod tests {
325
338
assert ! ( super :: convert( b"Hello" , "utf-8" , "ascii" ) . is_ok( ) ) ;
326
339
assert ! ( super :: convert( b"He\xaa llo" , "utf-8" , "ascii" ) . is_err( ) ) ;
327
340
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 )
329
342
. unwrap( )
330
343
. 0
331
344
. as_slice( ) ,
0 commit comments