Skip to content

Commit f1a81d5

Browse files
committed
Added Error::description copied c_void impl from core::ffi::c_void
1 parent e98975a commit f1a81d5

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/ffi.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
//! Direct bindings to the underlying C library functions. These should
1818
//! not be needed for most users.
1919
use core::{mem, hash};
20-
use core::ffi::c_void;
2120
use types::*;
2221
// use std::os::raw::{c_int, c_uchar, c_uint, c_void};
2322

src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -519,24 +519,31 @@ pub enum Error {
519519
InvalidTweak,
520520
}
521521

522-
// Passthrough Debug to Display, since errors should be user-visible
523-
impl fmt::Display for Error {
524-
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
525-
let res = match *self {
522+
impl Error {
523+
fn as_str(&self) -> &str {
524+
match *self {
526525
Error::IncorrectSignature => "secp: signature failed verification",
527526
Error::InvalidMessage => "secp: message was not 32 bytes (do you need to hash?)",
528527
Error::InvalidPublicKey => "secp: malformed public key",
529528
Error::InvalidSignature => "secp: malformed signature",
530529
Error::InvalidSecretKey => "secp: malformed or out-of-range secret key",
531530
Error::InvalidRecoveryId => "secp: bad recovery id",
532531
Error::InvalidTweak => "secp: bad tweak",
533-
};
534-
f.write_str(res)
532+
}
533+
}
534+
}
535+
536+
// Passthrough Debug to Display, since errors should be user-visible
537+
impl fmt::Display for Error {
538+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
539+
f.write_str(self.as_str())
535540
}
536541
}
537542

538543
#[cfg(feature = "std")]
539-
impl std::error::Error for Error {}
544+
impl std::error::Error for Error {
545+
fn description(&self) -> &str { self.as_str() }
546+
}
540547

541548
/// Marker trait for indicating that an instance of `Secp256k1` can be used for signing.
542549
pub trait Signing {}

src/types.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
use core::fmt;
2+
13
#![allow(non_camel_case_types)]
24
pub type c_int = i32;
35
pub type c_uchar = u8;
46
pub type c_uint = u32;
5-
pub use core::ffi::c_void;
7+
8+
/// This is an exact copy of https://doc.rust-lang.org/core/ffi/enum.c_void.html
9+
/// It should be Equivalent to C's void type when used as a pointer.
10+
///
11+
/// We can replace this with `core::ffi::c_void` once we update the rustc version to >=1.30.0.
12+
#[repr(u8)]
13+
pub enum c_void {
14+
#[doc(hidden)] __variant1,
15+
#[doc(hidden)] __variant2,
16+
}
17+
18+
impl fmt::Debug for c_void {
19+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20+
f.pad("c_void")
21+
}
22+
}

0 commit comments

Comments
 (0)