Skip to content

Commit 6a40444

Browse files
committed
Include stringified values with {SYNC,START}_PIPE_FD
This is to avoid additional `tryformat!()` calls at runtime.
1 parent 1787976 commit 6a40444

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/pipe.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ use serde::Deserialize;
1313
use tokio::fs::File;
1414
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
1515

16-
const START_PIPE_FD: RawFd = 3;
17-
const SYNC_PIPE_FD: RawFd = 4;
16+
// We embed the stringified file descriptors as consts to avoid dynamic string allocations.
17+
macro_rules! define_fds {
18+
($(const $name:ident = $fd:expr;)+) => {
19+
$(const $name: (RawFd, &str) = ($fd, stringify!($fd));)+
20+
};
21+
}
22+
23+
define_fds! {
24+
const START_PIPE_FD = 3;
25+
const SYNC_PIPE_FD = 4;
26+
}
1827

1928
/// An extension trait for `tokio::process::Command`.
2029
pub trait CommandExt {
@@ -32,15 +41,15 @@ impl CommandExt for tokio::process::Command {
3241
let sync_fd = sync.child_fd;
3342

3443
unsafe {
35-
self.env("_OCI_STARTPIPE", START_PIPE_FD.to_string())
36-
.env("_OCI_SYNCPIPE", SYNC_PIPE_FD.to_string())
44+
self.env("_OCI_STARTPIPE", START_PIPE_FD.1)
45+
.env("_OCI_SYNCPIPE", SYNC_PIPE_FD.1)
3746
.pre_exec(move || {
38-
if libc::dup2(start_fd, START_PIPE_FD) == -1 {
47+
if libc::dup2(start_fd, START_PIPE_FD.0) == -1 {
3948
eprintln!("failed to duplicate start pipe file descriptor");
4049
return Err(std::io::Error::last_os_error());
4150
}
4251

43-
if libc::dup2(sync_fd, SYNC_PIPE_FD) == -1 {
52+
if libc::dup2(sync_fd, SYNC_PIPE_FD.0) == -1 {
4453
eprintln!("failed to duplicate sync pipe file descriptor");
4554
return Err(std::io::Error::last_os_error());
4655
}

0 commit comments

Comments
 (0)