Skip to content

Commit

Permalink
make rustix the default feature
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed May 16, 2024
1 parent 00a6184 commit 4b6a4ef
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ all-features = true
# Features
#
[features]
default = ["bracketed-paste", "windows", "events", "libc"]
default = ["bracketed-paste", "windows", "events"]
windows = [
"dep:winapi",
"dep:crossterm_winapi",
Expand Down Expand Up @@ -71,12 +71,14 @@ crossterm_winapi = { version = "0.9.1", optional = true }
# UNIX dependencies
#
[target.'cfg(unix)'.dependencies]
# Default to using rustix for UNIX systems, but provide an option to use libc for backwards
# compatibility.
libc = { version = "0.2", default-features = false, optional = true }
rustix = { version = "0.38.34", default-features = false, features = [
"std",
"stdio",
"termios",
], optional = true }
] }
signal-hook = { version = "0.3.17", optional = true }
filedescriptor = { version = "0.8", optional = true }
mio = { version = "0.8", features = ["os-poll"], optional = true }
Expand Down
10 changes: 5 additions & 5 deletions src/terminal/sys/file_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;

#[cfg(feature = "libc")]
use libc::size_t;
#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
use rustix::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd};
#[cfg(feature = "libc")]
use std::{
Expand All @@ -24,7 +24,7 @@ pub struct FileDesc {
close_on_drop: bool,
}

#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
pub enum FileDesc {
Owned(OwnedFd),
Static(BorrowedFd<'static>),
Expand Down Expand Up @@ -64,7 +64,7 @@ impl FileDesc {
}
}

#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
impl FileDesc {
pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
let fd = match self {
Expand Down Expand Up @@ -103,7 +103,7 @@ impl AsRawFd for FileDesc {
}
}

#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
impl AsFd for FileDesc {
fn as_fd(&self) -> BorrowedFd<'_> {
match self {
Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn tty_fd() -> io::Result<FileDesc> {
Ok(FileDesc::new(fd, close_on_drop))
}

#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
/// Creates a file descriptor pointing to the standard input or `/dev/tty`.
pub fn tty_fd() -> io::Result<FileDesc> {
use std::fs::File;
Expand Down
14 changes: 7 additions & 7 deletions src/terminal/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use libc::{
TIOCGWINSZ,
};
use parking_lot::Mutex;
#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
use rustix::{
fd::AsFd,
termios::{Termios, Winsize},
Expand Down Expand Up @@ -42,7 +42,7 @@ impl From<winsize> for WindowSize {
}
}
}
#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
impl From<Winsize> for WindowSize {
fn from(size: Winsize) -> WindowSize {
WindowSize {
Expand Down Expand Up @@ -80,7 +80,7 @@ pub(crate) fn window_size() -> io::Result<WindowSize> {
Err(std::io::Error::last_os_error().into())
}

#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
pub(crate) fn window_size() -> io::Result<WindowSize> {
let file = File::open("/dev/tty").map(|file| (FileDesc::Owned(file.into())));
let fd = if let Ok(file) = &file {
Expand Down Expand Up @@ -120,7 +120,7 @@ pub(crate) fn enable_raw_mode() -> io::Result<()> {
Ok(())
}

#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
pub(crate) fn enable_raw_mode() -> io::Result<()> {
let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock();
if original_mode.is_some() {
Expand Down Expand Up @@ -154,7 +154,7 @@ pub(crate) fn disable_raw_mode() -> io::Result<()> {
Ok(())
}

#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
pub(crate) fn disable_raw_mode() -> io::Result<()> {
let mut original_mode = TERMINAL_MODE_PRIOR_RAW_MODE.lock();
if let Some(original_mode_ios) = original_mode.as_ref() {
Expand All @@ -166,13 +166,13 @@ pub(crate) fn disable_raw_mode() -> io::Result<()> {
Ok(())
}

#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
fn get_terminal_attr(fd: impl AsFd) -> io::Result<Termios> {
let result = rustix::termios::tcgetattr(fd)?;
Ok(result)
}

#[cfg(feature = "rustix")]
#[cfg(not(feature = "libc"))]
fn set_terminal_attr(fd: impl AsFd, termios: &Termios) -> io::Result<()> {
rustix::termios::tcsetattr(fd, rustix::termios::OptionalActions::Now, termios)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<S: AsRawFd> IsTty for S {
}
}

#[cfg(all(unix, feature = "rustix"))]
#[cfg(all(unix, not(feature = "libc")))]
impl<S: AsRawFd> IsTty for S {
fn is_tty(&self) -> bool {
let fd = self.as_raw_fd();
Expand Down

0 comments on commit 4b6a4ef

Please sign in to comment.