Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use core::error instead of std::error to increase compatibility in no_std environments #597

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ global-context = ["std"]
# if you are doing a no-std build, then this feature does nothing
# and is not necessary.)
global-context-less-secure = ["global-context"]
core-error = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to use this, I'd want the feature to be named unstable-nightly-core-error and document the limitations.


[dependencies]
secp256k1-sys = { version = "0.8.1", default-features = false, path = "./secp256k1-sys" }
Expand Down
2 changes: 1 addition & 1 deletion contrib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -ex

FEATURES="bitcoin-hashes global-context lowmemory rand recovery serde std alloc bitcoin-hashes-std rand-std"
FEATURES="bitcoin-hashes global-context lowmemory rand recovery serde std alloc bitcoin-hashes-std rand-std core-error"

cargo --version
rustc --version
Expand Down
4 changes: 4 additions & 0 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,10 @@ impl fmt::Display for InvalidParityValue {
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for InvalidParityValue {}

#[cfg(all(feature = "core-error", not(feature = "std")))]
#[cfg_attr(docsrs, doc(cfg(feature = "core-error")))]
impl core::error::Error for InvalidParityValue {}

impl From<InvalidParityValue> for Error {
fn from(error: InvalidParityValue) -> Self { Error::InvalidParityValue(error) }
}
Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(bench, feature(test))]
// Error in no_std
#![cfg_attr(feature = "core-error", feature(error_in_core))]

#[cfg(feature = "alloc")]
extern crate alloc;
Expand Down Expand Up @@ -367,6 +369,25 @@ impl std::error::Error for Error {
}
}
}
#[cfg(all(feature = "core-error", not(feature = "std")))]
#[cfg_attr(docsrs, doc(cfg(feature = "core-error")))]
impl core::error::Error for Error {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Error::IncorrectSignature => None,
Error::InvalidMessage => None,
Error::InvalidPublicKey => None,
Error::InvalidSignature => None,
Error::InvalidSecretKey => None,
Error::InvalidSharedSecret => None,
Error::InvalidRecoveryId => None,
Error::InvalidTweak => None,
Error::NotEnoughMemory => None,
Error::InvalidPublicKeySum => None,
Error::InvalidParityValue(error) => Some(error),
}
}
}
Copy link
Collaborator

@Kixunil Kixunil Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why duplicate this? You could just modify the condition above. Oh, brainfart, it's obvious. You could just cfg use std::error::Error as StdError/use core::error::Error as StdError and change impl to say StdError.


/// The secp256k1 engine, used to execute all signature operations.
pub struct Secp256k1<C: Context> {
Expand Down