pub struct Sampler { /* private fields */ }Expand description
Measures download and upload between samples. Owned by the caller: two
samplers never disturb each other. sample never blocks and allocates
nothing.
use std::time::Duration;
let mut net = sys_lib::net::Sampler::new()?;
loop {
std::thread::sleep(Duration::from_secs(5));
let t = net.sample()?;
println!("down {} up {}", t.download, t.upload);
for i in net.sample_per_interface()? {
println!(" {}: down {} up {}", i.interface, i.traffic.download, i.traffic.upload);
}
}Implementations§
Source§impl Sampler
impl Sampler
Sourcepub fn sample(&mut self) -> Result<Traffic>
pub fn sample(&mut self) -> Result<Traffic>
Download and upload since the previous sample, every interface but loopback. A counter that went backwards re-bases the sampler and reads as no traffic for that interval.
§Errors
TooSoon when no time passed since the previous sample.
Sourcepub fn sample_per_interface(&mut self) -> Result<Vec<InterfaceTraffic>>
pub fn sample_per_interface(&mut self) -> Result<Vec<InterfaceTraffic>>
Traffic since the previous per-interface sample, for each interface
(allocates the Vec). An interface seen for the first time reads as no
traffic.
§Errors
TooSoon when no time passed since the previous sample.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Sampler
impl RefUnwindSafe for Sampler
impl Send for Sampler
impl Sync for Sampler
impl Unpin for Sampler
impl UnsafeUnpin for Sampler
impl UnwindSafe for Sampler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more