Skip to content

Commit

Permalink
Migrate to core::error::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 13, 2024
1 parent 70d3103 commit 52ab790
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/async-signature.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
matrix:
rust:
- 1.75.0 # MSRV
- 1.81.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/password-hash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
rust:
- 1.60.0 # MSRV
- 1.81.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand Down Expand Up @@ -57,7 +57,7 @@ jobs:
strategy:
matrix:
rust:
- 1.60.0 # MSRV
- 1.81.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/signature.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
matrix:
rust:
- 1.72.0 # MSRV
- 1.81.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
strategy:
matrix:
rust:
- 1.72.0 # MSRV
- 1.81.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion async-signature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
keywords = ["crypto", "ecdsa", "ed25519", "signature", "signing"]
categories = ["cryptography", "no-std"]
edition = "2021"
rust-version = "1.75"
rust-version = "1.81"

[dependencies]
signature = "=2.3.0-pre.4"
Expand Down
4 changes: 2 additions & 2 deletions async-signature/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## Minimum Supported Rust Version

Rust **1.75** or higher.
Rust **1.81** or higher.

Minimum supported Rust version can be changed in the future, but it will be
done with a minor version bump.
Expand All @@ -36,7 +36,7 @@ dual licensed as above, without any additional terms or conditions.
[docs-image]: https://docs.rs/async-signature/badge.svg
[docs-link]: https://docs.rs/async-signature/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.75+-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.81+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260048-signatures
[build-image]: https://github.com/RustCrypto/traits/workflows/async-signature/badge.svg?branch=master&event=push
Expand Down
6 changes: 2 additions & 4 deletions digest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ impl fmt::Display for InvalidOutputSize {
}
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidOutputSize {}
impl core::error::Error for InvalidOutputSize {}

/// Buffer length is not equal to hash output size.
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
Expand All @@ -297,8 +296,7 @@ impl fmt::Display for InvalidBufferSize {
}
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidBufferSize {}
impl core::error::Error for InvalidBufferSize {}

#[cfg(feature = "std")]
mod hashwriter;
Expand Down
3 changes: 1 addition & 2 deletions digest/src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,4 @@ impl fmt::Display for MacError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for MacError {}
impl core::error::Error for MacError {}
3 changes: 1 addition & 2 deletions password-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repository = "https://github.com/RustCrypto/traits"
categories = ["authentication", "cryptography", "no-std"]
keywords = ["crypt", "mcf", "password", "pbkdf", "phc"]
edition = "2021"
rust-version = "1.60"
rust-version = "1.81"

[dependencies]
base64ct = "1.6"
Expand All @@ -27,7 +27,6 @@ rand_core = { version = "0.6.4", optional = true, default-features = false }
[features]
default = ["rand_core"]
alloc = ["base64ct/alloc"]
std = ["alloc", "base64ct/std", "rand_core/std"]

getrandom = ["rand_core/getrandom"]

Expand Down
4 changes: 2 additions & 2 deletions password-hash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ this crate for interoperability:

## Minimum Supported Rust Version

Rust **1.60** or higher.
Rust **1.81** or higher.

Minimum supported Rust version may be changed in the future, but it will be
accompanied by a minor version bump.
Expand Down Expand Up @@ -63,7 +63,7 @@ dual licensed as above, without any additional terms or conditions.
[build-image]: https://github.com/RustCrypto/traits/workflows/password-hash/badge.svg?branch=master&event=push
[build-link]: https://github.com/RustCrypto/traits/actions?query=workflow:password-hash
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.60+-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.81+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260046-password-hashes

Expand Down
11 changes: 5 additions & 6 deletions password-hash/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
impl core::error::Error for Error {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::B64Encoding(err) => Some(err),
// TODO: restore after base64ct will migrate to core::error::Error
// Self::B64Encoding(err) => Some(err),
Self::ParamValueInvalid(err) => Some(err),
Self::SaltInvalid(err) => Some(err),
_ => None,
Expand Down Expand Up @@ -167,5 +167,4 @@ impl fmt::Display for InvalidValue {
}
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidValue {}
impl core::error::Error for InvalidValue {}
2 changes: 0 additions & 2 deletions password-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "rand_core")]
pub use rand_core;
Expand Down
3 changes: 2 additions & 1 deletion signature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
keywords = ["crypto", "ecdsa", "ed25519", "signature", "signing"]
categories = ["cryptography", "no-std"]
edition = "2021"
rust-version = "1.72"
rust-version = "1.81"

[dependencies]
derive = { package = "signature_derive", version = "2", optional = true, path = "../signature_derive" }
Expand All @@ -24,6 +24,7 @@ sha2 = { version = "=0.11.0-pre.4", default-features = false }

