From 82fe77fbba23ab7eb0a4bf927dc211845ebd7e3f Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 9 Oct 2019 15:47:26 -0700 Subject: [PATCH] signature: Simplify alloc gating Unconditionally link to liballoc when the std feature is enabled and always access `Vec` as `alloc::vec::Vec`. MSRV 1.36+ --- .travis.yml | 27 ++++++++------------- signature-crate/README.md | 20 +++++++++------- signature-crate/src/error.rs | 41 +++++++++++++++----------------- signature-crate/src/lib.rs | 16 +++---------- signature-crate/src/prelude.rs | 7 ------ signature-crate/src/signature.rs | 2 +- 6 files changed, 45 insertions(+), 68 deletions(-) delete mode 100644 signature-crate/src/prelude.rs diff --git a/.travis.yml b/.travis.yml index db686246..77e5c1e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,17 +2,11 @@ language: rust cache: cargo rust: - - 1.31.0 + - 1.36.0 - stable - beta - nightly -install: - - rustup component add rustfmt - - rustup component add clippy - - rustup target add thumbv7em-none-eabihf - - rustup target add wasm32-unknown-unknown - script: - cargo test --verbose --release - cargo test --verbose --all-features --release @@ -25,23 +19,22 @@ matrix: include: - name: "Rust: stable (thumbv7em-none-eabihf)" rust: stable - script: - - cd signature-crate && cargo build --target thumbv7em-none-eabihf --no-default-features --release + install: rustup target add thumbv7em-none-eabihf + script: cd signature-crate && cargo build --target thumbv7em-none-eabihf --no-default-features --release - name: "Rust: stable (wasm32-unknown-unknown)" rust: stable - script: - - cd signature-crate && cargo build --target wasm32-unknown-unknown --no-default-features --release + install: rustup target add wasm32-unknown-unknown + script: cd signature-crate && cargo build --target wasm32-unknown-unknown --no-default-features --release - name: rustfmt rust: stable - script: - - cargo fmt --all -- --check + install: rustup component add rustfmt + script: cargo fmt --all -- --check - name: clippy + install: rustup component add clippy rust: stable - script: - - cargo clippy --all + script: cargo clippy --all - name: docs - script: - - cargo doc --all --no-deps + script: cargo doc --all --no-deps branches: only: diff --git a/signature-crate/README.md b/signature-crate/README.md index 69dc6199..c813b14a 100644 --- a/signature-crate/README.md +++ b/signature-crate/README.md @@ -7,14 +7,17 @@ [![Build Status][build-image]][build-link] This crate contains traits which provide generic, object-safe APIs for -generating and verifying [digital signatures]. +generating and verifying [digital signatures][1]. -The long-term goal is to use this crate in conjunction with the -[`ecdsa`][ecdsa-crate] and [`ed25519`][ed25519-crate], however those crates -are a work-in-progress. +It's presently useful in conjunction with the [`ed25519`][2] crate. +Support is also planned for the [`ecdsa`][3] and [`rsa`][4] crates. [Documentation][docs-link] +## Requirements + +- Rust **1.36+** + ## License All crates licensed under either of @@ -37,12 +40,13 @@ dual licensed as above, without any additional terms or conditions. [docs-image]: https://docs.rs/signature/badge.svg [docs-link]: https://docs.rs/signature/ [license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg -[rustc-image]: https://img.shields.io/badge/rustc-1.31+-blue.svg +[rustc-image]: https://img.shields.io/badge/rustc-1.36+-blue.svg [build-image]: https://travis-ci.org/RustCrypto/signatures.svg?branch=master [build-link]: https://travis-ci.org/RustCrypto/signatures [//]: # (general links) -[digital signatures]: https://en.wikipedia.org/wiki/Digital_signature -[ecdsa-crate]: https://github.com/RustCrypto/signatures/tree/master/ecdsa -[ed25519-crate]: https://github.com/RustCrypto/signatures/tree/master/ed25519 +[1]: https://en.wikipedia.org/wiki/Digital_signature +[2]: https://github.com/RustCrypto/signatures/tree/master/ed25519 +[3]: https://github.com/RustCrypto/signatures/tree/master/ecdsa +[4]: https://github.com/RustCrypto/RSA diff --git a/signature-crate/src/error.rs b/signature-crate/src/error.rs index 9a7be906..4889d369 100644 --- a/signature-crate/src/error.rs +++ b/signature-crate/src/error.rs @@ -3,14 +3,14 @@ use core::fmt::{self, Display}; #[cfg(feature = "std")] -use std::{boxed::Box, error::Error as StdError}; +use std::boxed::Box; /// Signature errors #[derive(Debug, Default)] pub struct Error { /// Cause of the error (if applicable) #[cfg(feature = "std")] - cause: Option>, + cause: Option>, } impl Error { @@ -23,7 +23,7 @@ impl Error { #[cfg(feature = "std")] pub fn from_cause(cause: E) -> Self where - E: Into>, + E: Into>, { Self { cause: Some(cause.into()), @@ -32,7 +32,7 @@ impl Error { /// Borrow the error's underlying cause (if available) #[cfg(feature = "std")] - pub fn cause(&self) -> Option<&dyn StdError> { + pub fn cause(&self) -> Option<&dyn std::error::Error> { self.cause.as_ref().map(|c| c.as_ref()) } @@ -40,16 +40,16 @@ impl Error { /// /// Panics if the error does not have a cause. #[cfg(feature = "std")] - pub fn into_cause(self) -> Box { + pub fn into_cause(self) -> Box { self.cause .expect("into_cause called on an error with no cause") } /// Attempt to downcast this error's cause into a concrete type #[cfg(feature = "std")] - pub fn downcast(self) -> Result, Box> + pub fn downcast(self) -> Result, Box> where - T: StdError + 'static, + T: std::error::Error + 'static, { self.cause .map(|cause| cause.downcast()) @@ -60,33 +60,30 @@ impl Error { #[cfg(feature = "std")] pub fn downcast_ref(&self) -> Option<&T> where - T: StdError + 'static, + T: std::error::Error + 'static, { self.cause.as_ref().and_then(|cause| cause.downcast_ref()) } } -#[cfg(not(feature = "std"))] impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "signature error") - } -} + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "signature error")?; -#[cfg(feature = "std")] -impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if let Some(ref cause) = self.cause { - write!(f, "{}", cause) - } else { - write!(f, "signature error") + #[cfg(feature = "std")] + { + if let Some(ref cause) = self.cause { + write!(f, ": {}", cause)?; + } } + + Ok(()) } } #[cfg(feature = "std")] -impl StdError for Error { - fn cause(&self) -> Option<&dyn StdError> { +impl std::error::Error for Error { + fn cause(&self) -> Option<&dyn std::error::Error> { self.cause.as_ref().map(|cause| cause.as_ref()) } } diff --git a/signature-crate/src/lib.rs b/signature-crate/src/lib.rs index f564e53d..05128acd 100644 --- a/signature-crate/src/lib.rs +++ b/signature-crate/src/lib.rs @@ -3,20 +3,11 @@ //! cryptography. #![no_std] -#![deny( - warnings, - missing_docs, - trivial_casts, - trivial_numeric_casts, - unsafe_code, - unused_import_braces, - unused_qualifications -)] +#![forbid(unsafe_code)] +#![warn(missing_docs, rust_2018_idioms, unused_qualifications)] #![doc(html_root_url = "https://docs.rs/signature/0.2.0")] -#[cfg(all(feature = "alloc", not(feature = "std")))] -#[allow(unused_imports)] // rustc bug? -#[macro_use] +#[cfg(feature = "alloc")] extern crate alloc; #[cfg(feature = "std")] @@ -36,7 +27,6 @@ pub use signature_derive::{Signer, Verifier}; pub use digest; mod error; -mod prelude; mod signature; mod signer; mod verifier; diff --git a/signature-crate/src/prelude.rs b/signature-crate/src/prelude.rs deleted file mode 100644 index 9c1bedd7..00000000 --- a/signature-crate/src/prelude.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! Crate-local prelude (for alloc-dependent features like `Vec`) - -#[cfg(all(feature = "alloc", not(feature = "std")))] -pub use alloc::vec::Vec; - -#[cfg(feature = "std")] -pub use std::vec::Vec; diff --git a/signature-crate/src/signature.rs b/signature-crate/src/signature.rs index 8376a5a4..6ceaad32 100644 --- a/signature-crate/src/signature.rs +++ b/signature-crate/src/signature.rs @@ -2,7 +2,7 @@ use core::fmt::Debug; use crate::error::Error; #[cfg(feature = "alloc")] -use crate::prelude::*; +use alloc::vec::Vec; /// Trait impl'd by concrete types that represent digital signatures pub trait Signature: AsRef<[u8]> + Debug + Sized {