heim_net/
counters.rs

1use std::fmt;
2
3use heim_common::prelude::*;
4use heim_common::units::Information;
5
6use crate::sys;
7
8/// Network device I/O counters.
9pub struct IoCounters(sys::IoCounters);
10
11wrap!(IoCounters, sys::IoCounters);
12
13impl IoCounters {
14    /// Returns network interface name.
15    pub fn interface(&self) -> &str {
16        self.as_ref().interface()
17    }
18
19    /// Returns information amount which was sent via this interface.
20    // TODO: Method returns `Information`, not the "bytes". Should it be renamed?
21    pub fn bytes_sent(&self) -> Information {
22        self.as_ref().bytes_sent()
23    }
24
25    /// Returns information amount which was received via this interface.
26    // TODO: Method returns `Information`, not the "bytes". Should it be renamed?
27    pub fn bytes_recv(&self) -> Information {
28        self.as_ref().bytes_recv()
29    }
30
31    /// Returns packets amount which was sent via this interface.
32    pub fn packets_sent(&self) -> u64 {
33        self.as_ref().packets_sent()
34    }
35
36    /// Returns packets amount which was sent via this interface.
37    pub fn packets_recv(&self) -> u64 {
38        self.as_ref().packets_recv()
39    }
40
41    // TODO: Not sure about methods names below:
42
43    /// Returns errors amount which had occurred while sending data
44    /// via this interface.
45    pub fn errors_sent(&self) -> u64 {
46        self.as_ref().errors_sent()
47    }
48
49    /// Returns errors amount which had occurred while receiving data
50    /// via this interface.
51    pub fn errors_recv(&self) -> u64 {
52        self.as_ref().errors_recv()
53    }
54
55    /// Returns packets amount which were dropped while receiving them.
56    pub fn drop_recv(&self) -> u64 {
57        self.as_ref().drop_recv()
58    }
59}
60
61impl fmt::Debug for IoCounters {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        f.debug_struct("IoCounters")
64            .field("interface", &self.interface())
65            .field("bytes_sent", &self.bytes_sent())
66            .field("bytes_recv", &self.bytes_recv())
67            .field("packets_sent", &self.packets_sent())
68            .field("packets_recv", &self.packets_recv())
69            .field("errors_sent", &self.errors_sent())
70            .field("errors_recv", &self.errors_recv())
71            .field("drop_recv", &self.drop_recv())
72            .finish()
73    }
74}
75
76/// Returns stream which yield [IO counters] for each network interface.
77///
78/// ## Compatibility
79///
80/// Windows implementation is missing, see [related issue](https://github.com/heim-rs/heim/issues/26)
81///
82/// [IO counters]: struct.IoCounters.html
83pub fn io_counters() -> impl Stream<Item = Result<IoCounters>> {
84    sys::io_counters().map_ok(Into::into)
85}