Debian Codegraph @

src/desktop/memory_monitor.rs

raw
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
20 use futures_util::Stream;
21
22 use crate::{Error, proxy::Proxy};
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")]
33 pub struct MemoryMonitor(Proxy<'static>);
34
35 impl MemoryMonitor {
36 /// Create a new instance of [`MemoryMonitor`].
37 pub async fn new() -> Result<Self, Error> {
38 let proxy = Proxy::new_desktop("org.freedesktop.portal.MemoryMonitor").await?;
39 Ok(Self(proxy))
40 }
41
42 /// Create a new instance of [`MemoryMonitor`].
43 pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
44 let proxy =
45 Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.MemoryMonitor")
46 .await?;
47 Ok(Self(proxy))
48 }
49
50 /// Returns the version of the portal interface.
51 pub fn version(&self) -> u32 {
52 self.0.version()
53 }
54
55 /// Signal emitted when a particular low memory situation happens
56 /// with 0 being the lowest level of memory availability warning, and 255
57 /// being the highest.
58 ///
59 /// # Specifications
60 ///
61 /// See also [`LowMemoryWarning`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.MemoryMonitor.html#org-freedesktop-portal-memorymonitor-lowmemorywarning).
62 #[doc(alias = "LowMemoryWarning")]
63 pub async fn receive_low_memory_warning(&self) -> Result<impl Stream<Item = i32>, Error> {
64 self.0.signal("LowMemoryWarning").await
65 }
66 }
67
68 impl std::ops::Deref for MemoryMonitor {
69 type Target = zbus::Proxy<'static>;
70
71 fn deref(&self) -> &Self::Target {
72 &self.0
73 }
74 }