2
2
3
3
use std:: { io, os:: raw:: c_char, path:: PathBuf , ptr} ;
4
4
5
- use crate :: { translate:: * , ConvertError , Error , GStr , GString , NormalizeMode , Slice } ;
5
+ use crate :: {
6
+ translate:: * , ConvertError , Error , GString , IntoGStr , IntoOptionalGStr , NormalizeMode , Slice ,
7
+ } ;
6
8
7
9
// rustdoc-stripper-ignore-next
8
10
/// A wrapper for [`ConvertError`](crate::ConvertError) that can hold an offset into the input
@@ -36,24 +38,26 @@ impl CvtError {
36
38
#[ doc( alias = "g_convert" ) ]
37
39
pub fn convert (
38
40
str_ : & [ u8 ] ,
39
- to_codeset : & str ,
40
- from_codeset : & str ,
41
+ to_codeset : impl IntoGStr ,
42
+ from_codeset : impl IntoGStr ,
41
43
) -> Result < ( Slice < u8 > , usize ) , CvtError > {
42
44
assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
43
45
let mut bytes_read = 0 ;
44
46
let mut bytes_written = 0 ;
45
47
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
+ } ) ;
57
61
if result. is_null ( ) {
58
62
Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
59
63
} else {
@@ -65,26 +69,30 @@ pub fn convert(
65
69
#[ doc( alias = "g_convert_with_fallback" ) ]
66
70
pub fn convert_with_fallback (
67
71
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 > ,
71
75
) -> Result < ( Slice < u8 > , usize ) , CvtError > {
72
76
assert ! ( str_. len( ) <= isize :: MAX as usize ) ;
73
77
let mut bytes_read = 0 ;
74
78
let mut bytes_written = 0 ;
75
79
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
+ } ) ;
88
96
if result. is_null ( ) {
89
97
Err ( CvtError :: new ( unsafe { from_glib_full ( error) } , bytes_read) )
90
98
} else {
@@ -117,10 +125,12 @@ unsafe impl Send for IConv {}
117
125
impl IConv {
118
126
#[ doc( alias = "g_iconv_open" ) ]
119
127
#[ 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
+ } ) ;
124
134
( iconv as isize != -1 ) . then ( || Self ( iconv) )
125
135
}
126
136
#[ doc( alias = "g_convert_with_iconv" ) ]
@@ -209,20 +219,23 @@ pub fn filename_charsets() -> (bool, Vec<GString>) {
209
219
}
210
220
211
221
#[ 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 > {
214
223
let mut bytes_read = 0 ;
215
224
let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
216
225
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
+ } ) ;
226
239
if error. is_null ( ) {
227
240
Ok ( unsafe {
228
241
(
@@ -265,20 +278,22 @@ pub fn filename_to_utf8(
265
278
}
266
279
267
280
#[ 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 > {
270
282
let mut bytes_read = 0 ;
271
283
let mut bytes_written = std:: mem:: MaybeUninit :: uninit ( ) ;
272
284
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
+ } ) ;
282
297
if error. is_null ( ) {
283
298
Ok ( unsafe {
284
299
(
@@ -393,7 +408,7 @@ mod tests {
393
408
assert ! ( super :: convert( b"Hello" , "utf-8" , "ascii" ) . is_ok( ) ) ;
394
409
assert ! ( super :: convert( b"He\xaa llo" , "utf-8" , "ascii" ) . is_err( ) ) ;
395
410
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 )
397
412
. unwrap( )
398
413
. 0
399
414
. as_slice( ) ,
0 commit comments