Skip to content

GString refactor #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- stable
- beta
- nightly
- "1.63.0"
- "1.64.0"
conf:
- { 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 }
- { name: "gdk-pixbuf", features: "v2_42", nightly: "--all-features", test_sys: true }
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
- stable
- beta
- nightly
- "1.63.0"
- "1.64.0"
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
Expand Down
3 changes: 2 additions & 1 deletion glib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = [
"gir-files/*",
]
edition = "2021"
rust-version = "1.63"
rust-version = "1.64"

[lib]
name = "glib"
Expand All @@ -34,6 +34,7 @@ rs-log = { package = "log", version = "0.4", optional = true }
smallvec = "1.0"
thiserror = "1"
gio_ffi = { package = "gio-sys", path = "../gio/sys", optional = true }
memchr = "2.5.0"

[dev-dependencies]
tempfile = "3"
Expand Down
127 changes: 71 additions & 56 deletions glib/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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

use crate::{translate::*, ConvertError, Error, GStr, GString, NormalizeMode, Slice};
use crate::{
translate::*, ConvertError, Error, GString, IntoGStr, IntoOptionalGStr, NormalizeMode, Slice,
};

// rustdoc-stripper-ignore-next
/// A wrapper for [`ConvertError`](crate::ConvertError) that can hold an offset into the input
Expand Down Expand Up @@ -36,24 +38,26 @@ impl CvtError {
#[doc(alias = "g_convert")]
pub fn convert(
str_: &[u8],
to_codeset: &str,
from_codeset: &str,
to_codeset: impl IntoGStr,
from_codeset: impl IntoGStr,
) -> Result<(Slice<u8>, usize), CvtError> {
assert!(str_.len() <= isize::MAX as usize);
let mut bytes_read = 0;
let mut bytes_written = 0;
let mut error = ptr::null_mut();
let result = unsafe {
ffi::g_convert(
str_.as_ptr(),
str_.len() as isize,
to_codeset.to_glib_none().0,
from_codeset.to_glib_none().0,
&mut bytes_read,
&mut bytes_written,
&mut error,
)
};
let result = to_codeset.run_with_gstr(|to_codeset| {
from_codeset.run_with_gstr(|from_codeset| unsafe {
ffi::g_convert(
str_.as_ptr(),
str_.len() as isize,
to_codeset.to_glib_none().0,
from_codeset.to_glib_none().0,
&mut bytes_read,
&mut bytes_written,
&mut error,
)
})
});
if result.is_null() {
Err(CvtError::new(unsafe { from_glib_full(error) }, bytes_read))
} else {
Expand All @@ -65,26 +69,30 @@ pub fn convert(
#[doc(alias = "g_convert_with_fallback")]
pub fn convert_with_fallback(
str_: &[u8],
to_codeset: &str,
from_codeset: &str,
fallback: Option<&str>,
to_codeset: impl IntoGStr,
from_codeset: impl IntoGStr,
fallback: Option<impl IntoGStr>,
) -> Result<(Slice<u8>, usize), CvtError> {
assert!(str_.len() <= isize::MAX as usize);
let mut bytes_read = 0;
let mut bytes_written = 0;
let mut error = ptr::null_mut();
let result = unsafe {
ffi::g_convert_with_fallback(
str_.as_ptr(),
str_.len() as isize,
to_codeset.to_glib_none().0,
from_codeset.to_glib_none().0,
fallback.to_glib_none().0,
&mut bytes_read,
&mut bytes_written,
&mut error,
)
};
let result = to_codeset.run_with_gstr(|to_codeset| {
from_codeset.run_with_gstr(|from_codeset| {
fallback.run_with_gstr(|fallback| unsafe {
ffi::g_convert_with_fallback(
str_.as_ptr(),
str_.len() as isize,
to_codeset.to_glib_none().0,
from_codeset.to_glib_none().0,
fallback.to_glib_none().0,
&mut bytes_read,
&mut bytes_written,
&mut error,
)
})
})
});
if result.is_null() {
Err(CvtError::new(unsafe { from_glib_full(error) }, bytes_read))
} else {
Expand Down Expand Up @@ -117,10 +125,12 @@ unsafe impl Send for IConv {}
impl IConv {
#[doc(alias = "g_iconv_open")]
#[allow(clippy::unnecessary_lazy_evaluations)]
pub fn new(to_codeset: &str, from_codeset: &str) -> Option<Self> {
let iconv = unsafe {
ffi::g_iconv_open(to_codeset.to_glib_none().0, from_codeset.to_glib_none().0)
};
pub fn new(to_codeset: impl IntoGStr, from_codeset: impl IntoGStr) -> Option<Self> {
let iconv = to_codeset.run_with_gstr(|to_codeset| {
from_codeset.run_with_gstr(|from_codeset| unsafe {
ffi::g_iconv_open(to_codeset.to_glib_none().0, from_codeset.to_glib_none().0)
})
});
(iconv as isize != -1).then(|| Self(iconv))
}
#[doc(alias = "g_convert_with_iconv")]
Expand Down Expand Up @@ -209,20 +219,23 @@ pub fn filename_charsets() -> (bool, Vec<GString>) {
}

#[doc(alias = "g_filename_from_utf8")]
pub fn filename_from_utf8(utf8string: &str) -> Result<(PathBuf, usize), CvtError> {
let len = utf8string.len() as isize;
pub fn filename_from_utf8(utf8string: impl IntoGStr) -> Result<(PathBuf, usize), CvtError> {
let mut bytes_read = 0;
let mut bytes_written = std::mem::MaybeUninit::uninit();
let mut error = ptr::null_mut();
let ret = unsafe {
ffi::g_filename_from_utf8(
utf8string.to_glib_none().0,
len,
&mut bytes_read,
bytes_written.as_mut_ptr(),
&mut error,
)
};
let ret = utf8string.run_with_gstr(|utf8string| {
assert!(utf8string.len() <= isize::MAX as usize);
let len = utf8string.len() as isize;
unsafe {
ffi::g_filename_from_utf8(
utf8string.to_glib_none().0,
len,
&mut bytes_read,
bytes_written.as_mut_ptr(),
&mut error,
)
}
});
if error.is_null() {
Ok(unsafe {
(
Expand Down Expand Up @@ -265,20 +278,22 @@ pub fn filename_to_utf8(
}

#[doc(alias = "g_locale_from_utf8")]
pub fn locale_from_utf8(utf8string: &GStr) -> Result<(Slice<u8>, usize), CvtError> {
assert!(utf8string.len() <= isize::MAX as usize);
pub fn locale_from_utf8(utf8string: impl IntoGStr) -> Result<(Slice<u8>, usize), CvtError> {
let mut bytes_read = 0;
let mut bytes_written = std::mem::MaybeUninit::uninit();
let mut error = ptr::null_mut();
let ret = unsafe {
ffi::g_locale_from_utf8(
utf8string.as_ptr(),
utf8string.len() as isize,
&mut bytes_read,
bytes_written.as_mut_ptr(),
&mut error,
)
};
let ret = utf8string.run_with_gstr(|utf8string| {
assert!(utf8string.len() <= isize::MAX as usize);
unsafe {
ffi::g_locale_from_utf8(
utf8string.as_ptr(),
utf8string.len() as isize,
&mut bytes_read,
bytes_written.as_mut_ptr(),
&mut error,
)
}
});
if error.is_null() {
Ok(unsafe {
(
Expand Down Expand Up @@ -393,7 +408,7 @@ mod tests {
assert!(super::convert(b"Hello", "utf-8", "ascii").is_ok());
assert!(super::convert(b"He\xaallo", "utf-8", "ascii").is_err());
assert_eq!(
super::convert_with_fallback(b"H\xc3\xa9llo", "ascii", "utf-8", None)
super::convert_with_fallback(b"H\xc3\xa9llo", "ascii", "utf-8", crate::NONE_STR)
.unwrap()
.0
.as_slice(),
Expand Down
5 changes: 2 additions & 3 deletions glib/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use std::ptr;
// #[cfg(windows)]
// #[cfg(any(feature = "v2_58", feature = "dox"))]
// use std::os::windows::io::AsRawHandle;
use crate::translate::*;
use crate::GString;
use crate::{translate::*, GStr};
#[cfg(not(windows))]
use crate::{Error, Pid, SpawnFlags};

Expand Down Expand Up @@ -213,7 +212,7 @@ pub fn spawn_async_with_pipes<
/// charset if available.
#[doc(alias = "g_get_charset")]
#[doc(alias = "get_charset")]
pub fn charset() -> (bool, Option<GString>) {
pub fn charset() -> (bool, Option<&'static GStr>) {
unsafe {
let mut out_charset = ptr::null();
let is_utf8 = from_glib(ffi::g_get_charset(&mut out_charset));
Expand Down
Loading