buf_list/
errors.rs

1// Copyright (c) 2018 the linkerd2-proxy authors
2// Copyright (c) The buf-list Contributors
3// SPDX-License-Identifier: Apache-2.0
4
5//! Error types returned by buf-list.
6
7use std::{error, fmt};
8
9/// An error returned if `read_exact` was called on a [`Cursor`](crate::Cursor) that doesn't have
10/// enough bytes remaining.
11///
12/// This is private because `read_exact_impl` returns an `io::Error`.
13#[derive(Debug)]
14pub(crate) struct ReadExactError {
15    pub(crate) remaining: u64,
16    pub(crate) buf_len: usize,
17}
18
19impl error::Error for ReadExactError {
20    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
21        None
22    }
23}
24
25impl fmt::Display for ReadExactError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        write!(
28            f,
29            "Cursor had {} bytes remaining but buffer was {} bytes long",
30            self.remaining, self.buf_len
31        )
32    }
33}