Skip to content

Commit bf3d2f1

Browse files
committed
glib: convert some functions to use IntoGStr
1 parent 003f236 commit bf3d2f1

File tree

3 files changed

+127
-98
lines changed

3 files changed

+127
-98
lines changed

glib/src/convert.rs

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

33
use std::{io, os::raw::c_char, path::PathBuf, ptr};
44

5-
use crate::{translate::*, ConvertError, Error, GStr, GString, Slice};
5+
use crate::{translate::*, ConvertError, Error, GString, IntoGStr, IntoOptionalGStr, Slice};
66

77
// rustdoc-stripper-ignore-next
88
/// A wrapper for [`ConvertError`](crate::ConvertError) that can hold an offset into the input
@@ -36,24 +36,26 @@ impl CvtError {
3636
#[doc(alias = "g_convert")]
3737
pub fn convert(
3838
str_: &[u8],
39-
to_codeset: &str,
40-
from_codeset: &str,
39+
to_codeset: impl IntoGStr,
40+
from_codeset: impl IntoGStr,
4141
) -> Result<(Slice<u8>, usize), CvtError> {
4242
assert!(str_.len() <= isize::MAX as usize);
4343
let mut bytes_read = 0;
4444
let mut bytes_written = 0;
4545
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+
});
5759
if result.is_null() {
5860
Err(CvtError::new(unsafe { from_glib_full(error) }, bytes_read))
5961
} else {
@@ -65,26 +67,30 @@ pub fn convert(
6567
#[doc(alias = "g_convert_with_fallback")]
6668
pub fn convert_with_fallback(
6769
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>,
7173
) -> Result<(Slice<u8>, usize), CvtError> {
7274
assert!(str_.len() <= isize::MAX as usize);
7375
let mut bytes_read = 0;
7476
let mut bytes_written = 0;
7577
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+
});
8894
if result.is_null() {
8995
Err(CvtError::new(unsafe { from_glib_full(error) }, bytes_read))
9096
} else {
@@ -117,10 +123,12 @@ unsafe impl Send for IConv {}
117123
impl IConv {
118124
#[doc(alias = "g_iconv_open")]
119125
#[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+
});
124132
(iconv as isize != -1).then(|| Self(iconv))
125133
}
126134
#[doc(alias = "g_convert_with_iconv")]
@@ -209,20 +217,23 @@ pub fn filename_charsets() -> (bool, Vec<GString>) {
209217
}
210218

211219
#[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> {
214221
let mut bytes_read = 0;
215222
let mut bytes_written = std::mem::MaybeUninit::uninit();
216223
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+
});
226237
if error.is_null() {
227238
Ok(unsafe {
228239
(
@@ -265,20 +276,22 @@ pub fn filename_to_utf8(
265276
}
266277

267278
#[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> {
270280
let mut bytes_read = 0;
271281
let mut bytes_written = std::mem::MaybeUninit::uninit();
272282
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+
});
282295
if error.is_null() {
283296
Ok(unsafe {
284297
(
@@ -325,7 +338,7 @@ mod tests {
325338
assert!(super::convert(b"Hello", "utf-8", "ascii").is_ok());
326339
assert!(super::convert(b"He\xaallo", "utf-8", "ascii").is_err());
327340
assert_eq!(
328-
super::convert_with_fallback(b"H\xc3\xa9llo", "ascii", "utf-8", None)
341+
super::convert_with_fallback(b"H\xc3\xa9llo", "ascii", "utf-8", crate::NONE_STR)
329342
.unwrap()
330343
.0
331344
.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/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)