Skip to content

Commit 51f87df

Browse files
committed
glib: convert some functions to use IntoGStr
1 parent 14b5dfc commit 51f87df

File tree

3 files changed

+127
-97
lines changed

3 files changed

+127
-97
lines changed

glib/src/convert.rs

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use crate::{translate::*, ConvertError, Error, GStr, GString, Slice};
3+
use crate::{translate::*, ConvertError, Error, GString, IntoGStr, IntoOptionalGStr, Slice};
44
use std::{io, os::raw::c_char, path::PathBuf, ptr};
55

66
// rustdoc-stripper-ignore-next
@@ -35,24 +35,26 @@ impl CvtError {
3535
#[doc(alias = "g_convert")]
3636
pub fn convert(
3737
str_: &[u8],
38-
to_codeset: &str,
39-
from_codeset: &str,
38+
to_codeset: impl IntoGStr,
39+
from_codeset: impl IntoGStr,
4040
) -> Result<(Slice<u8>, usize), CvtError> {
4141
assert!(str_.len() <= isize::MAX as usize);
4242
let mut bytes_read = 0;
4343
let mut bytes_written = 0;
4444
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+
});
5658
if result.is_null() {
5759
Err(CvtError::new(unsafe { from_glib_full(error) }, bytes_read))
5860
} else {
@@ -64,26 +66,30 @@ pub fn convert(
6466
#[doc(alias = "g_convert_with_fallback")]
6567
pub fn convert_with_fallback(
6668
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>,
7072
) -> Result<(Slice<u8>, usize), CvtError> {
7173
assert!(str_.len() <= isize::MAX as usize);
7274
let mut bytes_read = 0;
7375
let mut bytes_written = 0;
7476
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+
});
8793
if result.is_null() {
8894
Err(CvtError::new(unsafe { from_glib_full(error) }, bytes_read))
8995
} else {
@@ -116,10 +122,12 @@ unsafe impl Send for IConv {}
116122
impl IConv {
117123
#[doc(alias = "g_iconv_open")]
118124
#[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+
});
123131
(iconv as isize != -1).then(|| Self(iconv))
124132
}
125133
#[doc(alias = "g_convert_with_iconv")]
@@ -208,20 +216,23 @@ pub fn filename_charsets() -> (bool, Vec<GString>) {
208216
}
209217

