Skip to content

Commit a4a53d3

Browse files
committed
Add 0/1/2 fds in initializer of FD_REGISTRY
1 parent 9115b84 commit a4a53d3

1 file changed

Lines changed: 7 additions & 22 deletions

File tree

‎libcc2rs/src/fd.rs‎

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd};
66

77
pub struct FdRegistry {
88
fds: Vec<Option<OwnedFd>>,
9-
seeded: bool,
109
}
1110

1211
thread_local! {
13-
static FD_REGISTRY: RefCell<FdRegistry> = const {
14-
RefCell::new(FdRegistry {
15-
fds: Vec::new(),
16-
seeded: false,
17-
})
18-
};
12+
static FD_REGISTRY: RefCell<FdRegistry> = RefCell::new(FdRegistry {
13+
fds: vec![
14+
std::io::stdin().as_fd().try_clone_to_owned().ok(),
15+
std::io::stdout().as_fd().try_clone_to_owned().ok(),
16+
std::io::stderr().as_fd().try_clone_to_owned().ok(),
17+
],
18+
});
1919
}
2020

2121
impl FdRegistry {
@@ -33,19 +33,6 @@ impl FdRegistry {
3333
raw
3434
}
3535

36-
fn seed_std(&mut self) {
37-
if self.seeded {
38-
return;
39-
}
40-
self.seeded = true;
41-
if self.fds.len() < 3 {
42-
self.fds.resize_with(3, || None);
43-
}
44-
self.fds[0] = std::io::stdin().as_fd().try_clone_to_owned().ok();
45-
self.fds[1] = std::io::stdout().as_fd().try_clone_to_owned().ok();
46-
self.fds[2] = std::io::stderr().as_fd().try_clone_to_owned().ok();
47-
}
48-
4936
fn lookup(&self, fd: i32) -> BorrowedFd<'_> {
5037
self.fds
5138
.get(fd as usize)
@@ -56,15 +43,13 @@ impl FdRegistry {
5643

5744
pub fn with_fd<R>(fd: i32, f: impl FnOnce(BorrowedFd<'_>) -> R) -> R {
5845
FD_REGISTRY.with(|r| {
59-
r.borrow_mut().seed_std();
6046
let reg = r.borrow();
6147
f(reg.lookup(fd))
6248
})
6349
}
6450

6551
pub fn with_fds<R>(fds: &[i32], f: impl FnOnce(&[BorrowedFd<'_>]) -> R) -> R {
6652
FD_REGISTRY.with(|r| {
67-
r.borrow_mut().seed_std();
6853
let reg = r.borrow();
6954
let borrowed: Vec<BorrowedFd<'_>> = fds.iter().map(|&fd| reg.lookup(fd)).collect();
7055
f(&borrowed)

0 commit comments

Comments
 (0)