Skip to content

Commit 8a9d3be

Browse files
authored
Merge pull request #600 from jf2048/gstring-traits
GString refactor
2 parents d983c4e + 8fc9217 commit 8a9d3be

File tree

9 files changed

+1288
-266
lines changed

9 files changed

+1288
-266
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- stable
1919
- beta
2020
- nightly
21-
- "1.63.0"
21+
- "1.64.0"
2222
conf:
2323
- { name: "cairo", features: "png,pdf,svg,ps,use_glib,v1_18,freetype,script,xcb,xlib,win32-surface", nightly: "--features 'png,pdf,svg,ps,use_glib,v1_18,freetype,script,xcb,xlib,win32-surface'", test_sys: true }
2424
- { name: "gdk-pixbuf", features: "v2_42", nightly: "--all-features", test_sys: true }
@@ -100,7 +100,7 @@ jobs:
100100
- stable
101101
- beta
102102
- nightly
103-
- "1.63.0"
103+
- "1.64.0"
104104
steps:
105105
- uses: actions/checkout@v3
106106
- uses: actions-rs/toolchain@v1

glib/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exclude = [
1313
"gir-files/*",
1414
]
1515
edition = "2021"
16-
rust-version = "1.63"
16+
rust-version = "1.64"
1717

1818
[lib]
1919
name = "glib"
@@ -34,6 +34,7 @@ rs-log = { package = "log", version = "0.4", optional = true }
3434
smallvec = "1.0"
3535
thiserror = "1"
3636
gio_ffi = { package = "gio-sys", path = "../gio/sys", optional = true }
37+
memchr = "2.5.0"
3738

3839
[dev-dependencies]
3940
tempfile = "3"

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));

0 commit comments

Comments
 (0)