Skip to content

Commit

Permalink
signal: add SignalKind Hash/Eq impls and c_int conversion (#4540)
Browse files Browse the repository at this point in the history
  • Loading branch information
andybarron committed Feb 26, 2022
1 parent 413c812 commit 3dd5a0d
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion tokio/src/signal/unix.rs
Expand Up @@ -60,7 +60,7 @@ impl Init for OsExtraData {
}

/// Represents the specific kind of signal to listen for.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct SignalKind(libc::c_int);

impl SignalKind {
Expand All @@ -84,6 +84,17 @@ impl SignalKind {
Self(signum as libc::c_int)
}

/// Get the signal's numeric value.
///
/// ```rust
/// # use tokio::signal::unix::SignalKind;
/// let kind = SignalKind::interrupt();
/// assert_eq!(kind.as_raw_value(), libc::SIGINT);
/// ```
pub fn as_raw_value(&self) -> std::os::raw::c_int {
self.0
}

/// Represents the SIGALRM signal.
///
/// On Unix systems this signal is sent when a real-time timer has expired.
Expand Down Expand Up @@ -190,6 +201,18 @@ impl SignalKind {
}
}

impl From<std::os::raw::c_int> for SignalKind {
fn from(signum: std::os::raw::c_int) -> Self {
Self::from_raw(signum as libc::c_int)
}
}

impl From<SignalKind> for std::os::raw::c_int {
fn from(kind: SignalKind) -> Self {
kind.as_raw_value()
}
}

pub(crate) struct SignalInfo {
event_info: EventInfo,
init: Once,
Expand Down Expand Up @@ -474,4 +497,15 @@ mod tests {
)
.unwrap_err();
}

#[test]
fn from_c_int() {
assert_eq!(SignalKind::from(2), SignalKind::interrupt());
}

#[test]
fn into_c_int() {
let value: std::os::raw::c_int = SignalKind::interrupt().into();
assert_eq!(value, libc::SIGINT as _);
}
}

0 comments on commit 3dd5a0d

Please sign in to comment.