Skip to content

Commit 01585a6

Browse files
committed
Implement gio::FileInfo::set_attribute
Signed-off-by: fbrouille <[email protected]>
1 parent 7520fd3 commit 01585a6

File tree

4 files changed

+142
-120
lines changed

4 files changed

+142
-120
lines changed

gio/src/file.rs

Lines changed: 2 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::{cell::RefCell, ffi::CStr, mem, pin::Pin, ptr};
3+
use std::{cell::RefCell, mem, pin::Pin, ptr};
44

55
use glib::{prelude::*, translate::*};
66

77
#[cfg(feature = "v2_74")]
88
use crate::FileIOStream;
99
use crate::{
10-
ffi, Cancellable, File, FileAttributeType, FileCreateFlags, FileEnumerator, FileQueryInfoFlags,
10+
ffi, Cancellable, File, FileAttributeValue, FileCreateFlags, FileEnumerator, FileQueryInfoFlags,
1111
};
1212

1313
impl File {
@@ -1145,119 +1145,3 @@ pub trait FileExtManual: IsA<File> + Sized {
11451145
}
11461146

11471147
impl<O: IsA<File>> FileExtManual for O {}
1148-
1149-
#[derive(Debug)]
1150-
pub struct FileAttributeValue<'a>(FileAttributeValueInner<'a>);
1151-
1152-
impl From<&str> for FileAttributeValue<'_> {
1153-
fn from(value: &str) -> Self {
1154-
Self(FileAttributeValueInner::String(
1155-
ToGlibPtr::<*mut libc::c_char>::to_glib_none(value).1,
1156-
))
1157-
}
1158-
}
1159-
1160-
impl<'a> From<&'a CStr> for FileAttributeValue<'a> {
1161-
fn from(value: &'a CStr) -> Self {
1162-
Self(FileAttributeValueInner::ByteString(value))
1163-
}
1164-
}
1165-
1166-
impl From<bool> for FileAttributeValue<'_> {
1167-
fn from(value: bool) -> Self {
1168-
Self(FileAttributeValueInner::Boolean(value.into_glib()))
1169-
}
1170-
}
1171-
1172-
impl From<u32> for FileAttributeValue<'_> {
1173-
fn from(value: u32) -> Self {
1174-
Self(FileAttributeValueInner::Uint32(value))
1175-
}
1176-
}
1177-
1178-
impl From<i32> for FileAttributeValue<'_> {
1179-
fn from(value: i32) -> Self {
1180-
Self(FileAttributeValueInner::Int32(value))
1181-
}
1182-
}
1183-
1184-
impl From<u64> for FileAttributeValue<'_> {
1185-
fn from(value: u64) -> Self {
1186-
Self(FileAttributeValueInner::Uint64(value))
1187-
}
1188-
}
1189-
1190-
impl From<i64> for FileAttributeValue<'_> {
1191-
fn from(value: i64) -> Self {
1192-
Self(FileAttributeValueInner::Int64(value))
1193-
}
1194-
}
1195-
1196-
impl<'a, T: AsRef<glib::Object>> From<&'a T> for FileAttributeValue<'a> {
1197-
fn from(value: &'a T) -> Self {
1198-
Self(FileAttributeValueInner::Object(value.as_ref()))
1199-
}
1200-
}
1201-
1202-
impl<'a> From<&'a [&str]> for FileAttributeValue<'a> {
1203-
fn from(value: &'a [&str]) -> Self {
1204-
Self(FileAttributeValueInner::Stringv(value.into()))
1205-
}
1206-
}
1207-
1208-
impl FileAttributeValue<'_> {
1209-
pub(crate) fn type_(&self) -> FileAttributeType {
1210-
self.0.type_()
1211-
}
1212-
1213-
pub(crate) fn as_ptr(&self) -> glib::ffi::gpointer {
1214-
self.0.as_ptr()
1215-
}
1216-
}
1217-
1218-
#[derive(Debug)]
1219-
enum FileAttributeValueInner<'a> {
1220-
#[allow(dead_code)] // TODO remove this allow attribute when Pointer will be used by this crate
1221-
Pointer(FileAttributeType, glib::ffi::gpointer),
1222-
String(<&'a str as ToGlibPtr<'a, *mut libc::c_char>>::Storage),
1223-
ByteString(&'a CStr),
1224-
Boolean(glib::ffi::gboolean),
1225-
Uint32(u32),
1226-
Int32(i32),
1227-
Uint64(u64),
1228-
Int64(i64),
1229-
Object(&'a glib::Object),
1230-
Stringv(glib::StrV),
1231-
}
1232-
1233-
impl FileAttributeValueInner<'_> {
1234-
fn type_(&self) -> FileAttributeType {
1235-
match self {
1236-
Self::Pointer(type_, _) => *type_,
1237-
Self::String(_) => FileAttributeType::String,
1238-
Self::ByteString(_) => FileAttributeType::ByteString,
1239-
Self::Boolean(_) => FileAttributeType::Boolean,
1240-
Self::Uint32(_) => FileAttributeType::Uint32,
1241-
Self::Int32(_) => FileAttributeType::Int32,
1242-
Self::Uint64(_) => FileAttributeType::Uint64,
1243-
Self::Int64(_) => FileAttributeType::Int64,
1244-
Self::Object(_) => FileAttributeType::Object,
1245-
Self::Stringv(_) => FileAttributeType::Stringv,
1246-
}
1247-
}
1248-
1249-
fn as_ptr(&self) -> glib::ffi::gpointer {
1250-
match self {
1251-
Self::Pointer(_, s) => *s,
1252-
Self::String(s) => s.as_ptr() as _,
1253-
Self::ByteString(s) => s.as_ptr() as _,
1254-
Self::Boolean(s) => s as *const i32 as _,
1255-
Self::Uint32(s) => s as *const u32 as _,
1256-
Self::Int32(s) => s as *const i32 as _,
1257-
Self::Uint64(s) => s as *const u64 as _,
1258-
Self::Int64(s) => s as *const i64 as _,
1259-
Self::Object(s) => s.as_ptr() as _,
1260-
Self::Stringv(s) => s.as_ptr() as _,
1261-
}
1262-
}
1263-
}

