Skip to content

Commit

Permalink
Merge pull request #1113 from A6GibKm/rust-1.70
Browse files Browse the repository at this point in the history
Add a std_once_cell feature
  • Loading branch information
sdroege committed Jul 5, 2023
2 parents ce52380 + a393824 commit 363309c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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 363309c

Please sign in to comment.