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

Add a std_once_cell feature #1113

Merged
merged 1 commit into from
Jul 5, 2023
Merged
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
1 change: 1 addition & 0 deletions glib/Cargo.toml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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