Skip to content

Commit 8fc9217

Browse files
jf2048sdroege
authored andcommitted
glib: convert some functions to use IntoGStr
1 parent c77cbfc commit 8fc9217

File tree

4 files changed

+133
-100
lines changed

4 files changed

+133
-100
lines changed

glib/src/convert.rs

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
use 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")]
3739
pub 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")]
6670
pub 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 {}
117125
impl 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\xaallo", "utf-8", "ascii").is_err());
395410
assert_eq!(
396-
super::convert_with_fallback(b"H\xc3\xa9llo", "ascii", "utf-8", None)
411+
super::convert_with_fallback(b"H\xc3\xa9llo", "ascii", "utf-8", crate::NONE_STR)
397412
.unwrap()
398413
.0
399414
.as_slice(),

glib/src/functions.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use std::ptr;
1414
// #[cfg(windows)]
1515
// #[cfg(any(feature = "v2_58", feature = "dox"))]
1616
// use std::os::windows::io::AsRawHandle;
17-
use crate::translate::*;
18-
use crate::GString;
17+
use crate::{translate::*, GStr};
1918
#[cfg(not(windows))]
2019
use crate::{Error, Pid, SpawnFlags};
2120

@@ -213,7 +212,7 @@ pub fn spawn_async_with_pipes<
213212
/// charset if available.
214213
#[doc(alias = "g_get_charset")]
215214
#[doc(alias = "get_charset")]
216-
pub fn charset() -> (bool, Option<GString>) {
215+
pub fn charset() -> (bool, Option<&'static GStr>) {
217216
unsafe {
218217
let mut out_charset = ptr::null();
219218
let is_utf8 = from_glib(ffi::g_get_charset(&mut out_charset));

glib/src/gstring.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,10 @@ impl GStr {
238238

239239
#[doc(alias = "g_utf8_collate")]
240240
#[doc(alias = "utf8_collate")]
241-
pub fn collate(&self, other: impl AsRef<GStr>) -> Ordering {
242-
unsafe { ffi::g_utf8_collate(self.as_ptr(), other.as_ref().as_ptr()) }.cmp(&0)
241+
pub fn collate(&self, other: impl IntoGStr) -> Ordering {
242+
other.run_with_gstr(|other| {
243+
unsafe { ffi::g_utf8_collate(self.to_glib_none().0, other.to_glib_none().0) }.cmp(&0)
244+
})
243245
}
244246

245247
fn check_nuls(s: impl AsRef<[u8]>) -> Result<(), GStrError> {

glib/src/utils.rs

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,38 @@ use std::{
55
mem, ptr,
66
};
77

8-
use crate::translate::*;
8+
use crate::{translate::*, GString, IntoGStr, IntoOptionalGStr};
99

1010
// rustdoc-stripper-ignore-next
1111
/// Same as [`get_prgname()`].
1212
///
1313
/// [`get_prgname()`]: fn.get_prgname.html
1414
#[doc(alias = "get_program_name")]
15-
pub fn program_name() -> Option<String> {
15+
#[inline]
16+
pub fn program_name() -> Option<GString> {
1617
prgname()
1718
}
1819

1920
#[doc(alias = "g_get_prgname")]
2021
#[doc(alias = "get_prgname")]
21-
pub fn prgname() -> Option<String> {
22+
#[inline]
23+
pub fn prgname() -> Option<GString> {
2224
unsafe { from_glib_none(ffi::g_get_prgname()) }
2325
}
2426

2527
// rustdoc-stripper-ignore-next
2628
/// Same as [`set_prgname()`].
2729
///
2830
/// [`set_prgname()`]: fn.set_prgname.html
29-
pub fn set_program_name(name: Option<&str>) {
31+
#[inline]
32+
pub fn set_program_name(name: Option<impl IntoGStr>) {
3033
set_prgname(name)
3134
}
3235

3336
#[doc(alias = "g_set_prgname")]
34-
pub fn set_prgname(name: Option<&str>) {
35-
unsafe { ffi::g_set_prgname(name.to_glib_none().0) }
37+
#[inline]
38+
pub fn set_prgname(name: Option<impl IntoGStr>) {
39+
name.run_with_gstr(|name| unsafe { ffi::g_set_prgname(name.to_glib_none().0) })
3640
}
3741

3842
#[doc(alias = "g_environ_getenv")]
@@ -130,50 +134,60 @@ pub fn is_canonical_pspec_name(name: &str) -> bool {
130134

131135
#[doc(alias = "g_uri_escape_string")]
132136
pub fn uri_escape_string(
133-
unescaped: &str,
134-
reserved_chars_allowed: Option<&str>,
137+
unescaped: impl IntoGStr,
138+
reserved_chars_allowed: Option<impl IntoGStr>,
135139
allow_utf8: bool,
136140
) -> crate::GString {
137-
unsafe {
138-
from_glib_full(ffi::g_uri_escape_string(
139-
unescaped.to_glib_none().0,
140-
reserved_chars_allowed.to_glib_none().0,
141-
allow_utf8.into_glib(),
142-
))
143-
}
141+
unescaped.run_with_gstr(|unescaped| {
142+
reserved_chars_allowed.run_with_gstr(|reserved_chars_allowed| unsafe {
143+
from_glib_full(ffi::g_uri_escape_string(
144+
unescaped.to_glib_none().0,
145+
reserved_chars_allowed.to_glib_none().0,
146+
allow_utf8.into_glib(),
147+
))
148+
})
149+
})
144150
}
145151

146152
#[doc(alias = "g_uri_unescape_string")]
147153
pub fn uri_unescape_string(
148-
escaped_string: &str,
149-
illegal_characters: Option<&str>,
154+
escaped_string: impl IntoGStr,
155+
illegal_characters: Option<impl IntoGStr>,
150156
) -> Option<crate::GString> {
151-
unsafe {
152-
from_glib_full(ffi::g_uri_unescape_string(
153-
escaped_string.to_glib_none().0,
154-
illegal_characters.to_glib_none().0,
155-
))
156-
}
157+
escaped_string.run_with_gstr(|escaped_string| {
158+
illegal_characters.run_with_gstr(|illegal_characters| unsafe {
159+
from_glib_full(ffi::g_uri_unescape_string(
160+
escaped_string.to_glib_none().0,
161+
illegal_characters.to_glib_none().0,
162+
))
163+
})
164+
})
157165
}
158166

159167
#[doc(alias = "g_uri_parse_scheme")]
160-
pub fn uri_parse_scheme(uri: &str) -> Option<crate::GString> {
161-
unsafe { from_glib_full(ffi::g_uri_parse_scheme(uri.to_glib_none().0)) }
168+
pub fn uri_parse_scheme(uri: impl IntoGStr) -> Option<crate::GString> {
169+
uri.run_with_gstr(|uri| unsafe {
170+
from_glib_full(ffi::g_uri_parse_scheme(uri.to_glib_none().0))
171+
})
162172
}
163173

164174
#[doc(alias = "g_uri_unescape_segment")]
165175
pub fn uri_unescape_segment(
166-
escaped_string: Option<&str>,
167-
escaped_string_end: Option<&str>,
168-
illegal_characters: Option<&str>,
176+
escaped_string: Option<impl IntoGStr>,
177+
escaped_string_end: Option<impl IntoGStr>,
178+
illegal_characters: Option<impl IntoGStr>,
169179
) -> Option<crate::GString> {
170-
unsafe {
171-
from_glib_full(ffi::g_uri_unescape_segment(
172-
escaped_string.to_glib_none().0,
173-
escaped_string_end.to_glib_none().0,
174-
illegal_characters.to_glib_none().0,
175-
))
176-
}
180+
escaped_string.run_with_gstr(|escaped_string| {
181+
escaped_string_end.run_with_gstr(|escaped_string_end| {
182+
illegal_characters.run_with_gstr(|illegal_characters| unsafe {
183+
from_glib_full(ffi::g_uri_unescape_segment(
184+
escaped_string.to_glib_none().0,
185+
escaped_string_end.to_glib_none().0,
186+
illegal_characters.to_glib_none().0,
187+
))
188+
})
189+
})
190+
})
177191
}
178192

179193
#[cfg(test)]
@@ -246,16 +260,19 @@ mod tests {
246260
);
247261
assert_eq!(crate::uri_parse_scheme("foo"), None);
248262

249-
let escaped = crate::uri_escape_string("&foo", None, true);
263+
let escaped = crate::uri_escape_string("&foo", crate::NONE_STR, true);
250264
assert_eq!(escaped, GString::from("%26foo"));
251265

252-
let unescaped = crate::uri_unescape_string(escaped.as_str(), None);
266+
let unescaped = crate::uri_unescape_string(escaped.as_str(), crate::GStr::NONE);
253267
assert_eq!(unescaped, Some(GString::from("&foo")));
254268

255269
assert_eq!(
256-
crate::uri_unescape_segment(Some("/foo"), None, None),
270+
crate::uri_unescape_segment(Some("/foo"), crate::NONE_STR, crate::NONE_STR),
257271
Some(GString::from("/foo"))
258272
);
259-
assert_eq!(crate::uri_unescape_segment(Some("/foo%"), None, None), None);
273+
assert_eq!(
274+
crate::uri_unescape_segment(Some("/foo%"), crate::NONE_STR, crate::NONE_STR),
275+
None
276+
);
260277
}
261278
}

0 commit comments

Comments
 (0)