Skip to content

Commit fabce3c

Browse files
committed
Remove the libc dependency
It's still used in signal-hook-registry, but if that crate ever switches to a libc-free strategy we'll be ready. Signed-off-by: John Nunley <dev@notgull.net>
1 parent cb9443e commit fabce3c

File tree

4 files changed

+56
-40
lines changed

4 files changed

+56
-40
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ async-io = "1.12.0"
3939
fastrand = "2.0.1"
4040
futures-lite = "1.12.0"
4141
signal-hook = "0.3.14"
42+
43+
[target.'cfg(unix)'.dev-dependencies]
44+
libc = "0.2.139"

src/lib.rs

+50-37
Original file line numberDiff line numberDiff line change
@@ -89,40 +89,53 @@ use std::task::{Context, Poll};
8989
#[cfg(unix)]
9090
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
9191

92-
#[cfg(windows)]
93-
mod libc {
92+
mod signum {
9493
pub(crate) use std::os::raw::c_int;
9594

96-
// Copy-pasted from the libc crate
97-
pub const SIGHUP: c_int = 1;
98-
pub const SIGINT: c_int = 2;
99-
pub const SIGQUIT: c_int = 3;
100-
pub const SIGILL: c_int = 4;
101-
pub const SIGTRAP: c_int = 5;
102-
pub const SIGABRT: c_int = 6;
103-
pub const SIGFPE: c_int = 8;
104-
pub const SIGKILL: c_int = 9;
105-
pub const SIGSEGV: c_int = 11;
106-
pub const SIGPIPE: c_int = 13;
107-
pub const SIGALRM: c_int = 14;
108-
pub const SIGTERM: c_int = 15;
109-
pub const SIGTTIN: c_int = 21;
110-
pub const SIGTTOU: c_int = 22;
111-
pub const SIGXCPU: c_int = 24;
112-
pub const SIGXFSZ: c_int = 25;
113-
pub const SIGVTALRM: c_int = 26;
114-
pub const SIGPROF: c_int = 27;
115-
pub const SIGWINCH: c_int = 28;
116-
pub const SIGCHLD: c_int = 17;
117-
pub const SIGBUS: c_int = 7;
118-
pub const SIGUSR1: c_int = 10;
119-
pub const SIGUSR2: c_int = 12;
120-
pub const SIGCONT: c_int = 18;
121-
pub const SIGSTOP: c_int = 19;
122-
pub const SIGTSTP: c_int = 20;
123-
pub const SIGURG: c_int = 23;
124-
pub const SIGIO: c_int = 29;
125-
pub const SIGSYS: c_int = 31;
95+
macro_rules! sig {
96+
($rustix_name:ident, $raw_value:literal) => {{
97+
#[cfg(unix)]
98+
{
99+
rustix::process::Signal::$rustix_name as c_int
100+
}
101+
102+
#[cfg(windows)]
103+
{
104+
$raw_value
105+
}
106+
}};
107+
}
108+
109+
// Define these ourselves.
110+
pub const SIGHUP: c_int = sig!(Hup, 1);
111+
pub const SIGINT: c_int = sig!(Int, 2);
112+
pub const SIGQUIT: c_int = sig!(Quit, 3);
113+
pub const SIGILL: c_int = sig!(Ill, 4);
114+
pub const SIGTRAP: c_int = sig!(Trap, 5);
115+
pub const SIGABRT: c_int = sig!(Abort, 6);
116+
pub const SIGFPE: c_int = sig!(Fpe, 8);
117+
pub const SIGKILL: c_int = sig!(Kill, 9);
118+
pub const SIGSEGV: c_int = sig!(Segv, 11);
119+
pub const SIGPIPE: c_int = sig!(Pipe, 13);
120+
pub const SIGALRM: c_int = sig!(Alarm, 14);
121+
pub const SIGTERM: c_int = sig!(Term, 15);
122+
pub const SIGTTIN: c_int = sig!(Ttin, 21);
123+
pub const SIGTTOU: c_int = sig!(Ttou, 22);
124+
pub const SIGXCPU: c_int = sig!(Xcpu, 24);
125+
pub const SIGXFSZ: c_int = sig!(Xfsz, 25);
126+
pub const SIGVTALRM: c_int = sig!(Vtalarm, 26);
127+
pub const SIGPROF: c_int = sig!(Prof, 27);
128+
pub const SIGWINCH: c_int = sig!(Winch, 28);
129+
pub const SIGCHLD: c_int = sig!(Child, 17);
130+
pub const SIGBUS: c_int = sig!(Bus, 7);
131+
pub const SIGUSR1: c_int = sig!(Usr1, 10);
132+
pub const SIGUSR2: c_int = sig!(Usr2, 12);
133+
pub const SIGCONT: c_int = sig!(Cont, 18);
134+
pub const SIGSTOP: c_int = sig!(Stop, 19);
135+
pub const SIGTSTP: c_int = sig!(Tstp, 20);
136+
pub const SIGURG: c_int = sig!(Urg, 23);
137+
pub const SIGIO: c_int = sig!(Io, 29);
138+
pub const SIGSYS: c_int = sig!(Sys, 31);
126139
}
127140

128141
macro_rules! define_signal_enum {
@@ -141,26 +154,26 @@ macro_rules! define_signal_enum {
141154
pub enum Signal {
142155
$(
143156
$(#[$inner])*
144-
$name = libc::$value,
157+
$name = signum::$value,
145158
)*
146159
}
147160

148161
impl Signal {
149162
/// Returns the signal number.
150-
fn number(self) -> libc::c_int {
163+
fn number(self) -> std::os::raw::c_int {
151164
match self {
152165
$(
153-
Signal::$name => libc::$value,
166+
Signal::$name => signum::$value,
154167
)*
155168
}
156169
}
157170

158171
/// Parse a signal from its number.
159172
#[cfg(unix)]
160-
fn from_number(number: libc::c_int) -> Option<Self> {
173+
fn from_number(number: std::os::raw::c_int) -> Option<Self> {
161174
match number {
162175
$(
163-
libc::$value => Some(Signal::$name),
176+
signum::$value => Some(Signal::$name),
164177
)*
165178
_ => None,
166179
}

src/pipe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::os::unix::net::UnixStream;
1313
use std::pin::Pin;
1414
use std::task::{Context, Poll};
1515

16-
const BUFFER_LEN: usize = mem::size_of::<libc::c_int>();
16+
const BUFFER_LEN: usize = mem::size_of::<std::os::raw::c_int>();
1717

1818
/// The notifier that uses an asynchronous pipe.
1919
#[derive(Debug)]
@@ -80,7 +80,7 @@ impl Notifier {
8080
}
8181

8282
// Convert the buffer into a signal number.
83-
let number = i32::from_ne_bytes(buffer);
83+
let number = std::os::raw::c_int::from_ne_bytes(buffer);
8484

8585
// Convert the signal number into a signal.
8686
let signal = match Signal::from_number(number) {

src/windows_registry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::mem;
3535
use std::os::raw::c_int;
3636
use std::sync::Mutex;
3737

38-
use super::libc::SIGINT;
38+
use super::signum::SIGINT;
3939

4040
/// The ID of a signal handler.
4141
pub(crate) type SigId = usize;

0 commit comments

Comments
 (0)