Skip to main content

bytes_handoff/
error.rs

1use bytes::Bytes;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum BufferError {
6    #[error("buffer limit exceeded: attempted {attempted} bytes, limit {limit} bytes")]
7    LimitExceeded { attempted: usize, limit: usize },
8    #[error("split prefix out of bounds: requested {requested} bytes, available {available} bytes")]
9    SplitOutOfBounds { requested: usize, available: usize },
10    #[error("read error: {0}")]
11    Io(#[from] std::io::Error),
12}
13
14#[derive(Debug, Error)]
15pub enum WriteError {
16    #[error("write handoff is closed")]
17    Closed,
18    #[error("write exceeds byte budget: attempted {attempted} bytes, limit {limit} bytes")]
19    ByteBudgetExceeded { attempted: usize, limit: usize },
20    #[error("write error: {0}")]
21    Io(#[from] std::io::Error),
22}
23
24#[derive(Debug, Error)]
25#[error("write handoff backpressure: {reason}")]
26pub struct WriteBackpressure {
27    reason: BackpressureReason,
28    bytes: Bytes,
29}
30
31impl WriteBackpressure {
32    pub(crate) fn closed(bytes: Bytes) -> Self {
33        Self {
34            reason: BackpressureReason::Closed,
35            bytes,
36        }
37    }
38
39    pub(crate) fn queue_full(bytes: Bytes) -> Self {
40        Self {
41            reason: BackpressureReason::QueueFull,
42            bytes,
43        }
44    }
45
46    pub(crate) fn byte_budget_exceeded(bytes: Bytes, attempted: usize, limit: usize) -> Self {
47        Self {
48            reason: BackpressureReason::ByteBudgetExceeded { attempted, limit },
49            bytes,
50        }
51    }
52
53    pub fn into_bytes(self) -> Bytes {
54        self.bytes
55    }
56
57    pub fn reason(&self) -> BackpressureReason {
58        self.reason
59    }
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63pub enum BackpressureReason {
64    Closed,
65    QueueFull,
66    ByteBudgetExceeded { attempted: usize, limit: usize },
67}
68
69impl std::fmt::Display for BackpressureReason {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        match self {
72            Self::Closed => f.write_str("closed"),
73            Self::QueueFull => f.write_str("queue full"),
74            Self::ByteBudgetExceeded { attempted, limit } => {
75                write!(
76                    f,
77                    "byte budget exceeded: attempted {attempted} bytes, limit {limit} bytes"
78                )
79            }
80        }
81    }
82}