gio/src/file_attribute_value.rs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use glib::{
2+
object::ObjectType,
3+
translate::{IntoGlib, ToGlibPtr},
4+
};
5+
6+
use crate::FileAttributeType;
7+
8+
use std::ffi::CStr;
9+
10+
#[derive(Debug)]
11+
pub struct FileAttributeValue<'a>(FileAttributeValueInner<'a>);
12+
13+
impl From<&str> for FileAttributeValue<'_> {
14+
fn from(value: &str) -> Self {
15+
Self(FileAttributeValueInner::String(
16+
ToGlibPtr::<*mut libc::c_char>::to_glib_none(value).1,
17+
))
18+
}
19+
}
20+
21+
impl<'a> From<&'a CStr> for FileAttributeValue<'a> {
22+
fn from(value: &'a CStr) -> Self {
23+
Self(FileAttributeValueInner::ByteString(value))
24+
}
25+
}
26+
27+
impl From<bool> for FileAttributeValue<'_> {
28+
fn from(value: bool) -> Self {
29+
Self(FileAttributeValueInner::Boolean(value.into_glib()))
30+
}
31+
}
32+
33+
impl From<u32> for FileAttributeValue<'_> {
34+
fn from(value: u32) -> Self {
35+
Self(FileAttributeValueInner::Uint32(value))
36+
}
37+
}
38+
39+
impl From<i32> for FileAttributeValue<'_> {
40+
fn from(value: i32) -> Self {
41+
Self(FileAttributeValueInner::Int32(value))
42+
}
43+
}
44+
45+
impl From<u64> for FileAttributeValue<'_> {
46+
fn from(value: u64) -> Self {
47+
Self(FileAttributeValueInner::Uint64(value))
48+
}
49+
}
50+
51+
impl From<i64> for FileAttributeValue<'_> {
52+
fn from(value: i64) -> Self {
53+
Self(FileAttributeValueInner::Int64(value))
54+
}
55+
}
56+
57+
impl<'a, T: AsRef<glib::Object>> From<&'a T> for FileAttributeValue<'a> {
58+
fn from(value: &'a T) -> Self {
59+
Self(FileAttributeValueInner::Object(value.as_ref()))
60+
}
61+
}
62+
63+
impl<'a> From<&'a [&str]> for FileAttributeValue<'a> {
64+
fn from(value: &'a [&str]) -> Self {
65+
Self(FileAttributeValueInner::Stringv(value.into()))
66+
}
67+
}
68+
69+
impl FileAttributeValue<'_> {
70+
pub(crate) fn type_(&self) -> FileAttributeType {
71+
self.0.type_()
72+
}
73+
74+
pub(crate) fn as_ptr(&self) -> glib::ffi::gpointer {
75+
self.0.as_ptr()
76+
}
77+
}
78+
79+
#[derive(Debug)]
80+
pub(crate) enum FileAttributeValueInner<'a> {
81+
#[allow(dead_code)] // TODO remove this allow attribute when Pointer will be used by this crate
82+
Pointer(FileAttributeType, glib::ffi::gpointer),
83+
String(<&'a str as ToGlibPtr<'a, *mut libc::c_char>>::Storage),
84+
ByteString(&'a CStr),
85+
Boolean(glib::ffi::gboolean),
86+
Uint32(u32),
87+
Int32(i32),
88+
Uint64(u64),
89+
Int64(i64),
90+
Object(&'a glib::Object),
91+
Stringv(glib::StrV),
92+
}
93+
94+
impl FileAttributeValueInner<'_> {
95+
pub(crate) fn type_(&self) -> FileAttributeType {
96+
match self {
97+
Self::Pointer(type_, _) => *type_,
98+
Self::String(_) => FileAttributeType::String,
99+
Self::ByteString(_) => FileAttributeType::ByteString,
100+
Self::Boolean(_) => FileAttributeType::Boolean,
101+
Self::Uint32(_) => FileAttributeType::Uint32,
102+
Self::Int32(_) => FileAttributeType::Int32,
103+
Self::Uint64(_) => FileAttributeType::Uint64,
104+
Self::Int64(_) => FileAttributeType::Int64,
105+
Self::Object(_) => FileAttributeType::Object,
106+
Self::Stringv(_) => FileAttributeType::Stringv,
107+
}
108+
}
109+
110+
pub(crate) fn as_ptr(&self) -> glib::ffi::gpointer {
111+
match self {
112+
Self::Pointer(_, s) => *s,
113+
Self::String(s) => s.as_ptr() as _,
114+
Self::ByteString(s) => s.as_ptr() as _,
115+
Self::Boolean(s) => s as *const i32 as _,
116+
Self::Uint32(s) => s as *const u32 as _,
117+
Self::Int32(s) => s as *const i32 as _,
118+
Self::Uint64(s) => s as *const u64 as _,
119+
Self::Int64(s) => s as *const i64 as _,
120+
Self::Object(s) => s.as_ptr() as _,
121+
Self::Stringv(s) => s.as_ptr() as _,
122+
}
123+
}
124+
}

