diff --git a/wayland-server/CHANGELOG.md b/wayland-server/CHANGELOG.md index c0c420ea1d4..2440948b1e1 100644 --- a/wayland-server/CHANGELOG.md +++ b/wayland-server/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +#### Additions + +- Add `Weak::is_alive` allowing to check if a resource is still alive. + ## 0.31.1 -- 2024-01-29 - Dropped `nix` dependency in favor of `rustix` diff --git a/wayland-server/src/lib.rs b/wayland-server/src/lib.rs index 7526f430a46..32edd119bb9 100644 --- a/wayland-server/src/lib.rs +++ b/wayland-server/src/lib.rs @@ -293,6 +293,19 @@ impl Weak { I::from_id(&d_handle, self.id.clone()) } + /// Check if this resource is still alive + /// + /// This will return `false` if either: + /// - the object represented by this handle has already been destroyed at the protocol level + /// - the Wayland connection has already been closed + #[inline] + pub fn is_alive(&self) -> bool { + let Some(handle) = self.handle.upgrade() else { + return false; + }; + handle.object_info(self.id.clone()).is_ok() + } + /// The underlying [`ObjectId`] pub fn id(&self) -> ObjectId { self.id.clone()