Repository: kkawakam/rustyline
Pull request #285 (2019-09-06 00:05:41)

Add support for building on unknown targets like wasm32-unknown-unknown
With this PR, rustyline builds on `wasm32-unknown-unknown` target with no warnings:

```console
$ cargo build --target wasm32-unknown-unknown
   Compiling cfg-if v0.1.9
   Compiling log v0.4.8
   Compiling memchr v2.2.1
   Compiling libc v0.2.62
   Compiling unicode-width v0.1.6
   Compiling unicode-segmentation v1.3.0
   Compiling dirs-sys v0.3.4
   Compiling dirs v2.0.2
   Compiling rustyline v5.0.2 (/Users/lopopolo/dev/repos/rustyline)
    Finished dev [unoptimized + debuginfo] target(s) in 3.31s
```

This PR uses the `test` tty for unsupported platforms. The standard approach for getting a crate "working on Wasm" is to make it at least compile, even if it does not behave correctly (e.g. the implementation may panic). See https://github.com/BurntSushi/same-file/issues/42#issuecomment-447615062.

This enables building a workspace that includes rustyline for multiple targets, even if the crate using rustyline is not intended to be used on wasm platforms.


Patch:
diff --git a/src/completion.rs b/src/completion.rs
index 45e964e..de5d0b0 100644
--- a/src/completion.rs
+++ b/src/completion.rs
@@ -139,6 +139,10 @@ const DEFAULT_BREAK_CHARS: [u8; 17] = [
 ];
 #[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
@@ -146,6 +150,8 @@ const ESCAPE_CHAR: Option<char> = None;
 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] = [];
 
 #[derive(Clone, Copy, Debug, PartialEq)]
 pub enum Quote {
diff --git a/src/config.rs b/src/config.rs
index 01c744a..0f5783e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -162,7 +162,7 @@ pub enum BellStyle {
 /// `Audible` by default on unix (overriden by current Terminal settings).
 /// `None` on windows.
 impl Default for BellStyle {
-    #[cfg(windows)]
+    #[cfg(any(windows, target_arch = "wasm32"))]
     fn default() -> Self {
         BellStyle::None
     }
diff --git a/src/history.rs b/src/history.rs
index b7cc317..61431c9 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -245,7 +245,7 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
     }
 }
 
-#[cfg(windows)]
+#[cfg(any(windows, target_arch = "wasm32"))]
 fn umask() -> u16 {
     0
 }
@@ -253,7 +253,7 @@ fn umask() -> u16 {
 fn umask() -> libc::mode_t {
     unsafe { libc::umask(libc::S_IXUSR | libc::S_IRWXG | libc::S_IRWXO) }
 }
-#[cfg(windows)]
+#[cfg(any(windows, target_arch = "wasm32"))]
 fn restore_umask(_: u16) {}
 #[cfg(unix)]
 fn restore_umask(old_umask: libc::mode_t) {
@@ -262,7 +262,7 @@ fn restore_umask(old_umask: libc::mode_t) {
     }
 }
 
-#[cfg(windows)]
+#[cfg(any(windows, target_arch = "wasm32"))]
 fn fix_perm(_: &File) {}
 #[cfg(unix)]
 fn fix_perm(file: &File) {
diff --git a/src/keys.rs b/src/keys.rs
index c3072d8..72876d8 100644
--- a/src/keys.rs
+++ b/src/keys.rs
@@ -36,6 +36,7 @@ pub enum KeyPress {
     Up,
 }
 
+#[cfg(any(windows, unix))]
 pub fn char_to_key_press(c: char) -> KeyPress {
     if !c.is_control() {
         return KeyPress::Char(c);
diff --git a/src/tty/mod.rs b/src/tty/mod.rs
index 3acf138..61f2917 100644
--- a/src/tty/mod.rs
+++ b/src/tty/mod.rs
@@ -217,7 +217,7 @@ mod unix;
 #[cfg(unix)]
 pub use self::unix::*;
 
-#[cfg(test)]
+#[cfg(any(test, target_arch = "wasm32"))]
 mod test;
-#[cfg(test)]
+#[cfg(any(test, target_arch = "wasm32"))]
 pub use self::test::*;


Test patch:
diff --git a/src/tty/test.rs b/src/tty/test.rs
index ac0518a..c944955 100644
--- a/src/tty/test.rs
+++ b/src/tty/test.rs
@@ -185,7 +185,7 @@ impl Term for DummyTerminal {
     }
 
     fn create_writer(&self) -> Sink {
-        Sink {}
+        Sink::new()
     }
 }