gio/src/file_info.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77

88
use glib::{translate::*, StrV};
99

10-
use crate::{ffi, FileInfo};
10+
use crate::{ffi, FileAttributeValue, FileInfo};
1111

1212
impl FileInfo {
1313
#[cfg_attr(feature = "v2_62", deprecated)]
@@ -71,4 +71,17 @@ impl FileInfo {
7171
});
7272
}
7373
}
74+
75+
#[doc(alias = "g_file_info_set_attribute")]
76+
pub fn set_attribute<'a>(&self, attribute: &str, value: impl Into<FileAttributeValue<'a>>) {
77+
unsafe {
78+
let value: FileAttributeValue<'a> = value.into();
79+
ffi::g_file_info_set_attribute(
80+
self.to_glib_none().0,
81+
attribute.to_glib_none().0,
82+
value.type_().into_glib(),
83+
value.as_ptr(),
84+
);
85+
}
86+
}
7487
}

gio/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ mod debug_controller_dbus;
4343
mod desktop_app_info;
4444
mod error;
4545
mod file;
46-
pub use file::FileAttributeValue;
4746
mod file_attribute_info;
4847
pub use crate::file_attribute_info::FileAttributeInfo;
4948
mod file_attribute_info_list;
5049
mod file_attribute_matcher;
5150
pub use crate::file_attribute_matcher::FileAttributematcherIter;
51+
mod file_attribute_value;
52+
pub use file_attribute_value::FileAttributeValue;
5253
#[cfg(unix)]
5354
mod file_descriptor_based;
5455
#[cfg(unix)]

0 commit comments

Comments
 (0)