Skip to content

Commit

Permalink
TryFrom stabilized in 1.34, nng_pipe_ev::try_from no longer needed, u…
Browse files Browse the repository at this point in the history
…pdate nng
  • Loading branch information
jeikabu committed Apr 16, 2019
1 parent 07b75e1 commit 95be805
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 51 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ source-update-bindings = ["build-bindgen"]
cty = {version = "0.2", optional = true}

[build-dependencies]
version_check = "0.1"
cmake = {version = "0.1", optional = true}
bindgen = {version = "0.47", optional = true}

Expand Down
8 changes: 8 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
fn main() {
cfg();
link_nng();
build_bindgen();
}

fn cfg() {
match version_check::is_min_version("1.34.0") {
Some((true, _version)) => println!("cargo:rustc-cfg=try_from"),
_ => {},
}
}

#[cfg(feature = "build-nng")]
fn link_nng() {
struct Generator(&'static str);
Expand Down
2 changes: 1 addition & 1 deletion nng
102 changes: 52 additions & 50 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ mod bindings {
#[cfg(not(feature = "build-bindgen"))]
mod bindings;

#[cfg(try_from)]
use core::convert::TryFrom;
#[cfg(try_from)]
use core::num::TryFromIntError;

pub use crate::bindings::*;

impl nng_pipe {
Expand Down Expand Up @@ -94,81 +99,78 @@ impl nng_ctx {
};
}

/// Create a TryFromIntError
#[cfg(try_from)]
pub fn new_try_from_int_error() -> TryFromIntError {
u8::try_from(0xffff).unwrap_err()
}

impl nng_stat_type_enum {
// TODO: 1.33/1.34 replace this with TryFrom once stabilized:
// https://doc.rust-lang.org/std/convert/trait.TryFrom.html
/// Converts value returned by [nng_stat_type](https://nanomsg.github.io/nng/man/v1.1.0/nng_stat_type.3) into `nng_stat_type_enum`.
pub fn try_from(value: i32) -> Result<Self, TryFromIntError> {
pub fn try_convert_from(value: i32) -> Option<Self> {
use crate::nng_stat_type_enum::*;
match value {
value if value == NNG_STAT_SCOPE as i32 => Ok(NNG_STAT_SCOPE),
value if value == NNG_STAT_LEVEL as i32 => Ok(NNG_STAT_LEVEL),
value if value == NNG_STAT_COUNTER as i32 => Ok(NNG_STAT_COUNTER),
value if value == NNG_STAT_STRING as i32 => Ok(NNG_STAT_STRING),
value if value == NNG_STAT_BOOLEAN as i32 => Ok(NNG_STAT_BOOLEAN),
value if value == NNG_STAT_ID as i32 => Ok(NNG_STAT_ID),
_ => Err(TryFromIntError),
value if value == NNG_STAT_SCOPE as i32 => Some(NNG_STAT_SCOPE),
value if value == NNG_STAT_LEVEL as i32 => Some(NNG_STAT_LEVEL),
value if value == NNG_STAT_COUNTER as i32 => Some(NNG_STAT_COUNTER),
value if value == NNG_STAT_STRING as i32 => Some(NNG_STAT_STRING),
value if value == NNG_STAT_BOOLEAN as i32 => Some(NNG_STAT_BOOLEAN),
value if value == NNG_STAT_ID as i32 => Some(NNG_STAT_ID),
_ => None,
}
}
}

#[cfg(try_from)]
impl TryFrom<i32> for nng_stat_type_enum {
type Error = TryFromIntError;
fn try_from(value: i32) -> Result<Self, Self::Error> {
nng_stat_type_enum::try_convert_from(value).ok_or(new_try_from_int_error())
}
}

impl nng_unit_enum {
// TODO: 1.33/1.34 replace this with TryFrom once stabilized:
// https://doc.rust-lang.org/std/convert/trait.TryFrom.html
/// Converts value returned by [nng_stat_unit](https://nanomsg.github.io/nng/man/v1.1.0/nng_stat_unit.3) into `nng_unit_enum`.
pub fn try_from(value: i32) -> Result<Self, TryFromIntError> {
pub fn try_convert_from(value: i32) -> Option<Self> {
use crate::nng_unit_enum::*;
match value {
value if value == NNG_UNIT_NONE as i32 => Ok(NNG_UNIT_NONE),
value if value == NNG_UNIT_BYTES as i32 => Ok(NNG_UNIT_BYTES),
value if value == NNG_UNIT_MESSAGES as i32 => Ok(NNG_UNIT_MESSAGES),
value if value == NNG_UNIT_MILLIS as i32 => Ok(NNG_UNIT_MILLIS),
value if value == NNG_UNIT_EVENTS as i32 => Ok(NNG_UNIT_EVENTS),
_ => Err(TryFromIntError),
value if value == NNG_UNIT_NONE as i32 => Some(NNG_UNIT_NONE),
value if value == NNG_UNIT_BYTES as i32 => Some(NNG_UNIT_BYTES),
value if value == NNG_UNIT_MESSAGES as i32 => Some(NNG_UNIT_MESSAGES),
value if value == NNG_UNIT_MILLIS as i32 => Some(NNG_UNIT_MILLIS),
value if value == NNG_UNIT_EVENTS as i32 => Some(NNG_UNIT_EVENTS),
_ => None,
}
}
}

impl nng_pipe_ev {
// TODO: 1.33/1.34 replace this with TryFrom once stabilized:
// https://doc.rust-lang.org/std/convert/trait.TryFrom.html
pub fn try_from(value: i32) -> Result<Self, TryFromIntError> {
use crate::nng_pipe_ev::*;
match value {
value if value == NNG_PIPE_EV_ADD_PRE as i32 => Ok(NNG_PIPE_EV_ADD_PRE),
value if value == NNG_PIPE_EV_ADD_POST as i32 => Ok(NNG_PIPE_EV_ADD_POST),
value if value == NNG_PIPE_EV_REM_POST as i32 => Ok(NNG_PIPE_EV_REM_POST),
_ => Err(TryFromIntError),
}
#[cfg(try_from)]
impl TryFrom<i32> for nng_unit_enum {
type Error = TryFromIntError;
fn try_from(value: i32) -> Result<Self, Self::Error> {
nng_unit_enum::try_convert_from(value).ok_or(new_try_from_int_error())
}
}

impl nng_sockaddr_family {
pub fn try_from(value: i32) -> Result<Self, TryFromIntError> {
pub fn try_convert_from(value: i32) -> Option<Self> {
use crate::nng_sockaddr_family::*;
match value {
value if value == NNG_AF_UNSPEC as i32 => Ok(NNG_AF_UNSPEC),
value if value == NNG_AF_INPROC as i32 => Ok(NNG_AF_INPROC),
value if value == NNG_AF_IPC as i32 => Ok(NNG_AF_IPC),
value if value == NNG_AF_INET as i32 => Ok(NNG_AF_INET),
value if value == NNG_AF_INET6 as i32 => Ok(NNG_AF_INET6),
value if value == NNG_AF_ZT as i32 => Ok(NNG_AF_ZT),
_ => Err(TryFromIntError),
value if value == NNG_AF_UNSPEC as i32 => Some(NNG_AF_UNSPEC),
value if value == NNG_AF_INPROC as i32 => Some(NNG_AF_INPROC),
value if value == NNG_AF_IPC as i32 => Some(NNG_AF_IPC),
value if value == NNG_AF_INET as i32 => Some(NNG_AF_INET),
value if value == NNG_AF_INET6 as i32 => Some(NNG_AF_INET6),
value if value == NNG_AF_ZT as i32 => Some(NNG_AF_ZT),
_ => None,
}
}
}

// TODO: 1.33/1.34 replace this with TryFrom once stabilized:
// https://doc.rust-lang.org/std/num/struct.TryFromIntError.html
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TryFromIntError;

#[cfg(not(feature = "no_std"))]
impl std::fmt::Display for TryFromIntError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "TryFromIntError")
#[cfg(try_from)]
impl TryFrom<i32> for nng_sockaddr_family {
type Error = TryFromIntError;
fn try_from(value: i32) -> Result<Self, Self::Error> {
nng_sockaddr_family::try_convert_from(value).ok_or(new_try_from_int_error())
}
}

#[cfg(not(feature = "no_std"))]
impl std::error::Error for TryFromIntError {}

0 comments on commit 95be805

Please sign in to comment.