tokio_splice2/
utils.rs

1//! Some utils.
2
3use std::io;
4use std::num::NonZeroUsize;
5
6#[derive(Debug)]
7pub(crate) enum Offset {
8    None,
9    /// Read offset set.
10    In(Option<u64>),
11    /// Write offset set.
12    Out(Option<u64>),
13}
14
15impl Offset {
16    #[inline]
17    pub(crate) fn off_in(&mut self) -> Option<&mut u64> {
18        match self {
19            Offset::In(off) => off.as_mut(),
20            _ => None,
21        }
22    }
23
24    #[inline]
25    pub(crate) fn off_out(&mut self) -> Option<&mut u64> {
26        match self {
27            Offset::Out(off) => off.as_mut(),
28            _ => None,
29        }
30    }
31
32    pub(crate) fn calc_size_to_splice(
33        f_len: u64,
34        f_offset_start: Option<u64>,
35        f_offset_end: Option<u64>,
36    ) -> io::Result<u64> {
37        match (f_offset_start, f_offset_end) {
38            (Some(start), Some(end)) => {
39                if start > end || end > f_len {
40                    return Err(io::Error::new(
41                        io::ErrorKind::InvalidInput,
42                        "invalid offset range",
43                    ));
44                }
45                Ok(end - start)
46            }
47            (Some(start), None) => {
48                if start > f_len {
49                    return Err(io::Error::new(
50                        io::ErrorKind::InvalidInput,
51                        "invalid offset start",
52                    ));
53                }
54                Ok(f_len - start)
55            }
56            (None, Some(end)) => {
57                if end > f_len {
58                    return Err(io::Error::new(
59                        io::ErrorKind::InvalidInput,
60                        "invalid offset end",
61                    ));
62                }
63                Ok(end)
64            }
65            (None, None) => Ok(f_len),
66        }
67    }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub(crate) enum Drained {
72    /// New data has been read from `r` into pipe.
73    Some(NonZeroUsize),
74
75    /// Indicates that the draining process is complete and no more data can be
76    /// read from the `r` into pipe.
77    Done,
78}