diff --git a/glib/Cargo.toml b/glib/Cargo.toml index c3874ab2c154..3dfd83399aec 100644 --- a/glib/Cargo.toml +++ b/glib/Cargo.toml @@ -58,6 +58,7 @@ log = ["rs-log"] log_macros = ["log"] compiletests = [] gio = ["gio_ffi"] +std_once_cell = [] [package.metadata.docs.rs] all-features = true diff --git a/glib/src/property.rs b/glib/src/property.rs index 53ff6578bdb6..e407677dac91 100644 --- a/glib/src/property.rs +++ b/glib/src/property.rs @@ -47,6 +47,14 @@ impl Property for once_cell::sync::OnceCell { impl Property for once_cell::unsync::OnceCell { type Value = T::Value; } +#[cfg(feature = "std_once_cell")] +impl Property for std::cell::OnceCell { + type Value = T::Value; +} +#[cfg(feature = "std_once_cell")] +impl Property for std::sync::OnceLock { + type Value = T::Value; +} // Handle smart pointers trasparently impl Property for Rc { type Value = T::Value; @@ -179,6 +187,41 @@ impl PropertySet for once_cell::unsync::OnceCell { } } +#[cfg(feature = "std_once_cell")] +impl PropertyGet for std::cell::OnceCell { + type Value = T; + fn get R>(&self, f: F) -> R { + f(self.get().unwrap()) + } +} +#[cfg(feature = "std_once_cell")] +impl PropertyGet for std::sync::OnceLock { + type Value = T; + fn get R>(&self, f: F) -> R { + f(self.get().unwrap()) + } +} +#[cfg(feature = "std_once_cell")] +impl PropertySet for std::cell::OnceCell { + 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 PropertySet for std::sync::OnceLock { + 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> PropertyGet for WeakRef { type Value = Option;