heim_memory/
memory.rs

1use std::fmt;
2
3use heim_common::prelude::*;
4use heim_common::units::Information;
5
6use crate::sys;
7
8/// Physical memory statistics.
9///
10/// Only three metrics are guaranteed to be cross-platform,
11/// for other metrics see `MemoryExt` traits in the [os] submodules.
12///
13/// [os]: ./os/index.html
14pub struct Memory(sys::Memory);
15
16wrap!(Memory, sys::Memory);
17
18impl Memory {
19    /// The total amount of physical memory.
20    pub fn total(&self) -> Information {
21        self.as_ref().total()
22    }
23
24    /// The amount of memory that can be given instantly to processes
25    /// without the system going into swap.
26    pub fn available(&self) -> Information {
27        self.as_ref().available()
28    }
29
30    /// The amount of memory not being used at all (zeroed) that is readily available;
31    /// note that this does not reflect the actual memory available.
32    pub fn free(&self) -> Information {
33        self.as_ref().free()
34    }
35}
36
37impl fmt::Debug for Memory {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        f.debug_struct("Memory")
40            .field("total", &self.total())
41            .field("available", &self.available())
42            .field("free", &self.free())
43            .finish()
44    }
45}
46
47/// Returns future which will resolve into [Memory] struct.
48///
49/// [Memory]: ./struct.Memory.html
50pub fn memory() -> impl future::Future<Output = Result<Memory>> {
51    sys::memory().map(|res| res.map(Into::into))
52}