210218
#[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> {
213220
let mut bytes_read = 0;
214221
let mut bytes_written = std::mem::MaybeUninit::uninit();
215222
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+
});
225236
if error.is_null() {
226237
Ok(unsafe {
227238
(
@@ -264,20 +275,22 @@ pub fn filename_to_utf8(
264275
}
265276

266277
#[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> {
269279
let mut bytes_read = 0;
270280
let mut bytes_written = std::mem::MaybeUninit::uninit();
271281
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+
});
281294
if error.is_null() {
282295
Ok(unsafe {
283296
(
@@ -324,7 +337,7 @@ mod tests {
324337
assert!(super::convert(b"Hello", "utf-8", "ascii").is_ok());
325338
assert!(super::convert(b"He\xaallo", "utf-8", "ascii").is_err());
326339
assert_eq!(
327-
super::convert_with_fallback(b"H\xc3\xa9llo", "ascii", "utf-8", None)
340+
super::convert_with_fallback(b"H\xc3\xa9llo", "ascii", "utf-8", crate::NONE_STR)
328341
.unwrap()
329342
.0
330343
.as_slice(),

glib/src/functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3+
use crate::GStr;
34
#[cfg(not(windows))]
45
use std::boxed::Box as Box_;
56
#[cfg(not(windows))]
@@ -15,7 +16,6 @@ use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
1516
use crate::translate::*;
1617
#[cfg(not(windows))]
1718
use crate::Error;
18-
use crate::GString;
1919
#[cfg(not(windows))]
2020
use crate::Pid;
2121
#[cfg(not(windows))]
@@ -216,7 +216,7 @@ pub fn spawn_async_with_pipes<
216216
/// charset if available.
217217
#[doc(alias = "g_get_charset")]
218218
#[doc(alias = "get_charset")]
219-
pub fn charset() -> (bool, Option<GString>) {
219+
pub fn charset() -> (bool, Option<&'static GStr>) {
220220
unsafe {
221221
let mut out_charset = ptr::null();
222222
let is_utf8 = from_glib(ffi::g_get_charset(&mut out_charset));

glib/src/utils.rs

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use crate::translate::*;
3+
use crate::{translate::*, GString, IntoGStr, IntoOptionalGStr};
44
use std::ffi::{OsStr, OsString};
55
use std::mem;
66
use std::ptr;
@@ -10,27 +10,31 @@ use std::ptr;
1010
///
1111
/// [`get_prgname()`]: fn.get_prgname.html
1212
#[doc(alias = "get_program_name")]
13-
pub fn program_name() -> Option<String> {
13+
#[inline]
14+
pub fn program_name() -> Option<GString> {
1415
prgname()
1516
}
1617

1718
#[doc(alias = "g_get_prgname")]
1819
#[doc(alias = "get_prgname")]
19-
pub fn prgname() -> Option<String> {
20+
#[inline]
21+
pub fn prgname() -> Option<GString> {
2022
unsafe { from_glib_none(ffi::g_get_prgname()) }
2123
}
2224

2325
// rustdoc-stripper-ignore-next
2426
/// Same as [`set_prgname()`].
2527
///
2628
/// [`set_prgname()`]: fn.set_prgname.html
27-
pub fn set_program_name(name: Option<&str>) {
29+
#[inline]
30+
pub fn set_program_name(name: Option<impl IntoGStr>) {
2831
set_prgname(name)
2932
}
3033

3134
#[doc(alias = "g_set_prgname")]
32-
pub fn set_prgname(name: Option<&str>) {
33-
unsafe { ffi::g_set_prgname(name.to_glib_none().0) }
35+
#[inline]
36+
pub fn set_prgname(name: Option<impl IntoGStr>) {
37+
name.run_with_gstr(|name| unsafe { ffi::g_set_prgname(name.to_glib_none().0) })
3438
}
3539

3640
#[doc(alias = "g_environ_getenv")]
@@ -128,50 +132,60 @@ pub fn is_canonical_pspec_name(name: &str) -> bool {
128132

129133
#[doc(alias = "g_uri_escape_string")]
130134
pub fn uri_escape_string(
131-
unescaped: &str,
132-
reserved_chars_allowed: Option<&str>,
135+
unescaped: impl IntoGStr,
136+
reserved_chars_allowed: Option<impl IntoGStr>,
133137
allow_utf8: bool,
134138
) -> crate::GString {
135-
unsafe {
136-
from_glib_full(ffi::g_uri_escape_string(
137-
unescaped.to_glib_none().0,
138-
reserved_chars_allowed.to_glib_none().0,
139-
allow_utf8.into_glib(),
140-
))
141-
}
139+
unescaped.run_with_gstr(|unescaped| {
140+
reserved_chars_allowed.run_with_gstr(|reserved_chars_allowed| unsafe {
141+
from_glib_full(ffi::g_uri_escape_string(
142+
unescaped.to_glib_none().0,
143+
reserved_chars_allowed.to_glib_none().0,
144+
allow_utf8.into_glib(),
145+
))
146+
})
147+
})
142148
}
143149

144150
#[doc(alias = "g_uri_unescape_string")]
145151
pub fn uri_unescape_string(
146-
escaped_string: &str,
147-
illegal_characters: Option<&str>,
152+
escaped_string: impl IntoGStr,
153+
illegal_characters: Option<impl IntoGStr>,
148154
) -> Option<crate::GString> {
149-
unsafe {
150-
from_glib_full(ffi::g_uri_unescape_string(
151-
escaped_string.to_glib_none().0,
152-
illegal_characters.to_glib_none().0,
153-
))
154-
}
155+
escaped_string.run_with_gstr(|escaped_string| {
156+
illegal_characters.run_with_gstr(|illegal_characters| unsafe {
157+
from_glib_full(ffi::g_uri_unescape_string(
158+
escaped_string.to_glib_none().0,
159+
illegal_characters.to_glib_none().0,
160+
))
161+
})
162+
})
155163
}
156164

157165
#[doc(alias = "g_uri_parse_scheme")]
158-
pub fn uri_parse_scheme(uri: &str) -> Option<crate::GString> {
159-
unsafe { from_glib_full(ffi::g_uri_parse_scheme(uri.to_glib_none().0)) }
166+
pub fn uri_parse_scheme(uri: impl IntoGStr) -> Option<crate::GString> {
167+
uri.run_with_gstr(|uri| unsafe {
168+
from_glib_full(ffi::g_uri_parse_scheme(uri.to_glib_none().0))
169+
})
160170
}
161171

162172
#[doc(alias = "g_uri_unescape_segment")]
163173
pub fn uri_unescape_segment(
164-
escaped_string: Option<&str>,
165-
escaped_string_end: Option<&str>,
166-
illegal_characters: Option<&str>,
174+
escaped_string: Option<impl IntoGStr>,
175+
escaped_string_end: Option<impl IntoGStr>,
176+
illegal_characters: Option<impl IntoGStr>,
167177
) -> Option<crate::GString> {
168-
unsafe {
169-
from_glib_full(ffi::g_uri_unescape_segment(
170-
escaped_string.to_glib_none().0,
171-
escaped_string_end.to_glib_none().0,
172-
illegal_characters.to_glib_none().0,
173-
))
174-
}
178+
escaped_string.run_with_gstr(|escaped_string| {
179+
escaped_string_end.run_with_gstr(|escaped_string_end| {
180+
illegal_characters.run_with_gstr(|illegal_characters| unsafe {
181+
from_glib_full(ffi::g_uri_unescape_segment(
182+
escaped_string.to_glib_none().0,
183+
escaped_string_end.to_glib_none().0,
184+
illegal_characters.to_glib_none().0,
185+
))
186+
})
187+
})
188+
})
175189
}
176190

177191
#[cfg(test)]
@@ -244,16 +258,19 @@ mod tests {
244258
);
245259
assert_eq!(crate::uri_parse_scheme("foo"), None);
246260

247-
let escaped = crate::uri_escape_string("&foo", None, true);
261+
let escaped = crate::uri_escape_string("&foo", crate::NONE_STR, true);
248262
assert_eq!(escaped, GString::from("%26foo"));
249263

250-
let unescaped = crate::uri_unescape_string(escaped.as_str(), None);
264+
let unescaped = crate::uri_unescape_string(escaped.as_str(), crate::GStr::NONE);
251265
assert_eq!(unescaped, Some(GString::from("&foo")));
252266

253267
assert_eq!(
254-
crate::uri_unescape_segment(Some("/foo"), None, None),
268+
crate::uri_unescape_segment(Some("/foo"), crate::NONE_STR, crate::NONE_STR),
255269
Some(GString::from("/foo"))
256270
);
257-
assert_eq!(crate::uri_unescape_segment(Some("/foo%"), None, None), None);
271+
assert_eq!(
272+
crate::uri_unescape_segment(Some("/foo%"), crate::NONE_STR, crate::NONE_STR),
273+
None
274+
);
258275
}
259276
}

0 commit comments

Comments
 (0)