1
1
// Take a look at the license at the top of the repository in the LICENSE file.
2
2
3
- use crate :: { translate:: * , ConvertError , Error , GStr , GString , Slice } ;
3
+ use crate :: { translate:: * , ConvertError , Error , GString , IntoGStr , IntoOptionalGStr , Slice } ;
4
4
use std:: { io, os:: raw:: c_char, path:: PathBuf , ptr} ;
5
5
6
6
// rustdoc-stripper-ignore-next
@@ -35,24 +35,26 @@ impl CvtError {
35
35
#[ doc( alias = "g_convert" ) ]
36
36
pub fn convert (
37
37
str_ : & [ u8 ] ,
38
- to_codeset : & str ,
39
- from_codeset : & str ,
38
+ to_codeset : impl IntoGStr ,
39
+ from_codeset : impl IntoGStr ,
40
40
) -> Result < ( Slice < u8 > , usize ) , CvtError > {
41
41
assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
42
42
let mut bytes_read = 0 ;
43
43
let mut bytes_written = 0 ;
44
44
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
+ } ) ;
56
58
if result. is_null ( ) {
57
59
Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
58
60
} else {
@@ -64,26 +66,30 @@ pub fn convert(
64
66
#[ doc( alias = "g_convert_with_fallback" ) ]
65
67
pub fn convert_with_fallback (
66
68
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 > ,
70
72
) -> Result < ( Slice < u8 > , usize ) , CvtError > {
71
73
assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
72
74
let mut bytes_read = 0 ;
73
75
let mut bytes_written = 0 ;
74
76
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
+ } ) ;
87
93
if result. is_null ( ) {
88
94
Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
89
95
} else {
@@ -116,10 +122,12 @@ unsafe impl Send for IConv {}
116
122
impl IConv {
117
123
#[ doc( alias = "g_iconv_open" ) ]
118
124
#[ 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
+ } ) ;
123
131
( iconv as isize != -1 ) . then ( || Self ( iconv) )
124
132
}
125
133
#[ doc( alias = "g_convert_with_iconv" ) ]
@@ -208,20 +216,23 @@ pub fn filename_charsets() -> (bool, Vec<GString>) {
208
216
}
209
217
210
218
#[ 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 > {
213
220
let mut bytes_read = 0 ;
214
221
let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
215
222
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
+ } ) ;
225
236
if error. is_null ( ) {
226
237
Ok ( unsafe {
227
238
(
@@ -264,20 +275,22 @@ pub fn filename_to_utf8(
264
275
}
265
276
266
277
#[ 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 > {
269
279
let mut bytes_read = 0 ;
270
280
let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
271
281
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
+ } ) ;
281
294
if error. is_null ( ) {
282
295
Ok ( unsafe {
283
296
(
@@ -324,7 +337,7 @@ mod tests {
324
337
assert ! ( super :: convert( b"Hello" , "utf-8" , "ascii" ) . is_ok( ) ) ;
325
338
assert ! ( super :: convert( b"He\xaa llo" , "utf-8" , "ascii" ) . is_err( ) ) ;
326
339
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 )
328
341
. unwrap( )
329
342
. 0
330
343
. as_slice( ) ,
0 commit comments