[features]
alloc = []
# TODO: remove this feature in the next breaking release
std = ["alloc", "rand_core?/std"]

[package.metadata.docs.rs]
Expand Down
4 changes: 2 additions & 2 deletions signature/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ the [RustCrypto] organization, as well as [`ed25519-dalek`].

## Minimum Supported Rust Version

Rust **1.72** or higher.
Rust **1.81** or higher.

Minimum supported Rust version can be changed in the future, but it will be
done with a minor version bump.
Expand Down Expand Up @@ -56,7 +56,7 @@ dual licensed as above, without any additional terms or conditions.
[build-image]: https://github.com/RustCrypto/traits/actions/workflows/signature.yml/badge.svg
[build-link]: https://github.com/RustCrypto/traits/actions/workflows/signature.yml
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.72+-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.81+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260048-signatures

Expand Down
49 changes: 27 additions & 22 deletions signature/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use core::fmt::{self, Debug, Display};

#[cfg(feature = "std")]
use std::boxed::Box;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;

/// Result type.
///
Expand All @@ -16,17 +16,16 @@ pub type Result<T> = core::result::Result<T, Error>;
/// could potentially be used recover signing private keys or forge signatures
/// (e.g. [BB'06]).
///
/// When the `std` feature is enabled, it impls [`std::error::Error`] and
/// supports an optional [`std::error::Error::source`], which can be used by
/// things like remote signers (e.g. HSM, KMS) to report I/O or auth errors.
/// When the `alloc` feature is enabled, it supports an optional [`core::error::Error::source`],
/// which can be used by things like remote signers (e.g. HSM, KMS) to report I/O or auth errors.
///
/// [BB'06]: https://en.wikipedia.org/wiki/Daniel_Bleichenbacher
#[derive(Default)]
#[non_exhaustive]
pub struct Error {
/// Source of the error (if applicable).
#[cfg(feature = "std")]
source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
#[cfg(feature = "alloc")]
source: Option<Box<dyn core::error::Error + Send + Sync + 'static>>,
}

impl Error {
Expand All @@ -41,9 +40,9 @@ impl Error {
/// errors e.g. signature parsing or verification errors. The intended use
/// cases are for propagating errors related to external signers, e.g.
/// communication/authentication errors with HSMs, KMS, etc.
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
pub fn from_source(
source: impl Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
source: impl Into<Box<dyn core::error::Error + Send + Sync + 'static>>,
) -> Self {
Self {
source: Some(source.into()),
Expand All @@ -52,12 +51,12 @@ impl Error {
}

impl Debug for Error {
#[cfg(not(feature = "std"))]
#[cfg(not(feature = "alloc"))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("signature::Error {}")
}

#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("signature::Error { source: ")?;

Expand All @@ -77,31 +76,37 @@ impl Display for Error {
}
}

#[cfg(feature = "std")]
impl From<Box<dyn std::error::Error + Send + Sync + 'static>> for Error {
fn from(source: Box<dyn std::error::Error + Send + Sync + 'static>) -> Error {
#[cfg(feature = "alloc")]
impl From<Box<dyn core::error::Error + Send + Sync + 'static>> for Error {
fn from(source: Box<dyn core::error::Error + Send + Sync + 'static>) -> Error {
Self::from_source(source)
}
}

#[cfg(feature = "rand_core")]
impl From<rand_core::Error> for Error {
#[cfg(not(feature = "std"))]
#[cfg(not(feature = "alloc"))]
fn from(_source: rand_core::Error) -> Error {
Error::new()
}

#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn from(source: rand_core::Error) -> Error {
Error::from_source(source)
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_ref()
.map(|source| source.as_ref() as &(dyn std::error::Error + 'static))
impl core::error::Error for Error {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
#[cfg(not(feature = "alloc"))]
{
None
}
#[cfg(feature = "alloc")]
{
self.source
.as_ref()
.map(|source| source.as_ref() as &(dyn core::error::Error + 'static))
}
}
}
2 changes: 0 additions & 2 deletions signature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

pub mod hazmat;

Expand Down
3 changes: 0 additions & 3 deletions universal-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ categories = ["cryptography", "no-std"]
crypto-common = "0.2.0-rc.0"
subtle = { version = "2.4", default-features = false }

[features]
std = []

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
6 changes: 1 addition & 5 deletions universal-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#![forbid(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms, missing_debug_implementations)]

#[cfg(feature = "std")]
extern crate std;

pub use crypto_common::{
self, array,
typenum::{self, consts},
Expand Down Expand Up @@ -152,5 +149,4 @@ impl core::fmt::Display for Error {
}
}

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

0 comments on commit 52ab790

Please sign in to comment.