Added Error::description copied c_void impl from core::ffi::c_void

This commit is contained in:
Elichai Turkel 2019-02-18 16:47:11 +02:00
parent e98975a1c0
commit 312b9a55fc
No known key found for this signature in database
GPG Key ID: 5607C93B5F86650C
3 changed files with 32 additions and 9 deletions

View File

@ -17,7 +17,6 @@
//! Direct bindings to the underlying C library functions. These should //! Direct bindings to the underlying C library functions. These should
//! not be needed for most users. //! not be needed for most users.
use core::{mem, hash}; use core::{mem, hash};
use core::ffi::c_void;
use types::*; use types::*;
// use std::os::raw::{c_int, c_uchar, c_uint, c_void}; // use std::os::raw::{c_int, c_uchar, c_uint, c_void};

View File

@ -519,10 +519,9 @@ pub enum Error {
InvalidTweak, InvalidTweak,
} }
// Passthrough Debug to Display, since errors should be user-visible impl Error {
impl fmt::Display for Error { fn as_str(&self) -> &str {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self {
let res = match *self {
Error::IncorrectSignature => "secp: signature failed verification", Error::IncorrectSignature => "secp: signature failed verification",
Error::InvalidMessage => "secp: message was not 32 bytes (do you need to hash?)", Error::InvalidMessage => "secp: message was not 32 bytes (do you need to hash?)",
Error::InvalidPublicKey => "secp: malformed public key", 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::InvalidSecretKey => "secp: malformed or out-of-range secret key",
Error::InvalidRecoveryId => "secp: bad recovery id", Error::InvalidRecoveryId => "secp: bad recovery id",
Error::InvalidTweak => "secp: bad tweak", 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")] #[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. /// Marker trait for indicating that an instance of `Secp256k1` can be used for signing.
pub trait Signing {} pub trait Signing {}

View File

@ -1,5 +1,22 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use core::fmt;
pub type c_int = i32; pub type c_int = i32;
pub type c_uchar = u8; pub type c_uchar = u8;
pub type c_uint = u32; pub type c_uint = u32;
pub use core::ffi::c_void;
/// This is an exact copy of https://doc.rust-lang.org/core/ffi/enum.c_void.html
/// It should be Equivalent to C's void type when used as a pointer.
///
/// We can replace this with `core::ffi::c_void` once we update the rustc version to >=1.30.0.
#[repr(u8)]
pub enum c_void {
#[doc(hidden)] __variant1,
#[doc(hidden)] __variant2,
}
impl fmt::Debug for c_void {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("c_void")
}
}