Skip to content

Commit

Permalink
Add a std_once_cell feature
Browse files Browse the repository at this point in the history
That adds std::cell::OnceCell and std::sync::OnceLock support for the
properties macro.
  • Loading branch information
A6GibKm committed Jul 4, 2023
1 parent ce52380 commit e7a0ab3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Expand Up @@ -23,7 +23,7 @@ jobs:
- { 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 }
- { name: "gio", features: "v2_74", nightly: "--all-features", test_sys: true }
- { name: "glib", features: "v2_74", nightly: "--all-features", test_sys: true }
- { name: "glib", features: "v2_74", nightly: "--features=v2_76,log,log_macros,compiletests", test_sys: true }
- { name: "graphene", features: "", nightly: "", test_sys: true }
- { name: "pango", features: "v1_50", nightly: "--all-features", test_sys: true }
- { name: "pangocairo", features: "", nightly: "--all-features", test_sys: true }
Expand Down
1 change: 1 addition & 0 deletions glib/Cargo.toml
Expand Up @@ -58,6 +58,7 @@ log = ["rs-log"]
log_macros = ["log"]
compiletests = []
gio = ["gio_ffi"]
std_once_cell = []

[package.metadata.docs.rs]
all-features = true
Expand Down
43 changes: 43 additions & 0 deletions glib/src/property.rs
Expand Up @@ -47,6 +47,14 @@ impl<T: Property> Property for once_cell::sync::OnceCell<T> {
impl<T: Property> Property for once_cell::unsync::OnceCell<T> {
type Value = T::Value;
}
#[cfg(feature = "std_once_cell")]
impl<T: Property> Property for std::cell::OnceCell<T> {
type Value = T::Value;
}
#[cfg(feature = "std_once_cell")]
impl<T: Property> Property for std::sync::OnceLock<T> {
type Value = T::Value;
}
// Handle smart pointers trasparently
impl<T: Property> Property for Rc<T> {
type Value = T::Value;
Expand Down Expand Up @@ -179,6 +187,41 @@ impl<T> PropertySet for once_cell::unsync::OnceCell<T> {
}
}

#[cfg(feature = "std_once_cell")]
impl<T> PropertyGet for std::cell::OnceCell<T> {
type Value = T;
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
f(self.get().unwrap())
}
}
#[cfg(feature = "std_once_cell")]
impl<T> PropertyGet for std::sync::OnceLock<T> {
type Value = T;
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
f(self.get().unwrap())
}
}
#[cfg(feature = "std_once_cell")]
impl<T> PropertySet for std::cell::OnceCell<T> {
type SetValue = T;
fn set(&self, v: Self::SetValue) {
// I can't use `unwrap` because I would have to add a `Debug` bound to _v
if let Err(_v) = self.set(v) {
panic!("can't set value of OnceCell multiple times")
};
}
}
#[cfg(feature = "std_once_cell")]
impl<T> PropertySet for std::sync::OnceLock<T> {
type SetValue = T;
fn set(&self, v: Self::SetValue) {
// I can't use `unwrap` because I would have to add a `Debug` bound to _v
if let Err(_v) = self.set(v) {
panic!("can't set value of OnceCell multiple times")
};
}
}

impl<T: IsA<Object>> PropertyGet for WeakRef<T> {
type Value = Option<T>;

Expand Down

0 comments on commit e7a0ab3

Please sign in to comment.