1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use core::fmt;
#[cfg(feature = "alloc")]
use alloc::collections::TryReserveError;
#[derive(Debug, Clone)]
pub struct Error {
repr: ErrorRepr,
}
impl fmt::Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.repr {
#[cfg(feature = "alloc")]
ErrorRepr::Allocate(_) => f.write_str("memory allocation failed"),
ErrorRepr::BufferFull(_) => f.write_str("buffer full"),
}
}
}
impl From<BufferTooSmallError> for Error {
#[inline]
fn from(e: BufferTooSmallError) -> Self {
Self {
repr: ErrorRepr::BufferFull(e),
}
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<TryReserveError> for Error {
#[inline]
fn from(e: TryReserveError) -> Self {
Self {
repr: ErrorRepr::Allocate(e),
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.repr {
#[cfg(feature = "alloc")]
ErrorRepr::Allocate(e) => Some(e),
ErrorRepr::BufferFull(e) => Some(e),
}
}
}
#[derive(Debug, Clone)]
pub(crate) enum ErrorRepr {
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
Allocate(TryReserveError),
BufferFull(BufferTooSmallError),
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct BufferTooSmallError(());
impl BufferTooSmallError {
#[inline]
#[must_use]
pub(crate) fn new() -> Self {
Self(())
}
}
impl fmt::Display for BufferTooSmallError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("destination buffer does not have enough capacity")
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for BufferTooSmallError {}