heim_disk/
usage.rs

1use std::fmt;
2use std::path::Path;
3
4use heim_common::prelude::*;
5use heim_common::units::{Information, Ratio};
6
7use crate::sys;
8
9/// Disk usage statistics.
10///
11/// ## Compatibility
12///
13/// See [os]-specific extension traits also.
14///
15/// [os]: ./os/index.html
16pub struct Usage(sys::Usage);
17
18wrap!(Usage, sys::Usage);
19
20impl Usage {
21    /// Returns total information amount available in partition.
22    pub fn total(&self) -> Information {
23        self.as_ref().total()
24    }
25
26    /// Returns used information amount used in partition.
27    pub fn used(&self) -> Information {
28        self.as_ref().used()
29    }
30
31    /// Returns free information about used in partition.
32    pub fn free(&self) -> Information {
33        self.as_ref().free()
34    }
35
36    /// Returns the ratio between used and free information amount in partition.
37    pub fn ratio(&self) -> Ratio {
38        self.as_ref().ratio()
39    }
40}
41
42impl fmt::Debug for Usage {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        f.debug_struct("Usage")
45            .field("total", &self.total())
46            .field("used", &self.used())
47            .field("free", &self.free())
48            .field("ratio", &self.ratio())
49            .finish()
50    }
51}
52
53/// Returns disk [Usage] statistics about the partition which contains the given `path`.
54///
55/// [Usage]: ./struct.Usage.html
56pub fn usage<T>(path: T) -> impl Future<Output = Result<Usage>>
57where
58    T: AsRef<Path>,
59{
60    sys::usage(path).map(|res| res.map(Into::into))
61}