mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-17 00:00:11 +02:00
signal: add SignalKind Hash/Eq impls and c_int conversion (#4540)
This commit is contained in:
@@ -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 {
|
||||
@@ -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.
|
||||
@@ -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,
|
||||
@@ -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 _);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user