nanomsg.rs
nanomsg.rs copied to clipboard
Enhance error handling
The native nanomsg lib has quite a lot of constants for the errors, and they should be reported to the nanomsg.rs users. The nn_errno and nn_strerror should be used internally to produce usable results.
Sadly, the nanomsg error code are platform dependants and I don't know how to port this kind of C code in rust:
#ifndef EBADF
#define EBADF (NN_HAUSNUMERO + 13)
#endif
We can represent them with cfg and const types.
#[cfg(not(target_os = "windows"))]
use libc::{EPROTONOSUPPORT};
#[cfg(target_os = "windows")]
const EPROTONOSUPPORT: c_int = (NN_HAUSNUMERO + 2)
Some of the errors are already defined in the libc bindings that Rust ships with as they are standard POSIX errors.
The issue is finding out what platforms don't have them defined, I'm guessing windows is the major one that doesn't have them.
Also Rust hardcodes the values for each of these constants while Nanomsg has the NN_HAUSNUMERO
random number as to not collide with other OSes error types.
As usual, there is already some Rust goodness to support what we need. All of it in the trait FromPrimitive. I think I will update ErrorKind to take advantage of this.
While implementing the part that gets the nanomsg error message attached to the error code, I tried to use c_str_to_static_slice to avoid the copy but failed to import the function so far. @thehydroimpulse, any idea about how to do this ?
@blabaere mm, trying it on play.rust-lang.org seems to work for me. I'm not sure if that has just been added on a more-recent nightly, though.