24
24
//! not reordered or elided the access.
25
25
26
26
use std:: cmp:: min;
27
- use std:: error;
28
- use std:: fmt;
29
27
use std:: io:: { self , Read , Write } ;
30
28
use std:: marker:: PhantomData ;
31
29
use std:: mem:: { align_of, size_of} ;
@@ -43,54 +41,28 @@ use copy_slice_impl::copy_slice;
43
41
44
42
/// `VolatileMemory` related errors.
45
43
#[ allow( missing_docs) ]
46
- #[ derive( Debug ) ]
44
+ #[ derive( Debug , thiserror :: Error ) ]
47
45
pub enum Error {
48
46
/// `addr` is out of bounds of the volatile memory slice.
47
+ #[ error( "address 0x{addr:x} is out of bounds" ) ]
49
48
OutOfBounds { addr : usize } ,
50
49
/// Taking a slice at `base` with `offset` would overflow `usize`.
50
+ #[ error( "address 0x{base:x} offset by 0x{offset:x} would overflow" ) ]
51
51
Overflow { base : usize , offset : usize } ,
52
52
/// Taking a slice whose size overflows `usize`.
53
+ #[ error( "{nelements:?} elements of size {size:?} would overflow a usize" ) ]
53
54
TooBig { nelements : usize , size : usize } ,
54
55
/// Trying to obtain a misaligned reference.
56
+ #[ error( "address 0x{addr:x} is not aligned to {alignment:?}" ) ]
55
57
Misaligned { addr : usize , alignment : usize } ,
56
58
/// Writing to memory failed
59
+ #[ error( "{0}" ) ]
57
60
IOError ( io:: Error ) ,
58
61
/// Incomplete read or write
62
+ #[ error( "only used {completed} bytes in {expected} long buffer" ) ]
59
63
PartialBuffer { expected : usize , completed : usize } ,
60
64
}
61
65
62
- impl fmt:: Display for Error {
63
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
64
- match self {
65
- Error :: OutOfBounds { addr } => write ! ( f, "address 0x{:x} is out of bounds" , addr) ,
66
- Error :: Overflow { base, offset } => write ! (
67
- f,
68
- "address 0x{:x} offset by 0x{:x} would overflow" ,
69
- base, offset
70
- ) ,
71
- Error :: TooBig { nelements, size } => write ! (
72
- f,
73
- "{:?} elements of size {:?} would overflow a usize" ,
74
- nelements, size
75
- ) ,
76
- Error :: Misaligned { addr, alignment } => {
77
- write ! ( f, "address 0x{:x} is not aligned to {:?}" , addr, alignment)
78
- }
79
- Error :: IOError ( error) => write ! ( f, "{}" , error) ,
80
- Error :: PartialBuffer {
81
- expected,
82
- completed,
83
- } => write ! (
84
- f,
85
- "only used {} bytes in {} long buffer" ,
86
- completed, expected
87
- ) ,
88
- }
89
- }
90
- }
91
-
92
- impl error:: Error for Error { }
93
-
94
66
/// Result of volatile memory operations.
95
67
pub type Result < T > = result:: Result < T , Error > ;
96
68
0 commit comments