From 4a8599b7058477734d03e5f5ebab97084340b517 Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Mon, 18 Feb 2019 16:47:11 +0200 Subject: [PATCH] Added Error::description impl and c_void as c_uchar --- src/ffi.rs | 1 - src/lib.rs | 21 ++++++++++++++------- src/types.rs | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 2bba67086..f173300c7 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -17,7 +17,6 @@ //! Direct bindings to the underlying C library functions. These should //! not be needed for most users. use core::{mem, hash}; -use core::ffi::c_void; use types::*; // use std::os::raw::{c_int, c_uchar, c_uint, c_void}; diff --git a/src/lib.rs b/src/lib.rs index 52a4e1372..c65d168b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -519,10 +519,9 @@ pub enum Error { InvalidTweak, } -// Passthrough Debug to Display, since errors should be user-visible -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - let res = match *self { +impl Error { + fn as_str(&self) -> &str { + match *self { Error::IncorrectSignature => "secp: signature failed verification", Error::InvalidMessage => "secp: message was not 32 bytes (do you need to hash?)", Error::InvalidPublicKey => "secp: malformed public key", @@ -530,13 +529,21 @@ impl fmt::Display for Error { Error::InvalidSecretKey => "secp: malformed or out-of-range secret key", Error::InvalidRecoveryId => "secp: bad recovery id", Error::InvalidTweak => "secp: bad tweak", - }; - f.write_str(res) + } + } +} + +// Passthrough Debug to Display, since errors should be user-visible +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + f.write_str(self.as_str()) } } #[cfg(feature = "std")] -impl std::error::Error for Error {} +impl std::error::Error for Error { + fn description(&self) -> &str { self.as_str() } +} /// Marker trait for indicating that an instance of `Secp256k1` can be used for signing. pub trait Signing {} diff --git a/src/types.rs b/src/types.rs index 9bcb3b91b..2b8e848a5 100644 --- a/src/types.rs +++ b/src/types.rs @@ -2,4 +2,4 @@ pub type c_int = i32; pub type c_uchar = u8; pub type c_uint = u32; -pub use core::ffi::c_void; \ No newline at end of file +pub type c_void = c_uchar; \ No newline at end of file