Repository: kkawakam/rustyline
Pull request #304 (2019-12-01 09:47:56)

Introduce cfg-if


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 58b6bf0..4357d1e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,6 +20,7 @@ maintenance = { status = "actively-developed" }
 members = ["rustyline-derive"]
 
 [dependencies]
+cfg-if = "0.1"
 dirs = { version = "2.0", optional = true }
 libc = "0.2"
 log = "0.4"
diff --git a/src/completion.rs b/src/completion.rs
index de5d0b0..eadae06 100644
--- a/src/completion.rs
+++ b/src/completion.rs
@@ -123,35 +123,31 @@ pub struct FilenameCompleter {
 
 const DOUBLE_QUOTES_ESCAPE_CHAR: Option<char> = Some('\\');
 
-// rl_basic_word_break_characters, rl_completer_word_break_characters
-#[cfg(unix)]
-const DEFAULT_BREAK_CHARS: [u8; 18] = [
-    b' ', b'\t', b'\n', b'"', b'\\', b'\'', b'`', b'@', b'$', b'>', b'<', b'=', b';', b'|', b'&',
-    b'{', b'(', b'\0',
-];
-#[cfg(unix)]
-const ESCAPE_CHAR: Option<char> = Some('\\');
-// Remove \ to make file completion works on windows
-#[cfg(windows)]
-const DEFAULT_BREAK_CHARS: [u8; 17] = [
-    b' ', b'\t', b'\n', b'"', b'\'', b'`', b'@', b'$', b'>', b'<', b'=', b';', b'|', b'&', b'{',
-    b'(', b'\0',
-];
-#[cfg(windows)]
-const ESCAPE_CHAR: Option<char> = None;
-#[cfg(target_arch = "wasm32")]
-const DEFAULT_BREAK_CHARS: [u8; 0] = [];
-#[cfg(target_arch = "wasm32")]
-const ESCAPE_CHAR: Option<char> = None;
-
-// In double quotes, not all break_chars need to be escaped
-// https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
-#[cfg(unix)]
-const DOUBLE_QUOTES_SPECIAL_CHARS: [u8; 4] = [b'"', b'$', b'\\', b'`'];
-#[cfg(windows)]
-const DOUBLE_QUOTES_SPECIAL_CHARS: [u8; 1] = [b'"']; // TODO Validate: only '"' ?
-#[cfg(target_arch = "wasm32")]
-const DOUBLE_QUOTES_SPECIAL_CHARS: [u8; 0] = [];
+cfg_if::cfg_if! {
+    if #[cfg(unix)] {
+        // rl_basic_word_break_characters, rl_completer_word_break_characters
+        const DEFAULT_BREAK_CHARS: [u8; 18] = [
+            b' ', b'\t', b'\n', b'"', b'\\', b'\'', b'`', b'@', b'$', b'>', b'<', b'=', b';', b'|', b'&',
+            b'{', b'(', b'\0',
+        ];
+        const ESCAPE_CHAR: Option<char> = Some('\\');
+        // In double quotes, not all break_chars need to be escaped
+        // https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
+        const DOUBLE_QUOTES_SPECIAL_CHARS: [u8; 4] = [b'"', b'$', b'\\', b'`'];
+    } else if #[cfg(windows)] {
+        // Remove \ to make file completion works on windows
+        const DEFAULT_BREAK_CHARS: [u8; 17] = [
+            b' ', b'\t', b'\n', b'"', b'\'', b'`', b'@', b'$', b'>', b'<', b'=', b';', b'|', b'&', b'{',
+            b'(', b'\0',
+        ];
+        const ESCAPE_CHAR: Option<char> = None;
+        const DOUBLE_QUOTES_SPECIAL_CHARS: [u8; 1] = [b'"']; // TODO Validate: only '"' ?
+    } else if #[cfg(target_arch = "wasm32")] {
+        const DEFAULT_BREAK_CHARS: [u8; 0] = [];
+        const ESCAPE_CHAR: Option<char> = None;
+        const DOUBLE_QUOTES_SPECIAL_CHARS: [u8; 0] = [];
+    }
+}
 
 #[derive(Clone, Copy, Debug, PartialEq)]
 pub enum Quote {
diff --git a/src/history.rs b/src/history.rs
index 9de4bea..b12a0bd 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -245,30 +245,32 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
     }
 }
 
