From 1debbb0534b4db1f15532aa283c26c00bd63dd10 Mon Sep 17 00:00:00 2001 From: Jason Francis Date: Wed, 31 May 2023 17:57:21 -0400 Subject: [PATCH 1/2] glib: impl FromGlibPtr traits for CString --- glib/src/translate.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/glib/src/translate.rs b/glib/src/translate.rs index 11cfec05f01b..a0db92c8560f 100644 --- a/glib/src/translate.rs +++ b/glib/src/translate.rs @@ -732,6 +732,14 @@ impl GlibPtrDefault for String { type GlibType = *mut c_char; } +impl GlibPtrDefault for CStr { + type GlibType = *mut c_char; +} + +impl GlibPtrDefault for CString { + type GlibType = *mut c_char; +} + #[cfg(not(windows))] pub(crate) fn path_to_c(path: &Path) -> CString { // GLib paths on UNIX are always in the local encoding, just like in Rust @@ -1711,6 +1719,40 @@ impl FromGlibPtrFull<*mut c_char> for String { } } +impl FromGlibPtrNone<*const c_char> for CString { + #[inline] + unsafe fn from_glib_none(ptr: *const c_char) -> Self { + debug_assert!(!ptr.is_null()); + CStr::from_ptr(ptr).to_owned() + } +} + +impl FromGlibPtrFull<*const c_char> for CString { + #[inline] + unsafe fn from_glib_full(ptr: *const c_char) -> Self { + let res = from_glib_none(ptr); + ffi::g_free(ptr as *mut _); + res + } +} + +impl FromGlibPtrNone<*mut c_char> for CString { + #[inline] + unsafe fn from_glib_none(ptr: *mut c_char) -> Self { + debug_assert!(!ptr.is_null()); + CStr::from_ptr(ptr).to_owned() + } +} + +impl FromGlibPtrFull<*mut c_char> for CString { + #[inline] + unsafe fn from_glib_full(ptr: *mut c_char) -> Self { + let res = from_glib_none(ptr); + ffi::g_free(ptr as *mut _); + res + } +} + #[cfg(not(windows))] pub(crate) unsafe fn c_to_path_buf(ptr: *const c_char) -> PathBuf { debug_assert!(!ptr.is_null()); From b805c566409294731d5ac8aa5d4c1806d7315094 Mon Sep 17 00:00:00 2001 From: Jason Francis Date: Wed, 31 May 2023 17:57:43 -0400 Subject: [PATCH 2/2] gio: use CStr and CString for FileInfo byte string methods --- gio/Gir.toml | 8 ++++++++ gio/src/auto/file_info.rs | 22 ---------------------- gio/src/file_info.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/gio/Gir.toml b/gio/Gir.toml index 7b83c26da5ed..c09927904cd2 100644 --- a/gio/Gir.toml +++ b/gio/Gir.toml @@ -822,6 +822,14 @@ status = "generate" name = "set_attribute_stringv" # use strv manual = true + [[object.function]] + name = "get_attribute_byte_string" + # use CString + manual = true + [[object.function]] + name = "set_attribute_byte_string" + # use CStr + manual = true [[object]] name = "Gio.FilterOutputStream" diff --git a/gio/src/auto/file_info.rs b/gio/src/auto/file_info.rs index 33a49e00b4cd..e40c7f87c66e 100644 --- a/gio/src/auto/file_info.rs +++ b/gio/src/auto/file_info.rs @@ -72,17 +72,6 @@ impl FileInfo { } } - #[doc(alias = "g_file_info_get_attribute_byte_string")] - #[doc(alias = "get_attribute_byte_string")] - pub fn attribute_byte_string(&self, attribute: &str) -> Option { - unsafe { - from_glib_none(ffi::g_file_info_get_attribute_byte_string( - self.to_glib_none().0, - attribute.to_glib_none().0, - )) - } - } - //#[doc(alias = "g_file_info_get_attribute_data")] //#[doc(alias = "get_attribute_data")] //pub fn attribute_data(&self, attribute: &str, value_pp: /*Unimplemented*/&mut Basic: Pointer) -> Option<(FileAttributeType, FileAttributeStatus)> { @@ -341,17 +330,6 @@ impl FileInfo { } } - #[doc(alias = "g_file_info_set_attribute_byte_string")] - pub fn set_attribute_byte_string(&self, attribute: &str, attr_value: &str) { - unsafe { - ffi::g_file_info_set_attribute_byte_string( - self.to_glib_none().0, - attribute.to_glib_none().0, - attr_value.to_glib_none().0, - ); - } - } - #[doc(alias = "g_file_info_set_attribute_int32")] pub fn set_attribute_int32(&self, attribute: &str, attr_value: i32) { unsafe { diff --git a/gio/src/file_info.rs b/gio/src/file_info.rs index 0019833d4552..88f7867ea724 100644 --- a/gio/src/file_info.rs +++ b/gio/src/file_info.rs @@ -71,4 +71,30 @@ impl FileInfo { }); } } + + #[doc(alias = "g_file_info_get_attribute_byte_string")] + #[doc(alias = "get_attribute_byte_string")] + pub fn attribute_byte_string(&self, attribute: &str) -> Option { + unsafe { + from_glib_none(ffi::g_file_info_get_attribute_byte_string( + self.to_glib_none().0, + attribute.to_glib_none().0, + )) + } + } + + #[doc(alias = "g_file_info_set_attribute_byte_string")] + pub fn set_attribute_byte_string( + &self, + attribute: &str, + attr_value: impl AsRef, + ) { + unsafe { + ffi::g_file_info_set_attribute_byte_string( + self.to_glib_none().0, + attribute.to_glib_none().0, + attr_value.as_ref().to_glib_none().0, + ); + } + } }