1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::fmt;

use heim_common::prelude::*;
use heim_common::units::Information;

use crate::sys;

/// Network device I/O counters.
pub struct IoCounters(sys::IoCounters);

wrap!(IoCounters, sys::IoCounters);

impl IoCounters {
    /// Returns network interface name.
    pub fn interface(&self) -> &str {
        self.as_ref().interface()
    }

    /// Returns information amount which was sent via this interface.
    // TODO: Method returns `Information`, not the "bytes". Should it be renamed?
    pub fn bytes_sent(&self) -> Information {
        self.as_ref().bytes_sent()
    }

    /// Returns information amount which was received via this interface.
    // TODO: Method returns `Information`, not the "bytes". Should it be renamed?
    pub fn bytes_recv(&self) -> Information {
        self.as_ref().bytes_recv()
    }

    /// Returns packets amount which was sent via this interface.
    pub fn packets_sent(&self) -> u64 {
        self.as_ref().packets_sent()
    }

    /// Returns packets amount which was sent via this interface.
    pub fn packets_recv(&self) -> u64 {
        self.as_ref().packets_recv()
    }

    // TODO: Not sure about methods names below:

    /// Returns errors amount which had occurred while sending data
    /// via this interface.
    pub fn errors_sent(&self) -> u64 {
        self.as_ref().errors_sent()
    }

    /// Returns errors amount which had occurred while receiving data
    /// via this interface.
    pub fn errors_recv(&self) -> u64 {
        self.as_ref().errors_recv()
    }

    /// Returns packets amount which were dropped while receiving them.
    pub fn drop_recv(&self) -> u64 {
        self.as_ref().drop_recv()
    }
}

impl fmt::Debug for IoCounters {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("IoCounters")
            .field("interface", &self.interface())
            .field("bytes_sent", &self.bytes_sent())
            .field("bytes_recv", &self.bytes_recv())
            .field("packets_sent", &self.packets_sent())
            .field("packets_recv", &self.packets_recv())
            .field("errors_sent", &self.errors_sent())
            .field("errors_recv", &self.errors_recv())
            .field("drop_recv", &self.drop_recv())
            .finish()
    }
}

/// Returns stream which yield [IO counters] for each network interface.
///
/// ## Compatibility
///
/// Windows implementation is missing, see [related issue](https://github.com/heim-rs/heim/issues/26)
///
/// [IO counters]: struct.IoCounters.html
pub fn io_counters() -> impl Stream<Item = Result<IoCounters>> {
    sys::io_counters().map_ok(Into::into)
}