1 // Copyright 2018 Developers of the Rand project.
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
12 use core
::num
::NonZeroU32
;
15 /// Error type of random number generators
17 /// In order to be compatible with `std` and `no_std`, this type has two
18 /// possible implementations: with `std` a boxed `Error` trait object is stored,
19 /// while with `no_std` we merely store an error code.
22 inner
: Box
<dyn std
::error
::Error
+ Send
+ Sync
+ '
static>,
23 #[cfg(not(feature="std"))]
28 /// Construct from any type supporting `std::error::Error`
30 /// Available only when configured with `std`.
32 /// See also `From<NonZeroU32>`, which is available with and without `std`.
35 pub fn new
<E
>(err
: E
) -> Self
36 where E
: Into
<Box
<dyn std
::error
::Error
+ Send
+ Sync
+ '
static>>
38 Error { inner: err.into() }
41 /// Reference the inner error (`std` only)
43 /// When configured with `std`, this is a trivial operation and never
44 /// panics. Without `std`, this method is simply unavailable.
47 pub fn inner(&self) -> &(dyn std
::error
::Error
+ Send
+ Sync
+ '
static) {
51 /// Unwrap the inner error (`std` only)
53 /// When configured with `std`, this is a trivial operation and never
54 /// panics. Without `std`, this method is simply unavailable.
57 pub fn take_inner(self) -> Box
<dyn std
::error
::Error
+ Send
+ Sync
+ '
static> {
61 /// Codes below this point represent OS Errors (i.e. positive i32 values).
62 /// Codes at or above this point, but below [`Error::CUSTOM_START`] are
63 /// reserved for use by the `rand` and `getrandom` crates.
64 pub const INTERNAL_START
: u32 = 1 << 31;
66 /// Codes at or above this point can be used by users to define their own
68 pub const CUSTOM_START
: u32 = (1 << 31) + (1 << 30);
70 /// Extract the raw OS error code (if this error came from the OS)
72 /// This method is identical to `std::io::Error::raw_os_error()`, except
73 /// that it works in `no_std` contexts. If this method returns `None`, the
74 /// error value can still be formatted via the `Diplay` implementation.
76 pub fn raw_os_error(&self) -> Option
<i32> {
77 #[cfg(feature="std")] {
78 if let Some(e
) = self.inner
.downcast_ref
::<std
::io
::Error
>() {
79 return e
.raw_os_error();
83 Some(code
) if u32::from(code
) < Self::INTERNAL_START
=>
84 Some(u32::from(code
) as i32),
89 /// Retrieve the error code, if any.
91 /// If this `Error` was constructed via `From<NonZeroU32>`, then this method
92 /// will return this `NonZeroU32` code (for `no_std` this is always the
93 /// case). Otherwise, this method will return `None`.
95 pub fn code(&self) -> Option
<NonZeroU32
> {
96 #[cfg(feature="std")] {
97 self.inner
.downcast_ref
::<ErrorCode
>().map(|c
| c
.0)
99 #[cfg(not(feature="std"))] {
105 impl fmt
::Debug
for Error
{
106 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
107 #[cfg(feature="std")] {
108 write
!(f
, "Error {{ inner: {:?} }}", self.inner
)
110 #[cfg(all(feature="getrandom", not(feature="std")))] {
111 getrandom
::Error
::from(self.code
).fmt(f
)
113 #[cfg(not(feature="getrandom"))] {
114 write
!(f
, "Error {{ code: {} }}", self.code
)
119 impl fmt
::Display
for Error
{
120 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
121 #[cfg(feature="std")] {
122 write
!(f
, "{}", self.inner
)
124 #[cfg(all(feature="getrandom", not(feature="std")))] {
125 getrandom
::Error
::from(self.code
).fmt(f
)
127 #[cfg(not(feature="getrandom"))] {
128 write
!(f
, "error code {}", self.code
)
133 impl From
<NonZeroU32
> for Error
{
135 fn from(code
: NonZeroU32
) -> Self {
136 #[cfg(feature="std")] {
137 Error { inner: Box::new(ErrorCode(code)) }
139 #[cfg(not(feature="std"))] {
145 #[cfg(feature="getrandom")]
146 impl From
<getrandom
::Error
> for Error
{
148 fn from(error
: getrandom
::Error
) -> Self {
149 #[cfg(feature="std")] {
150 Error { inner: Box::new(error) }
152 #[cfg(not(feature="std"))] {
153 Error { code: error.code() }
158 #[cfg(feature="std")]
159 impl std
::error
::Error
for Error
{
161 fn source(&self) -> Option
<&(dyn std
::error
::Error
+ '
static)> {
166 #[cfg(feature="std")]
167 impl From
<Error
> for std
::io
::Error
{
169 fn from(error
: Error
) -> Self {
170 if let Some(code
) = error
.raw_os_error() {
171 std
::io
::Error
::from_raw_os_error(code
)
173 std
::io
::Error
::new(std
::io
::ErrorKind
::Other
, error
)
178 #[cfg(feature="std")]
179 #[derive(Debug, Copy, Clone)]
180 struct ErrorCode(NonZeroU32
);
182 #[cfg(feature="std")]
183 impl fmt
::Display
for ErrorCode
{
184 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
185 write
!(f
, "error code {}", self.0)
189 #[cfg(feature="std")]
190 impl std
::error
::Error
for ErrorCode {}