tokio_splice2/
traffic.rs

1//! Traffic transmitted result.
2
3use std::io;
4
5#[derive(Debug)]
6/// Traffic transmitted throughout the `splice(2)` operation, regardless of any
7/// errors.
8pub struct TrafficResult {
9    /// The number of bytes that have been transferred from a to b
10    pub tx: usize,
11
12    /// The number of bytes that have been transferred from b to a.
13    pub rx: usize,
14
15    /// The error that occurred during the `splice(2)` operation, if any.
16    pub error: Option<io::Error>,
17}
18
19impl TrafficResult {
20    #[must_use]
21    #[inline]
22    /// Merges two `TrafficResult` instances.
23    pub fn merge(self, other: Self) -> Self {
24        Self {
25            tx: self.tx + other.tx,
26            rx: self.rx + other.rx,
27            error: self.error.or(other.error),
28        }
29    }
30
31    #[must_use]
32    #[inline]
33    /// Returns the total number of bytes transmitted in both directions.
34    pub const fn sum(&self) -> usize {
35        self.tx.saturating_add(self.rx)
36    }
37
38    #[inline]
39    /// Turns the `TrafficResult` into an `io::Result<usize>`.
40    ///
41    /// ## Errors
42    ///
43    /// Extracts the error from the `TrafficResult` if it exists.
44    pub fn into_result(self) -> io::Result<Self> {
45        if let Some(err) = self.error {
46            Err(err)
47        } else {
48            Ok(TrafficResult {
49                error: None,
50                ..self
51            })
52        }
53    }
54}