Skip to content
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

Make FileInfo bytestring methods use CStrings #1112

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions gio/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm we can't do string_type = "c_string"... that only handles all the other options. I wonder how that got forgotten :)


[[object]]
name = "Gio.FilterOutputStream"
Expand Down
22 changes: 0 additions & 22 deletions gio/src/auto/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<glib::GString> {
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)> {
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions gio/src/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::ffi::CString> {
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<std::ffi::CStr>,
) {
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,
);
}
}
}
42 changes: 42 additions & 0 deletions glib/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down