diff --git a/CHANGELOG.md b/CHANGELOG.md index c72957566b..c176249bca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#1069](https://github.com/nix-rust/nix/pull/1069)) - Add `mkdirat`. ([#1084](https://github.com/nix-rust/nix/pull/1084)) +- `From` and `TryFrom` impls for many enums that correspond to libc constants. + ([#1088](https://github.com/nix-rust/nix/pull/1088)) ### Changed - Support for `ifaddrs` now present when building for Android. @@ -22,6 +24,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed ### Removed +- `Signal::from_c_int`. Use `Signal::try_from` instead. + ([#1088](https://github.com/nix-rust/nix/pull/1088)) +- `From` impl for `BaudRate`. Use `BaudRate::try_from` instead. + ([#1088](https://github.com/nix-rust/nix/pull/1088)) ## [0.14.1] - 2019-06-06 ### Added diff --git a/Cargo.toml b/Cargo.toml index 622e4331c0..a4d84d8cfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,9 @@ bitflags = "1.0" cfg-if = "0.1.0" void = "1.0.2" +[build-dependencies] +version_check = "0.9.1" + [target.'cfg(target_os = "dragonfly")'.build-dependencies] cc = "1" diff --git a/build.rs b/build.rs index 92fd3667fa..3bb5eb709b 100644 --- a/build.rs +++ b/build.rs @@ -1,12 +1,14 @@ #[cfg(target_os = "dragonfly")] extern crate cc; +extern crate version_check as rustc; -#[cfg(target_os = "dragonfly")] fn main() { + #[cfg(target_os = "dragonfly")] cc::Build::new() .file("src/errno_dragonfly.c") .compile("liberrno_dragonfly.a"); -} -#[cfg(not(target_os = "dragonfly"))] -fn main() {} + if rustc::is_min_version("1.34.0").unwrap_or(false) { + println!("cargo:rustc-cfg=try_from"); + } +} diff --git a/src/macros.rs b/src/macros.rs index 3d1b0e4b76..10baa0795b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -2,8 +2,6 @@ /// with values from the libc crate. It is used the same way as the `bitflags!` macro, except /// that only the name of the flag value has to be given. /// -/// The `libc` crate must be in scope with the name `libc`. -/// /// # Example /// ``` /// libc_bitflags!{ @@ -53,7 +51,7 @@ macro_rules! libc_bitflags { pub struct $BitFlags: $T { $( $(#[$inner $($args)*])* - const $Flag = libc::$Flag $(as $cast)*; + const $Flag = ::libc::$Flag $(as $cast)*; )+ } } @@ -61,196 +59,113 @@ macro_rules! libc_bitflags { } /// The `libc_enum!` macro helps with a common use case of defining an enum exclusively using -/// values from the `libc` crate. This macro supports both `pub` and private `enum`s. +/// values from the `libc` crate. The type after the enum name specifies the type of the constants +/// in `libc`. The macro will generate impls of `From` and `TryFrom` to convert between numeric and +/// enum values. +/// +/// `TryFrom` is only implemented for Rust >= 1.34.0, where the trait is stable. An equivalent +/// `try_from` inherent method is made available regardless of the Rust version. `TryInto` should +/// not be used as long as the MSRV for nix is less than 1.34.0. +/// /// -/// The `libc` crate must be in scope with the name `libc`. +/// Documentation for each variant must be provided before any cfg attributes. /// /// # Example /// ``` -/// libc_enum!{ -/// pub enum ProtFlags { +/// libc_enum! { +/// pub enum ProtFlags: c_int { /// PROT_NONE, /// PROT_READ, /// PROT_WRITE, /// PROT_EXEC, +/// /// Documentation before cfg attribute. /// #[cfg(any(target_os = "linux", target_os = "android"))] /// PROT_GROWSDOWN, /// #[cfg(any(target_os = "linux", target_os = "android"))] /// PROT_GROWSUP, /// } /// } +/// +/// let flag: c_int = ProtFlags::PROT_NONE.into(); +/// let flag: ProtFlags = ProtFlags::try_from(::libc::PROT_NONE).unwrap(); /// ``` macro_rules! libc_enum { - // (non-pub) Exit rule. - (@make_enum - { - name: $BitFlags:ident, - attrs: [$($attrs:tt)*], - entries: [$($entries:tt)*], - } - ) => { - $($attrs)* - #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] - enum $BitFlags { - $($entries)* - } - }; - - // (pub) Exit rule. - (@make_enum - { - pub, - name: $BitFlags:ident, - attrs: [$($attrs:tt)*], - entries: [$($entries:tt)*], - } - ) => { - $($attrs)* - #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] - pub enum $BitFlags { - $($entries)* - } - }; - - // (non-pub) Done accumulating. - (@accumulate_entries - { - name: $BitFlags:ident, - attrs: $attrs:tt, - }, - $entries:tt; + // pub + ( + $(#[$enum_attr:meta])* + pub $(($($scope:tt)*))* enum $($def:tt)* ) => { libc_enum! { - @make_enum - { - name: $BitFlags, - attrs: $attrs, - entries: $entries, - } + @(pub $(($($scope)*))*) + $(#[$enum_attr])* + enum $($def)* } }; - // (pub) Done accumulating. - (@accumulate_entries - { - pub, - name: $BitFlags:ident, - attrs: $attrs:tt, - }, - $entries:tt; + // non-pub + ( + $(#[$enum_attr:meta])* + enum $($def:tt)* ) => { libc_enum! { - @make_enum - { - pub, - name: $BitFlags, - attrs: $attrs, - entries: $entries, - } + @() + $(#[$enum_attr])* + enum $($def)* } }; - // Munch an attr. - (@accumulate_entries - $prefix:tt, - [$($entries:tt)*]; - #[$attr:meta] $($tail:tt)* - ) => { - libc_enum! { - @accumulate_entries - $prefix, - [ - $($entries)* - #[$attr] - ]; - $($tail)* + ( + @($($vis:tt)*) + $(#[$enum_attr:meta])* + enum $enum:ident : $prim:ty { + $( + $(#[doc = $var_doc:tt])* + $(#[cfg($var_cfg:meta)])* + $entry:ident + ),* $(,)* } - }; - - // Munch last ident if not followed by a comma. - (@accumulate_entries - $prefix:tt, - [$($entries:tt)*]; - $entry:ident ) => { - libc_enum! { - @accumulate_entries - $prefix, - [ - $($entries)* - $entry = libc::$entry, - ]; + $(#[$enum_attr])* + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + $($vis)* enum $enum { + $( + $(#[doc = $var_doc])* + $(#[cfg($var_cfg)])* + $entry = ::libc::$entry as isize + ),* } - }; - // Munch an ident; covers terminating comma case. - (@accumulate_entries - $prefix:tt, - [$($entries:tt)*]; - $entry:ident, $($tail:tt)* - ) => { - libc_enum! { - @accumulate_entries - $prefix, - [ - $($entries)* - $entry = libc::$entry, - ]; - $($tail)* + impl $enum { + pub fn try_from(value: $prim) -> std::result::Result<$enum, ::Error> { + match value { + $( + $(#[cfg($var_cfg)])* + ::libc::$entry => Ok($enum::$entry), + )* + // don't think this Error is the correct one + _ => Err(::Error::invalid_argument()) + } + } } - }; - // Munch an ident and cast it to the given type; covers terminating comma. - (@accumulate_entries - $prefix:tt, - [$($entries:tt)*]; - $entry:ident as $ty:ty, $($tail:tt)* - ) => { - libc_enum! { - @accumulate_entries - $prefix, - [ - $($entries)* - $entry = libc::$entry as $ty, - ]; - $($tail)* + impl std::convert::From<$enum> for $prim { + fn from(value: $enum) -> $prim { + match value { + $( + $(#[cfg($var_cfg)])* + $enum::$entry => ::libc::$entry, + )* + } + } } - }; - // (non-pub) Entry rule. - ( - $(#[$attr:meta])* - enum $BitFlags:ident { - $($vals:tt)* - } - ) => { - libc_enum! { - @accumulate_entries - { - name: $BitFlags, - attrs: [$(#[$attr])*], - }, - []; - $($vals)* - } - }; + #[cfg(try_from)] + impl std::convert::TryFrom<$prim> for $enum { + type Error = ::Error; - // (pub) Entry rule. - ( - $(#[$attr:meta])* - pub enum $BitFlags:ident { - $($vals:tt)* - } - ) => { - libc_enum! { - @accumulate_entries - { - pub, - name: $BitFlags, - attrs: [$(#[$attr])*], - }, - []; - $($vals)* + fn try_from(value: $prim) -> std::result::Result<$enum, Self::Error> { + $enum::try_from(value) + } } }; } diff --git a/src/sys/aio.rs b/src/sys/aio.rs index 40fa4e1517..8a8d86e9d2 100644 --- a/src/sys/aio.rs +++ b/src/sys/aio.rs @@ -39,8 +39,7 @@ use sys::time::TimeSpec; libc_enum! { /// Mode for `AioCb::fsync`. Controls whether only data or both data and /// metadata are synced. - #[repr(i32)] - pub enum AioFsyncMode { + pub enum AioFsyncMode: i32 { /// do it like `fsync` O_SYNC, /// on supported operating systems only, do it like `fdatasync` @@ -57,8 +56,7 @@ libc_enum! { /// When used with [`lio_listio`](fn.lio_listio.html), determines whether a /// given `aiocb` should be used for a read operation, a write operation, or /// ignored. Has no effect for any other aio functions. - #[repr(i32)] - pub enum LioOpcode { + pub enum LioOpcode: i32 { LIO_NOP, LIO_WRITE, LIO_READ, @@ -67,8 +65,7 @@ libc_enum! { libc_enum! { /// Mode for [`lio_listio`](fn.lio_listio.html) - #[repr(i32)] - pub enum LioMode { + pub enum LioMode: i32 { /// Requests that [`lio_listio`](fn.lio_listio.html) block until all /// requested operations have been completed LIO_WAIT, diff --git a/src/sys/event.rs b/src/sys/event.rs index 8cd7372f88..06c53c09e1 100644 --- a/src/sys/event.rs +++ b/src/sys/event.rs @@ -35,9 +35,7 @@ type type_of_event_filter = u32; #[cfg(not(target_os = "netbsd"))] type type_of_event_filter = i16; libc_enum! { - #[cfg_attr(target_os = "netbsd", repr(u32))] - #[cfg_attr(not(target_os = "netbsd"), repr(i16))] - pub enum EventFilter { + pub enum EventFilter: type_of_event_filter { EVFILT_AIO, /// Returns whenever there is no remaining data in the write buffer #[cfg(target_os = "freebsd")] diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 4e250501dd..e8c185915c 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -102,12 +102,11 @@ libc_bitflags!{ } } -libc_enum!{ +libc_enum! { /// Usage information for a range of memory to allow for performance optimizations by the kernel. /// /// Used by [`madvise`](./fn.madvise.html). - #[repr(i32)] - pub enum MmapAdvise { + pub enum MmapAdvise: i32 { /// No further special treatment. This is the default. MADV_NORMAL, /// Expect random page references. diff --git a/src/sys/ptrace/bsd.rs b/src/sys/ptrace/bsd.rs index 7797d10647..77b73c9369 100644 --- a/src/sys/ptrace/bsd.rs +++ b/src/sys/ptrace/bsd.rs @@ -21,9 +21,8 @@ cfg_if! { } libc_enum! { - #[repr(i32)] /// Ptrace Request enum defining the action to be taken. - pub enum Request { + pub enum Request: i32 { PT_TRACE_ME, PT_READ_I, PT_READ_D, diff --git a/src/sys/ptrace/linux.rs b/src/sys/ptrace/linux.rs index df15e66527..ce1b2d791c 100644 --- a/src/sys/ptrace/linux.rs +++ b/src/sys/ptrace/linux.rs @@ -26,11 +26,14 @@ cfg_if! { } } -libc_enum!{ - #[cfg_attr(not(any(target_env = "musl", target_os = "android")), repr(u32))] - #[cfg_attr(any(target_env = "musl", target_os = "android"), repr(i32))] +#[cfg(not(any(target_env = "musl", target_os = "android")))] +type type_of_request = u32; +#[cfg(any(target_env = "musl", target_os = "android"))] +type type_of_request = i32; + +libc_enum! { /// Ptrace Request enum defining the action to be taken. - pub enum Request { + pub enum Request: type_of_request { PTRACE_TRACEME, PTRACE_PEEKTEXT, PTRACE_PEEKDATA, @@ -109,12 +112,11 @@ libc_enum!{ } } -libc_enum!{ - #[repr(i32)] +libc_enum! { /// Using the ptrace options the tracer can configure the tracee to stop /// at certain events. This enum is used to define those events as defined /// in `man ptrace`. - pub enum Event { + pub enum Event: i32 { /// Event that stops before a return from fork or clone. PTRACE_EVENT_FORK, /// Event that stops before a return from vfork or clone. diff --git a/src/sys/quota.rs b/src/sys/quota.rs index 8946fca221..da3a58149f 100644 --- a/src/sys/quota.rs +++ b/src/sys/quota.rs @@ -27,9 +27,8 @@ impl QuotaCmd { } // linux quota version >= 2 -libc_enum!{ - #[repr(i32)] - enum QuotaSubCmd { +libc_enum! { + enum QuotaSubCmd: i32 { Q_SYNC, Q_QUOTAON, Q_QUOTAOFF, @@ -38,10 +37,9 @@ libc_enum!{ } } -libc_enum!{ +libc_enum! { /// The scope of the quota. - #[repr(i32)] - pub enum QuotaType { + pub enum QuotaType: i32 { /// Specify a user quota USRQUOTA, /// Specify a group quota @@ -49,10 +47,9 @@ libc_enum!{ } } -libc_enum!{ +libc_enum! { /// The type of quota format to use. - #[repr(i32)] - pub enum QuotaFmt { + pub enum QuotaFmt: i32 { /// Use the original quota format. QFMT_VFS_OLD, /// Use the standard VFS v0 quota format. diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index bafa8fc119..1bcd0c17c7 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -11,8 +11,7 @@ libc_enum! { /// /// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for /// enabling/disabling Ctrl-Alt-Delete. - #[repr(i32)] - pub enum RebootMode { + pub enum RebootMode: i32 { RB_HALT_SYSTEM, RB_KEXEC, RB_POWER_OFF, diff --git a/src/sys/signal.rs b/src/sys/signal.rs index 1013a77fd4..b3be5b8d27 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -16,13 +16,8 @@ use std::ptr; #[cfg(not(target_os = "openbsd"))] pub use self::sigevent::*; -libc_enum!{ - // Currently there is only one definition of c_int in libc, as well as only one - // type for signal constants. - // We would prefer to use the libc::c_int alias in the repr attribute. Unfortunately - // this is not (yet) possible. - #[repr(i32)] - pub enum Signal { +libc_enum! { + pub enum Signal: libc::c_int { SIGHUP, SIGINT, SIGQUIT, @@ -288,18 +283,6 @@ impl Signal { pub fn iterator() -> SignalIterator { SignalIterator{next: 0} } - - // We do not implement the From trait, because it is supposed to be infallible. - // With Rust RFC 1542 comes the appropriate trait TryFrom. Once it is - // implemented, we'll replace this function. - #[inline] - pub fn from_c_int(signum: libc::c_int) -> Result { - if 0 < signum && signum < NSIG { - Ok(unsafe { mem::transmute(signum) }) - } else { - Err(Error::invalid_argument()) - } - } } pub const SIGIOT : Signal = SIGABRT; @@ -319,8 +302,7 @@ libc_bitflags!{ } libc_enum! { - #[repr(i32)] - pub enum SigmaskHow { + pub enum SigmaskHow: i32 { SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK, @@ -413,7 +395,7 @@ impl SigSet { let mut signum: libc::c_int = unsafe { mem::uninitialized() }; let res = unsafe { libc::sigwait(&self.sigset as *const libc::sigset_t, &mut signum) }; - Errno::result(res).map(|_| Signal::from_c_int(signum).unwrap()) + Errno::result(res).map(|_| Signal::try_from(signum).unwrap()) } } @@ -538,12 +520,14 @@ pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result for libc::termios { } } -libc_enum!{ +libc_enum! { /// Baud rates supported by the system. /// /// For the BSDs, arbitrary baud rates can be specified by using `u32`s directly instead of this /// enum. /// /// B0 is special and will disable the port. - #[cfg_attr(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64"), repr(u64))] - #[cfg_attr(not(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64")), repr(u32))] - pub enum BaudRate { + pub enum BaudRate: libc::speed_t { B0, B50, B75, @@ -376,126 +374,12 @@ libc_enum!{ } } -impl From for BaudRate { - fn from(s: libc::speed_t) -> BaudRate { - - use libc::{B0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400, B4800, - B9600, B19200, B38400, B57600, B115200, B230400}; - #[cfg(any(target_os = "android", target_os = "linux"))] - use libc::{B500000, B576000, B1000000, B1152000, B1500000, B2000000}; - #[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))] - use libc::{B2500000, B3000000, B3500000, B4000000}; - #[cfg(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd"))] - use libc::{B7200, B14400, B28800, B76800}; - #[cfg(any(target_os = "android", - target_os = "freebsd", - target_os = "linux", - target_os = "netbsd"))] - use libc::{B460800, B921600}; - - match s { - B0 => BaudRate::B0, - B50 => BaudRate::B50, - B75 => BaudRate::B75, - B110 => BaudRate::B110, - B134 => BaudRate::B134, - B150 => BaudRate::B150, - B200 => BaudRate::B200, - B300 => BaudRate::B300, - B600 => BaudRate::B600, - B1200 => BaudRate::B1200, - B1800 => BaudRate::B1800, - B2400 => BaudRate::B2400, - B4800 => BaudRate::B4800, - #[cfg(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd"))] - B7200 => BaudRate::B7200, - B9600 => BaudRate::B9600, - #[cfg(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd"))] - B14400 => BaudRate::B14400, - B19200 => BaudRate::B19200, - #[cfg(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd"))] - B28800 => BaudRate::B28800, - B38400 => BaudRate::B38400, - B57600 => BaudRate::B57600, - #[cfg(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd"))] - B76800 => BaudRate::B76800, - B115200 => BaudRate::B115200, - B230400 => BaudRate::B230400, - #[cfg(any(target_os = "android", - target_os = "freebsd", - target_os = "linux", - target_os = "netbsd"))] - B460800 => BaudRate::B460800, - #[cfg(any(target_os = "android", target_os = "linux"))] - B500000 => BaudRate::B500000, - #[cfg(any(target_os = "android", target_os = "linux"))] - B576000 => BaudRate::B576000, - #[cfg(any(target_os = "android", - target_os = "freebsd", - target_os = "linux", - target_os = "netbsd"))] - B921600 => BaudRate::B921600, - #[cfg(any(target_os = "android", target_os = "linux"))] - B1000000 => BaudRate::B1000000, - #[cfg(any(target_os = "android", target_os = "linux"))] - B1152000 => BaudRate::B1152000, - #[cfg(any(target_os = "android", target_os = "linux"))] - B1500000 => BaudRate::B1500000, - #[cfg(any(target_os = "android", target_os = "linux"))] - B2000000 => BaudRate::B2000000, - #[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))] - B2500000 => BaudRate::B2500000, - #[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))] - B3000000 => BaudRate::B3000000, - #[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))] - B3500000 => BaudRate::B3500000, - #[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))] - B4000000 => BaudRate::B4000000, - b => unreachable!("Invalid baud constant: {}", b), - } - } -} - -// TODO: Include `TryFrom for BaudRate` once that API stabilizes -#[cfg(any(target_os = "freebsd", - target_os = "dragonfly", - target_os = "ios", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd"))] -impl From for u32 { - fn from(b: BaudRate) -> u32 { - b as u32 - } -} - // TODO: Add TCSASOFT, which will require treating this as a bitfield. libc_enum! { /// Specify when a port configuration change should occur. /// /// Used as an argument to `tcsetattr()` - #[repr(i32)] - pub enum SetArg { + pub enum SetArg: i32 { /// The change will occur immediately TCSANOW, /// The change occurs after all output has been written @@ -509,8 +393,7 @@ libc_enum! { /// Specify a combination of the input and output buffers to flush /// /// Used as an argument to `tcflush()`. - #[repr(i32)] - pub enum FlushArg { + pub enum FlushArg: i32 { /// Flush data that was received but not read TCIFLUSH, /// Flush data written but not transmitted @@ -524,8 +407,7 @@ libc_enum! { /// Specify how transmission flow should be altered /// /// Used as an argument to `tcflow()`. - #[repr(i32)] - pub enum FlowArg { + pub enum FlowArg: i32 { /// Suspend transmission TCOOFF, /// Resume transmission @@ -540,8 +422,7 @@ libc_enum! { // TODO: Make this usable directly as a slice index. libc_enum! { /// Indices into the `termios.c_cc` array for special characters. - #[repr(usize)] - pub enum SpecialCharacterIndices { + pub enum SpecialCharacterIndices: usize { VDISCARD, #[cfg(any(target_os = "dragonfly", target_os = "freebsd", @@ -929,7 +810,7 @@ cfg_if!{ /// `cfsetispeed()` sets the intput baud rate in the given `Termios` structure. pub fn cfsetispeed>(termios: &mut Termios, baud: T) -> Result<()> { let inner_termios = unsafe { termios.get_libc_termios_mut() }; - let res = unsafe { libc::cfsetispeed(inner_termios, baud.into() as libc::speed_t) }; + let res = unsafe { libc::cfsetispeed(inner_termios, baud.into()) }; termios.update_wrapper(); Errno::result(res).map(drop) } @@ -940,7 +821,7 @@ cfg_if!{ /// `cfsetospeed()` sets the output baud rate in the given termios structure. pub fn cfsetospeed>(termios: &mut Termios, baud: T) -> Result<()> { let inner_termios = unsafe { termios.get_libc_termios_mut() }; - let res = unsafe { libc::cfsetospeed(inner_termios, baud.into() as libc::speed_t) }; + let res = unsafe { libc::cfsetospeed(inner_termios, baud.into()) }; termios.update_wrapper(); Errno::result(res).map(drop) } @@ -952,7 +833,7 @@ cfg_if!{ /// this is part of the 4.4BSD standard and not part of POSIX. pub fn cfsetspeed>(termios: &mut Termios, baud: T) -> Result<()> { let inner_termios = unsafe { termios.get_libc_termios_mut() }; - let res = unsafe { libc::cfsetspeed(inner_termios, baud.into() as libc::speed_t) }; + let res = unsafe { libc::cfsetspeed(inner_termios, baud.into()) }; termios.update_wrapper(); Errno::result(res).map(drop) } @@ -963,7 +844,7 @@ cfg_if!{ /// `cfgetispeed()` extracts the input baud rate from the given `Termios` structure. pub fn cfgetispeed(termios: &Termios) -> BaudRate { let inner_termios = termios.get_libc_termios(); - unsafe { libc::cfgetispeed(&*inner_termios) }.into() + BaudRate::try_from(unsafe { libc::cfgetispeed(&*inner_termios) }).unwrap() } /// Get output baud rate (see @@ -972,7 +853,7 @@ cfg_if!{ /// `cfgetospeed()` extracts the output baud rate from the given `Termios` structure. pub fn cfgetospeed(termios: &Termios) -> BaudRate { let inner_termios = termios.get_libc_termios(); - unsafe { libc::cfgetospeed(&*inner_termios) }.into() + BaudRate::try_from(unsafe { libc::cfgetospeed(&*inner_termios) }).unwrap() } /// Set input baud rate (see @@ -981,7 +862,7 @@ cfg_if!{ /// `cfsetispeed()` sets the intput baud rate in the given `Termios` structure. pub fn cfsetispeed(termios: &mut Termios, baud: BaudRate) -> Result<()> { let inner_termios = unsafe { termios.get_libc_termios_mut() }; - let res = unsafe { libc::cfsetispeed(inner_termios, baud as libc::speed_t) }; + let res = unsafe { libc::cfsetispeed(inner_termios, baud.into()) }; termios.update_wrapper(); Errno::result(res).map(drop) } @@ -992,7 +873,7 @@ cfg_if!{ /// `cfsetospeed()` sets the output baud rate in the given `Termios` structure. pub fn cfsetospeed(termios: &mut Termios, baud: BaudRate) -> Result<()> { let inner_termios = unsafe { termios.get_libc_termios_mut() }; - let res = unsafe { libc::cfsetospeed(inner_termios, baud as libc::speed_t) }; + let res = unsafe { libc::cfsetospeed(inner_termios, baud.into()) }; termios.update_wrapper(); Errno::result(res).map(drop) } @@ -1004,7 +885,7 @@ cfg_if!{ /// this is part of the 4.4BSD standard and not part of POSIX. pub fn cfsetspeed(termios: &mut Termios, baud: BaudRate) -> Result<()> { let inner_termios = unsafe { termios.get_libc_termios_mut() }; - let res = unsafe { libc::cfsetspeed(inner_termios, baud as libc::speed_t) }; + let res = unsafe { libc::cfsetspeed(inner_termios, baud.into()) }; termios.update_wrapper(); Errno::result(res).map(drop) } diff --git a/src/sys/wait.rs b/src/sys/wait.rs index c54f7ec579..18df303799 100644 --- a/src/sys/wait.rs +++ b/src/sys/wait.rs @@ -126,7 +126,7 @@ fn signaled(status: i32) -> bool { } fn term_signal(status: i32) -> Result { - Signal::from_c_int(unsafe { libc::WTERMSIG(status) }) + Signal::try_from(unsafe { libc::WTERMSIG(status) }) } fn dumped_core(status: i32) -> bool { @@ -138,7 +138,7 @@ fn stopped(status: i32) -> bool { } fn stop_signal(status: i32) -> Result { - Signal::from_c_int(unsafe { libc::WSTOPSIG(status) }) + Signal::try_from(unsafe { libc::WSTOPSIG(status) }) } #[cfg(any(target_os = "android", target_os = "linux"))] diff --git a/test/sys/test_signal.rs b/test/sys/test_signal.rs index 8780763f77..0865185d7d 100644 --- a/test/sys/test_signal.rs +++ b/test/sys/test_signal.rs @@ -75,7 +75,7 @@ lazy_static! { } extern fn test_sigaction_handler(signal: libc::c_int) { - let signal = Signal::from_c_int(signal).unwrap(); + let signal: Signal = Signal::try_from(signal).unwrap(); SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed); } diff --git a/test/sys/test_signalfd.rs b/test/sys/test_signalfd.rs index a3b6098841..91186a2682 100644 --- a/test/sys/test_signalfd.rs +++ b/test/sys/test_signalfd.rs @@ -20,6 +20,6 @@ fn test_signalfd() { // And now catch that same signal. let res = fd.read_signal().unwrap().unwrap(); - let signo = Signal::from_c_int(res.ssi_signo as i32).unwrap(); + let signo: Signal = Signal::try_from(res.ssi_signo as i32).unwrap(); assert_eq!(signo, signal::SIGUSR1); }