ring/error/unspecified.rs
1// Copyright 2016-2024 Brian Smith.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15#[cfg(feature = "std")]
16extern crate std;
17
18/// An error with absolutely no details.
19///
20/// *ring* uses this unit type as the error type in most of its results
21/// because (a) usually the specific reasons for a failure are obvious or are
22/// not useful to know, and/or (b) providing more details about a failure might
23/// provide a dangerous side channel, and/or (c) it greatly simplifies the
24/// error handling logic.
25///
26/// `Result<T, ring::error::Unspecified>` is mostly equivalent to
27/// `Result<T, ()>`. However, `ring::error::Unspecified` implements
28/// [`std::error::Error`] and users of *ring* can implement
29/// `From<ring::error::Unspecified>` to map this to their own error types, as
30/// described in [“Error Handling” in the Rust Book]:
31///
32/// ```
33/// use ring::rand::{self, SecureRandom};
34///
35/// enum Error {
36/// CryptoError,
37///
38/// # #[cfg(feature = "alloc")]
39/// IOError(std::io::Error),
40/// // [...]
41/// }
42///
43/// impl From<ring::error::Unspecified> for Error {
44/// fn from(_: ring::error::Unspecified) -> Self { Error::CryptoError }
45/// }
46///
47/// fn eight_random_bytes() -> Result<[u8; 8], Error> {
48/// let rng = rand::SystemRandom::new();
49/// let mut bytes = [0; 8];
50///
51/// // The `From<ring::error::Unspecified>` implementation above makes this
52/// // equivalent to
53/// // `rng.fill(&mut bytes).map_err(|_| Error::CryptoError)?`.
54/// rng.fill(&mut bytes)?;
55///
56/// Ok(bytes)
57/// }
58///
59/// assert!(eight_random_bytes().is_ok());
60/// ```
61///
62/// Experience with using and implementing other crypto libraries like has
63/// shown that sophisticated error reporting facilities often cause significant
64/// bugs themselves, both within the crypto library and within users of the
65/// crypto library. This approach attempts to minimize complexity in the hopes
66/// of avoiding such problems. In some cases, this approach may be too extreme,
67/// and it may be important for an operation to provide some details about the
68/// cause of a failure. Users of *ring* are encouraged to report such cases so
69/// that they can be addressed individually.
70///
71/// [`std::error::Error`]: https://doc.rust-lang.org/std/error/trait.Error.html
72/// [“Error Handling” in the Rust Book]:
73/// https://doc.rust-lang.org/book/first-edition/error-handling.html#the-from-trait
74#[derive(Clone, Copy, Debug, PartialEq)]
75pub struct Unspecified;
76
77// This is required for the implementation of `std::error::Error`.
78impl core::fmt::Display for Unspecified {
79 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
80 f.write_str("ring::error::Unspecified")
81 }
82}
83
84#[cfg(feature = "std")]
85impl std::error::Error for Unspecified {}