-#[cfg(any(windows, target_arch = "wasm32"))]
-fn umask() -> u16 {
-    0
-}
-#[cfg(unix)]
-fn umask() -> libc::mode_t {
-    unsafe { libc::umask(libc::S_IXUSR | libc::S_IRWXG | libc::S_IRWXO) }
-}
-#[cfg(any(windows, target_arch = "wasm32"))]
-fn restore_umask(_: u16) {}
-#[cfg(unix)]
-fn restore_umask(old_umask: libc::mode_t) {
-    unsafe {
-        libc::umask(old_umask);
-    }
-}
+cfg_if::cfg_if! {
+    if #[cfg(any(windows, target_arch = "wasm32"))] {
+        fn umask() -> u16 {
+            0
+        }
 
-#[cfg(any(windows, target_arch = "wasm32"))]
-fn fix_perm(_: &File) {}
-#[cfg(unix)]
-fn fix_perm(file: &File) {
-    use std::os::unix::io::AsRawFd;
-    unsafe {
-        libc::fchmod(file.as_raw_fd(), libc::S_IRUSR | libc::S_IWUSR);
+        fn restore_umask(_: u16) {}
+
+        fn fix_perm(_: &File) {}
+    } else if #[cfg(unix)] {
+        fn umask() -> libc::mode_t {
+            unsafe { libc::umask(libc::S_IXUSR | libc::S_IRWXG | libc::S_IRWXO) }
+        }
+
+        fn restore_umask(old_umask: libc::mode_t) {
+            unsafe {
+                libc::umask(old_umask);
+            }
+        }
+
+        fn fix_perm(file: &File) {
+            use std::os::unix::io::AsRawFd;
+            unsafe {
+                libc::fchmod(file.as_raw_fd(), libc::S_IRUSR | libc::S_IWUSR);
+            }
+        }
     }
 }
 
diff --git a/src/tty/mod.rs b/src/tty/mod.rs
index df7abbb..3f6097e 100644
--- a/src/tty/mod.rs
+++ b/src/tty/mod.rs
@@ -156,21 +156,19 @@ pub trait Term {
     fn create_writer(&self) -> Self::Writer;
 }
 
-// If on Windows platform import Windows TTY module
-// and re-export into mod.rs scope
-#[cfg(windows)]
-mod windows;
-#[cfg(windows)]
-pub use self::windows::*;
-
-// If on Unix platform import Unix TTY module
-// and re-export into mod.rs scope
-#[cfg(unix)]
-mod unix;
-#[cfg(unix)]
-pub use self::unix::*;
-
-#[cfg(any(test, target_arch = "wasm32"))]
-mod test;
-#[cfg(any(test, target_arch = "wasm32"))]
-pub use self::test::*;
+cfg_if::cfg_if! {
+    if #[cfg(any(test, target_arch = "wasm32"))] {
+        mod test;
+        pub use self::test::*;
+    } else if #[cfg(windows)] {
+        // If on Windows platform import Windows TTY module
+        // and re-export into mod.rs scope
+        mod windows;
+        pub use self::windows::*;
+    } else if #[cfg(unix)] {
+        // If on Unix platform import Unix TTY module
+        // and re-export into mod.rs scope
+        mod unix;
+        pub use self::unix::*;
+    }
+}


Test patch:
diff --git a/src/test/history.rs b/src/test/history.rs
index c6df132..88cb3b5 100644
--- a/src/test/history.rs
+++ b/src/test/history.rs
@@ -162,11 +162,7 @@ fn ctrl_r_with_long_prompt() {
         assert_history(
             *mode,
             &["rustc", "cargo"],
-            &[
-                KeyPress::Ctrl('R'),
-                KeyPress::Char('o'),
-                KeyPress::Enter,
-            ],
+            &[KeyPress::Ctrl('R'), KeyPress::Char('o'), KeyPress::Enter],
             ">>>>>>>>>>>>>>>>>>>>>>>>>>> ",
             ("cargo", ""),
         );