ashpd/desktop/memory_monitor.rs
1//! # Examples
2//!
3//! ```rust,no_run
4//! use ashpd::desktop::memory_monitor::MemoryMonitor;
5//! use futures_util::StreamExt;
6//!
7//! async fn run() -> ashpd::Result<()> {
8//! let proxy = MemoryMonitor::new().await?;
9//! let level = proxy
10//! .receive_low_memory_warning()
11//! .await?
12//! .next()
13//! .await
14//! .expect("Stream exhausted");
15//! println!("{}", level);
16//! Ok(())
17//! }
18//! ```
19
20use futures_util::Stream;
21
22use crate::{proxy::Proxy, Error};
23
24/// The interface provides information about low system memory to sandboxed
25/// applications.
26///
27/// It is not a portal in the strict sense, since it does not involve user
28/// interaction.
29///
30/// Wrapper of the DBus interface: [`org.freedesktop.portal.MemoryMonitor`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.MemoryMonitor.html).
31#[derive(Debug)]
32#[doc(alias = "org.freedesktop.portal.MemoryMonitor")]
33pub struct MemoryMonitor<'a>(Proxy<'a>);
34
35impl<'a> MemoryMonitor<'a> {
36 /// Create a new instance of [`MemoryMonitor`].
37 pub async fn new() -> Result<MemoryMonitor<'a>, Error> {
38 let proxy = Proxy::new_desktop("org.freedesktop.portal.MemoryMonitor").await?;
39 Ok(Self(proxy))
40 }
41
42 /// Signal emitted when a particular low memory situation happens
43 /// with 0 being the lowest level of memory availability warning, and 255
44 /// being the highest.
45 ///
46 /// # Specifications
47 ///
48 /// See also [`LowMemoryWarning`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.MemoryMonitor.html#org-freedesktop-portal-memorymonitor-lowmemorywarning).
49 #[doc(alias = "LowMemoryWarning")]
50 pub async fn receive_low_memory_warning(&self) -> Result<impl Stream<Item = i32>, Error> {
51 self.0.signal("LowMemoryWarning").await
52 }
53}
54
55impl<'a> std::ops::Deref for MemoryMonitor<'a> {
56 type Target = zbus::Proxy<'a>;
57
58 fn deref(&self) -> &Self::Target {
59 &self.0
60 }
61}