Skip to content

Commit 6555ab2

Browse files
authored
Switch from std::error::Error to core::error::Error (#767)
The latter was being used in some places, but not consistently. Notably this gets rid of the `std` dependency on various test examples.
1 parent 0fe4838 commit 6555ab2

File tree

7 files changed

+34
-36
lines changed

7 files changed

+34
-36
lines changed

argon2/src/lib.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@
3232
//! password-based authentication. Do not use this API to derive cryptographic
3333
//! keys: see the "key derivation" usage example below.
3434
//!
35-
#![cfg_attr(all(feature = "password-hash", feature = "std"), doc = "```")]
36-
#![cfg_attr(
37-
not(all(feature = "password-hash", feature = "std")),
38-
doc = "```ignore"
39-
)]
40-
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
35+
#![cfg_attr(all(feature = "alloc", feature = "getrandom"), doc = "```")]
36+
#![cfg_attr(not(all(feature = "alloc", feature = "getrandom")), doc = "```ignore")]
37+
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
4138
//! use argon2::{
4239
//! password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, phc::Salt},
4340
//! Argon2
@@ -66,12 +63,9 @@
6663
//!
6764
//! [pepper]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#peppering
6865
//!
69-
#![cfg_attr(all(feature = "password-hash", feature = "std"), doc = "```")]
70-
#![cfg_attr(
71-
not(all(feature = "password-hash", feature = "std")),
72-
doc = "```ignore"
73-
)]
74-
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
66+
#![cfg_attr(all(feature = "alloc", feature = "getrandom"), doc = "```")]
67+
#![cfg_attr(not(all(feature = "alloc", feature = "getrandom")), doc = "```ignore")]
68+
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
7569
//! use argon2::{
7670
//! password_hash::{
7771
//! phc::{PasswordHash, Salt},
@@ -118,12 +112,9 @@
118112
//! This API is useful for transforming a password into cryptographic keys for
119113
//! e.g. password-based encryption.
120114
//!
121-
#![cfg_attr(all(feature = "password-hash", feature = "std"), doc = "```")]
122-
#![cfg_attr(
123-
not(all(feature = "password-hash", feature = "std")),
124-
doc = "```ignore"
125-
)]
126-
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
115+
#![cfg_attr(feature = "alloc", doc = "```")]
116+
#![cfg_attr(not(feature = "alloc"), doc = "```ignore")]
117+
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
127118
//! use argon2::Argon2;
128119
//!
129120
//! let password = b"hunter42"; // Bad password; don't actually use!

balloon-hash/src/error.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,4 @@ impl From<Error> for password_hash::Error {
5757
}
5858
}
5959

60-
#[cfg(feature = "std")]
61-
impl std::error::Error for Error {}
60+
impl core::error::Error for Error {}

balloon-hash/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//!
3131
#![cfg_attr(feature = "getrandom", doc = "```")]
3232
#![cfg_attr(not(feature = "getrandom"), doc = "```ignore")]
33-
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
33+
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
3434
//! use balloon_hash::{
3535
//! password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, phc::Salt},
3636
//! Balloon

bcrypt-pbkdf/src/errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ impl fmt::Display for Error {
2424
}
2525
}
2626

27-
#[cfg(feature = "std")]
28-
impl std::error::Error for Error {}
27+
impl core::error::Error for Error {}

password-auth/src/errors.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ impl fmt::Display for ParseError {
3030
}
3131
}
3232

33+
impl core::error::Error for ParseError {
34+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
35+
Some(&self.0)
36+
}
37+
}
38+
3339
/// Password verification errors.
3440
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
3541
pub enum VerifyError {
@@ -55,8 +61,11 @@ impl From<ParseError> for VerifyError {
5561
}
5662
}
5763

58-
#[cfg(feature = "std")]
59-
impl std::error::Error for ParseError {}
60-
61-
#[cfg(feature = "std")]
62-
impl std::error::Error for VerifyError {}
64+
impl core::error::Error for VerifyError {
65+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
66+
match self {
67+
Self::Parse(err) => Some(err),
68+
_ => None,
69+
}
70+
}
71+
}

scrypt/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ phc = { version = "0.6.0-rc.0", optional = true, features = ["rand_core"] }
2525

2626
[features]
2727
default = ["simple", "rayon"]
28+
alloc = ["password-hash?/alloc"]
29+
30+
getrandom = ["simple", "phc/getrandom"]
2831
rayon = ["dep:rayon"]
2932
simple = ["dep:password-hash", "dep:phc"]
3033

scrypt/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,25 @@
1212
//!
1313
//! # Usage (simple with default params)
1414
//!
15-
//! ```
16-
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
17-
//! # #[cfg(all(feature = "simple", feature = "std"))]
18-
//! # {
15+
#![cfg_attr(all(feature = "alloc", feature = "getrandom"), doc = "```")]
16+
#![cfg_attr(not(all(feature = "alloc", feature = "getrandom")), doc = "```ignore")]
17+
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
1918
//! use scrypt::{
2019
//! password_hash::{
21-
//! rand_core::OsRng,
22-
//! PasswordHash, PasswordHasher, PasswordVerifier, SaltString
20+
//! PasswordHasher, PasswordVerifier, phc::{PasswordHash, Salt}
2321
//! },
2422
//! Scrypt
2523
//! };
2624
//!
2725
//! let password = b"hunter42"; // Bad password; don't actually use!
28-
//! let salt = SaltString::generate(&mut OsRng);
26+
//! let salt = Salt::generate();
2927
//!
3028
//! // Hash password to PHC string ($scrypt$...)
3129
//! let password_hash = Scrypt.hash_password(password, &salt)?.to_string();
3230
//!
3331
//! // Verify password against PHC string
3432
//! let parsed_hash = PasswordHash::new(&password_hash)?;
3533
//! assert!(Scrypt.verify_password(password, &parsed_hash).is_ok());
36-
//! # }
3734
//! # Ok(())
3835
//! # }
3936
//! ```

0 commit comments

Comments
 (0)