Skip to content

Commit 4846423

Browse files
committed
Provide inherent method to get error message string
The new method provides an alternative to the `description` method on `std::error::Error`, which is soft-deprecated, and current beta clippy warns about its use.
1 parent bba1d98 commit 4846423

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

src/lib.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,9 @@ impl Error {
299299
},
300300
}
301301
}
302-
}
303302

304-
impl std::error::Error for Error {
305-
fn description(&self) -> &str {
303+
/// Returns the error message provided by 0MQ.
304+
pub fn message(self) -> &'static str {
306305
unsafe {
307306
let s = zmq_sys::zmq_strerror(self.to_raw());
308307
let v: &'static [u8] = mem::transmute(ffi::CStr::from_ptr(s).to_bytes());
@@ -311,27 +310,22 @@ impl std::error::Error for Error {
311310
}
312311
}
313312

313+
impl std::error::Error for Error {
314+
fn description(&self) -> &str {
315+
self.message()
316+
}
317+
}
318+
314319
impl std::fmt::Display for Error {
315320
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
316-
unsafe {
317-
let s = zmq_sys::zmq_strerror(self.to_raw());
318-
let v: &'static [u8] = mem::transmute(ffi::CStr::from_ptr(s).to_bytes());
319-
write!(f, "{}", str::from_utf8(v).unwrap())
320-
}
321+
write!(f, "{}", self.message())
321322
}
322323
}
323324

324325
impl fmt::Debug for Error {
325-
/// Return the error string for an error.
326326
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
327-
unsafe {
328-
let s = zmq_sys::zmq_strerror(self.to_raw());
329-
write!(
330-
f,
331-
"{}",
332-
str::from_utf8(ffi::CStr::from_ptr(s).to_bytes()).unwrap()
333-
)
334-
}
327+
// FIXME: An unquoted string is not a good `Debug` output.
328+
write!(f, "{}", self.message())
335329
}
336330
}
337331

tests/test.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ test!(test_version, {
159159
});
160160

161161
test!(test_zmq_error, {
162-
use std::error::Error as StdError;
163-
164162
let ctx = Context::new();
165163
let sock = ctx.socket(SocketType::REP).unwrap();
166164

@@ -171,7 +169,7 @@ test!(test_zmq_error, {
171169
// ZMQ error strings might not be guaranteed, so we'll not check
172170
// against specific messages, but still check that formatting does
173171
// not segfault, for example, and gives the same strings.
174-
let desc = err.description();
172+
let desc = err.message();
175173
let display = format!("{}", err);
176174
let debug = format!("{:?}", err);
177175
assert_eq!(desc, display);

0 commit comments

Comments
 (0)