diff --git a/glib/src/convert.rs b/glib/src/convert.rs index d9a303f0b7ae..3ddf21ae9fe5 100644 --- a/glib/src/convert.rs +++ b/glib/src/convert.rs @@ -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 @@ -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, 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 { @@ -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, ) -> Result<(Slice, 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 { @@ -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 { - 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 { + 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")] @@ -209,20 +219,23 @@ pub fn filename_charsets() -> (bool, Vec) { } #[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 { ( @@ -265,20 +278,22 @@ pub fn filename_to_utf8( } #[doc(alias = "g_locale_from_utf8")] -pub fn locale_from_utf8(utf8string: &GStr) -> Result<(Slice, usize), CvtError> { - assert!(utf8string.len() <= isize::MAX as usize); +pub fn locale_from_utf8(utf8string: impl IntoGStr) -> Result<(Slice, 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 { ( @@ -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(), diff --git a/glib/src/functions.rs b/glib/src/functions.rs index f812c2312caa..4bec745318a7 100644 --- a/glib/src/functions.rs +++ b/glib/src/functions.rs @@ -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}; @@ -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) { +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)); diff --git a/glib/src/gstring.rs b/glib/src/gstring.rs index 702b965bbd47..cf97e10bcf00 100644 --- a/glib/src/gstring.rs +++ b/glib/src/gstring.rs @@ -236,8 +236,10 @@ impl GStr { #[doc(alias = "g_utf8_collate")] #[doc(alias = "utf8_collate")] - pub fn collate(&self, other: impl AsRef) -> Ordering { - unsafe { ffi::g_utf8_collate(self.as_ptr(), other.as_ref().as_ptr()) }.cmp(&0) + pub fn collate(&self, other: impl IntoGStr) -> Ordering { + other.run_with_gstr(|other| { + unsafe { ffi::g_utf8_collate(self.to_glib_none().0, other.to_glib_none().0) }.cmp(&0) + }) } fn check_nuls(s: impl AsRef<[u8]>) -> Result<(), GStrError> { diff --git a/glib/src/utils.rs b/glib/src/utils.rs index 49606579f361..71a0a54c1ca1 100644 --- a/glib/src/utils.rs +++ b/glib/src/utils.rs @@ -5,20 +5,22 @@ use std::{ mem, ptr, }; -use crate::translate::*; +use crate::{translate::*, GString, IntoGStr, IntoOptionalGStr}; // rustdoc-stripper-ignore-next /// Same as [`get_prgname()`]. /// /// [`get_prgname()`]: fn.get_prgname.html #[doc(alias = "get_program_name")] -pub fn program_name() -> Option { +#[inline] +pub fn program_name() -> Option { prgname() } #[doc(alias = "g_get_prgname")] #[doc(alias = "get_prgname")] -pub fn prgname() -> Option { +#[inline] +pub fn prgname() -> Option { unsafe { from_glib_none(ffi::g_get_prgname()) } } @@ -26,13 +28,15 @@ pub fn prgname() -> Option { /// Same as [`set_prgname()`]. /// /// [`set_prgname()`]: fn.set_prgname.html -pub fn set_program_name(name: Option<&str>) { +#[inline] +pub fn set_program_name(name: Option) { set_prgname(name) } #[doc(alias = "g_set_prgname")] -pub fn set_prgname(name: Option<&str>) { - unsafe { ffi::g_set_prgname(name.to_glib_none().0) } +#[inline] +pub fn set_prgname(name: Option) { + name.run_with_gstr(|name| unsafe { ffi::g_set_prgname(name.to_glib_none().0) }) } #[doc(alias = "g_environ_getenv")] @@ -130,50 +134,60 @@ pub fn is_canonical_pspec_name(name: &str) -> bool { #[doc(alias = "g_uri_escape_string")] pub fn uri_escape_string( - unescaped: &str, - reserved_chars_allowed: Option<&str>, + unescaped: impl IntoGStr, + reserved_chars_allowed: Option, allow_utf8: bool, ) -> crate::GString { - unsafe { - from_glib_full(ffi::g_uri_escape_string( - unescaped.to_glib_none().0, - reserved_chars_allowed.to_glib_none().0, - allow_utf8.into_glib(), - )) - } + unescaped.run_with_gstr(|unescaped| { + reserved_chars_allowed.run_with_gstr(|reserved_chars_allowed| unsafe { + from_glib_full(ffi::g_uri_escape_string( + unescaped.to_glib_none().0, + reserved_chars_allowed.to_glib_none().0, + allow_utf8.into_glib(), + )) + }) + }) } #[doc(alias = "g_uri_unescape_string")] pub fn uri_unescape_string( - escaped_string: &str, - illegal_characters: Option<&str>, + escaped_string: impl IntoGStr, + illegal_characters: Option, ) -> Option { - unsafe { - from_glib_full(ffi::g_uri_unescape_string( - escaped_string.to_glib_none().0, - illegal_characters.to_glib_none().0, - )) - } + escaped_string.run_with_gstr(|escaped_string| { + illegal_characters.run_with_gstr(|illegal_characters| unsafe { + from_glib_full(ffi::g_uri_unescape_string( + escaped_string.to_glib_none().0, + illegal_characters.to_glib_none().0, + )) + }) + }) } #[doc(alias = "g_uri_parse_scheme")] -pub fn uri_parse_scheme(uri: &str) -> Option { - unsafe { from_glib_full(ffi::g_uri_parse_scheme(uri.to_glib_none().0)) } +pub fn uri_parse_scheme(uri: impl IntoGStr) -> Option { + uri.run_with_gstr(|uri| unsafe { + from_glib_full(ffi::g_uri_parse_scheme(uri.to_glib_none().0)) + }) } #[doc(alias = "g_uri_unescape_segment")] pub fn uri_unescape_segment( - escaped_string: Option<&str>, - escaped_string_end: Option<&str>, - illegal_characters: Option<&str>, + escaped_string: Option, + escaped_string_end: Option, + illegal_characters: Option, ) -> Option { - unsafe { - from_glib_full(ffi::g_uri_unescape_segment( - escaped_string.to_glib_none().0, - escaped_string_end.to_glib_none().0, - illegal_characters.to_glib_none().0, - )) - } + escaped_string.run_with_gstr(|escaped_string| { + escaped_string_end.run_with_gstr(|escaped_string_end| { + illegal_characters.run_with_gstr(|illegal_characters| unsafe { + from_glib_full(ffi::g_uri_unescape_segment( + escaped_string.to_glib_none().0, + escaped_string_end.to_glib_none().0, + illegal_characters.to_glib_none().0, + )) + }) + }) + }) } #[cfg(test)] @@ -246,16 +260,19 @@ mod tests { ); assert_eq!(crate::uri_parse_scheme("foo"), None); - let escaped = crate::uri_escape_string("&foo", None, true); + let escaped = crate::uri_escape_string("&foo", crate::NONE_STR, true); assert_eq!(escaped, GString::from("%26foo")); - let unescaped = crate::uri_unescape_string(escaped.as_str(), None); + let unescaped = crate::uri_unescape_string(escaped.as_str(), crate::GStr::NONE); assert_eq!(unescaped, Some(GString::from("&foo"))); assert_eq!( - crate::uri_unescape_segment(Some("/foo"), None, None), + crate::uri_unescape_segment(Some("/foo"), crate::NONE_STR, crate::NONE_STR), Some(GString::from("/foo")) ); - assert_eq!(crate::uri_unescape_segment(Some("/foo%"), None, None), None); + assert_eq!( + crate::uri_unescape_segment(Some("/foo%"), crate::NONE_STR, crate::NONE_STR), + None + ); } }