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
96
97
98
use std::fmt;
use std::fmt::{Display, Formatter};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub struct Error {
kind: ErrorType,
}
impl Error {
pub fn kind(&self) -> ErrorType {
self.kind.clone()
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self.kind, f)
}
}
impl std::error::Error for Error {}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ErrorType {
AnnotatedBuiltinType,
AnnotatedOpaqueType,
AnnotatedArray,
SizeOverflow,
PowerOfTwoAlignment,
SubByteAlignment,
SubByteSize,
MultiplePragmaPackedAnnotations,
NamedZeroSizeBitField,
UnnamedRegularField,
OversizedBitfield,
PragmaPackedField,
}
impl Display for ErrorType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use ErrorType::*;
let s = match self {
AnnotatedBuiltinType => "Builtin types cannot have annotations",
AnnotatedOpaqueType => "Opaque types cannot have annotations",
AnnotatedArray => "Arrays cannot have annotations",
SizeOverflow => "The object size in bits overflows u64",
PowerOfTwoAlignment => "Alignments must be a power of two",
SubByteAlignment => "Alignments must be at least 8",
SubByteSize => "Sizes must be a multiple of 8",
PragmaPackedField => "Fields cannot have pragma_pack annotations",
MultiplePragmaPackedAnnotations => {
"A type/field can have at most one packed annotation"
}
NamedZeroSizeBitField => "A zero-sized bit-field cannot be named",
UnnamedRegularField => "Regular fields must be named",
OversizedBitfield => {
"The width of a bit-field cannot be larger than the width of the underlying type"
}
};
f.write_str(s)
}
}
pub(crate) fn err(kind: ErrorType) -> Error {
Error { kind }
}