Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add SignalKind Hash/Eq impls and c_int conversion #4540

Merged
merged 4 commits into from
Feb 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion tokio/src/signal/unix.rs
Original file line number Diff line number Diff line change
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 _);
}
}