From 826f0cb9e381051814a7bba3998d5a11546d5363 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 05:02:17 -0500 Subject: [PATCH 01/69] Add input function Condenses the normal input process into one function. This is good for new rust users who don't understand the normal process and for users who don't require special functionality. --- src/libstd/io/stdio.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index d6b7ad6254a8c..bf95ea41de41d 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -5,7 +5,7 @@ use crate::io::prelude::*; use crate::cell::RefCell; use crate::fmt; use crate::io::lazy::Lazy; -use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter}; +use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter, Write}; use crate::sync::{Arc, Mutex, MutexGuard, Once}; use crate::sys::stdio; use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard}; @@ -872,6 +872,43 @@ impl fmt::Debug for StderrLock<'_> { } } +/// Locks the current handle and reads a line of input, returning a `String` containing +/// the input. +/// +/// If you need more explicit control over +/// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. +/// +/// [`Stdin::lock`]: struct.Stdin.html#method.lock +/// [`Stdout::lock`]: struct.Stdout.html#method.lock +/// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to read and write bytes that are not valid UTF-8 will +/// return an error. +/// +/// # Examples +/// +/// ```no_run +/// use std::io::input; +/// +/// fn main() { +/// let user_input = input("Please enter some text: "); +/// +/// println!("You typed: {}", get); +/// +/// } +/// ``` +pub fn input(message:&str) -> String { + print!("{}", message); + let mut input = String::new(); + let _ = stdout().flush(); + stdin().read_line(&mut input).expect("RUST ERROR HELP"); + if ['\n','\r'].contains(&input.chars().next_back().unwrap()) { + input.pop(); + } + return input; +} + /// Resets the thread-local stderr handle to the specified writer /// /// This will replace the current thread's stderr handle, returning the old From 7f49b83e139b142f578890d5e42f5245e9965168 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 05:34:51 -0500 Subject: [PATCH 02/69] Update stdio.rs --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index bf95ea41de41d..aa9c25f896a0c 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -902,7 +902,7 @@ pub fn input(message:&str) -> String { print!("{}", message); let mut input = String::new(); let _ = stdout().flush(); - stdin().read_line(&mut input).expect("RUST ERROR HELP"); + stdin().read_line(&mut input).expect("Failed to read from stdin"); if ['\n','\r'].contains(&input.chars().next_back().unwrap()) { input.pop(); } From 3221594a2b3aeb851b580d3d741dd7f3ca2c8a76 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 15:38:03 -0500 Subject: [PATCH 03/69] Update stdio.rs - Properly formated. - Added ```rust for docs. - Split function into two : ```input()``` and ```inputln(&str)```. - Deleted unnecessary comment for non-windows users. - Removed ```print!()``` macro. - Added warning not recommending this method for normal use. --- src/libstd/io/stdio.rs | 67 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index aa9c25f896a0c..cb59fcb1a21c3 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -874,41 +874,82 @@ impl fmt::Debug for StderrLock<'_> { /// Locks the current handle and reads a line of input, returning a `String` containing /// the input. -/// +/// /// If you need more explicit control over /// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. /// /// [`Stdin::lock`]: struct.Stdin.html#method.lock /// [`Stdout::lock`]: struct.Stdout.html#method.lock +/// +/// ### Note: Safety and Usage +/// This function is not the recommended solution to get user input, it should +/// only be used for simple actions. If you require more explicit control over +/// getting user input, see the [`Stdin::read_line`] method. /// -/// ### Note: Windows Portability Consideration -/// When operating in a console, the Windows implementation of this stream does not support -/// non-UTF-8 byte sequences. Attempting to read and write bytes that are not valid UTF-8 will -/// return an error. +/// [`Stdin::read_line`]: struct.Stdin.html#method.read_line /// /// # Examples /// -/// ```no_run -/// use std::io::input; +/// ```rust +/// use std::io::inputln; /// /// fn main() { -/// let user_input = input("Please enter some text: "); +/// let user_input = inputln("Please enter some text: "); /// -/// println!("You typed: {}", get); +/// println!("You typed: {}", user_input); /// /// } /// ``` -pub fn input(message:&str) -> String { - print!("{}", message); +pub fn inputln(prompt: &str) -> String { + stdout() + .write(prompt.as_bytes()) + .unwrap(); let mut input = String::new(); let _ = stdout().flush(); - stdin().read_line(&mut input).expect("Failed to read from stdin"); - if ['\n','\r'].contains(&input.chars().next_back().unwrap()) { + stdin() + .read_line(&mut input) + .expect("Failed to read from stdin"); + if ['\n', '\r'].contains(&input.chars().next_back().unwrap()) { input.pop(); } return input; } +/// Equivalent to the [`inputln`] method except that a prompt is not printed before +/// capturing input. +/// +/// [`inputln`]: #method.inputln +/// +/// ### Note: Safety and Usage +/// This function is not the recommended solution to get user input, it should +/// only be used for simple actions. If you require more explicit control over +/// getting user input, see the [`Stdin::read_line`] method. +/// +/// [`Stdin::read_line`]: struct.Stdin.html#method.read_line +/// +/// # Examples +/// +/// ```rust +/// use std::io::input; +/// +/// fn main() { +/// print!("Please enter some text: "); +/// +/// let user_input = input(); +/// +/// println!("You typed: {}", user_input); +/// +/// } +/// ``` +pub fn input() -> String { + let mut input = String::new(); + let _ = stdout().flush(); + stdin() + .read_line(&mut input) + .expect("Failed to read from stdin"); + return input; +} + /// Resets the thread-local stderr handle to the specified writer /// /// This will replace the current thread's stderr handle, returning the old From ef12ef87f2146436125c7a79fbde0ad26dfe2856 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 15:54:37 -0500 Subject: [PATCH 04/69] Update stdio.rs - Accounted for all return options, ("\n"), ("\r"), ("\r\n"), ("\n\r") --- src/libstd/io/stdio.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index cb59fcb1a21c3..2a83a7170bd28 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -909,7 +909,10 @@ pub fn inputln(prompt: &str) -> String { stdin() .read_line(&mut input) .expect("Failed to read from stdin"); - if ['\n', '\r'].contains(&input.chars().next_back().unwrap()) { + if input.chars().next_back().unwrap() == '\n' { + input.pop(); + } + if input.chars().next_back().unwrap() == '\r' { input.pop(); } return input; @@ -947,6 +950,12 @@ pub fn input() -> String { stdin() .read_line(&mut input) .expect("Failed to read from stdin"); + if input.chars().next_back().unwrap() == '\n' { + input.pop(); + } + if input.chars().next_back().unwrap() == '\r' { + input.pop(); + } return input; } From d75a8a68ffc04a189ea8ff115d4c7433a6f84615 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 16:08:51 -0500 Subject: [PATCH 05/69] Update stdio.rs Streamlined the ```inputln(&str)``` function. --- src/libstd/io/stdio.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 2a83a7170bd28..17eaa74991a45 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -904,18 +904,7 @@ pub fn inputln(prompt: &str) -> String { stdout() .write(prompt.as_bytes()) .unwrap(); - let mut input = String::new(); - let _ = stdout().flush(); - stdin() - .read_line(&mut input) - .expect("Failed to read from stdin"); - if input.chars().next_back().unwrap() == '\n' { - input.pop(); - } - if input.chars().next_back().unwrap() == '\r' { - input.pop(); - } - return input; + return input(); } /// Equivalent to the [`inputln`] method except that a prompt is not printed before From a336e600b6fae2c1d558d3b692f5744c1038334a Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 16:13:23 -0500 Subject: [PATCH 06/69] Update stdio.rs Changed ```inputln(&str)``` function name to better represent the function. --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 17eaa74991a45..3f4a847b5e766 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -900,7 +900,7 @@ impl fmt::Debug for StderrLock<'_> { /// /// } /// ``` -pub fn inputln(prompt: &str) -> String { +pub fn input_prompt(prompt: &str) -> String { stdout() .write(prompt.as_bytes()) .unwrap(); From c635807cd2ca95c7489418bc9cc7de98951a2288 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 16:16:30 -0500 Subject: [PATCH 07/69] Update stdio.rs Fixed docs --- src/libstd/io/stdio.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 3f4a847b5e766..daa0792fc9dcf 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -891,10 +891,10 @@ impl fmt::Debug for StderrLock<'_> { /// # Examples /// /// ```rust -/// use std::io::inputln; +/// use std::io::input_prompt; /// /// fn main() { -/// let user_input = inputln("Please enter some text: "); +/// let user_input = input_prompt("Please enter some text: "); /// /// println!("You typed: {}", user_input); /// @@ -907,10 +907,10 @@ pub fn input_prompt(prompt: &str) -> String { return input(); } -/// Equivalent to the [`inputln`] method except that a prompt is not printed before +/// Equivalent to the [`input_prompt`] method except that a prompt is not printed before /// capturing input. /// -/// [`inputln`]: #method.inputln +/// [`input_prompt`]: #method.input_prompt /// /// ### Note: Safety and Usage /// This function is not the recommended solution to get user input, it should From 23ad1f02d39a5b4c61d5af0f650b5addbd36a1d9 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 19:43:35 -0500 Subject: [PATCH 08/69] Update stdio.rs Attempting to fix, Error unused function. --- src/libstd/io/stdio.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index daa0792fc9dcf..5ae639b9c81b8 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -900,6 +900,7 @@ impl fmt::Debug for StderrLock<'_> { /// /// } /// ``` +#[stable(feature = "input_prompt", since = "1.44.1")] pub fn input_prompt(prompt: &str) -> String { stdout() .write(prompt.as_bytes()) @@ -933,6 +934,7 @@ pub fn input_prompt(prompt: &str) -> String { /// /// } /// ``` +#[stable(feature = "input", since = "1.44.1")] pub fn input() -> String { let mut input = String::new(); let _ = stdout().flush(); From 6885ef3ac6949d94450925283a95b7ec819f0d9c Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 20:03:33 -0500 Subject: [PATCH 09/69] Update stdio.rs Set functions as unstable --- src/libstd/io/stdio.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 5ae639b9c81b8..a335bea003ce5 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -900,7 +900,11 @@ impl fmt::Debug for StderrLock<'_> { /// /// } /// ``` -#[stable(feature = "input_prompt", since = "1.44.1")] +#[unstable( + feature = "input_prompt", + reason = "this function may be replaced with a more general mechanism", + issue = "none" +)] pub fn input_prompt(prompt: &str) -> String { stdout() .write(prompt.as_bytes()) @@ -934,7 +938,11 @@ pub fn input_prompt(prompt: &str) -> String { /// /// } /// ``` -#[stable(feature = "input", since = "1.44.1")] +#[unstable( + feature = "input", + reason = "this function may be replaced with a more general mechanism", + issue = "none" +)] pub fn input() -> String { let mut input = String::new(); let _ = stdout().flush(); From 3248baa2f2ca59bbd5bcda1b0157bb765ebdbc29 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 21:26:19 -0500 Subject: [PATCH 10/69] Update mod.rs Use input functions. --- src/libstd/io/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 717d2868abf98..0de7e0e0038ec 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -287,6 +287,8 @@ pub use self::stdio::{_eprint, _print}; #[unstable(feature = "libstd_io_internals", issue = "42788")] #[doc(no_inline, hidden)] pub use self::stdio::{set_panic, set_print}; +#[unstable(feature = "input", issue = "none")] +pub use self::stdio::{input_prompt, input}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::util::{copy, empty, repeat, sink, Empty, Repeat, Sink}; From 89b682739d62e310f21476c2e2a35f80ab71e08d Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 9 Jul 2020 22:03:54 -0500 Subject: [PATCH 11/69] Update stdio.rs Flagged by tidy for trailing whitespace? --- src/libstd/io/stdio.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index a335bea003ce5..ef4755c8c198f 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -885,7 +885,7 @@ impl fmt::Debug for StderrLock<'_> { /// This function is not the recommended solution to get user input, it should /// only be used for simple actions. If you require more explicit control over /// getting user input, see the [`Stdin::read_line`] method. -/// +/// /// [`Stdin::read_line`]: struct.Stdin.html#method.read_line /// /// # Examples @@ -895,9 +895,9 @@ impl fmt::Debug for StderrLock<'_> { /// /// fn main() { /// let user_input = input_prompt("Please enter some text: "); -/// +/// /// println!("You typed: {}", user_input); -/// +/// /// } /// ``` #[unstable( @@ -916,12 +916,12 @@ pub fn input_prompt(prompt: &str) -> String { /// capturing input. /// /// [`input_prompt`]: #method.input_prompt -/// +/// /// ### Note: Safety and Usage /// This function is not the recommended solution to get user input, it should /// only be used for simple actions. If you require more explicit control over /// getting user input, see the [`Stdin::read_line`] method. -/// +/// /// [`Stdin::read_line`]: struct.Stdin.html#method.read_line /// /// # Examples @@ -931,11 +931,11 @@ pub fn input_prompt(prompt: &str) -> String { /// /// fn main() { /// print!("Please enter some text: "); -/// +/// /// let user_input = input(); -/// +/// /// println!("You typed: {}", user_input); -/// +/// /// } /// ``` #[unstable( From 6ed2162fcdaf23d226f4668a2b54715e9c607d87 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 12:58:21 -0500 Subject: [PATCH 12/69] Update stdio.rs doc changes and faster whitespace remover --- src/libstd/io/stdio.rs | 53 ++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index ef4755c8c198f..91dcf0f1743ca 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -872,61 +872,58 @@ impl fmt::Debug for StderrLock<'_> { } } -/// Locks the current handle and reads a line of input, returning a `String` containing -/// the input. +/// Equivalent to the [`input`] method except that +/// a prompt is printed before capturing input. /// -/// If you need more explicit control over -/// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. -/// -/// [`Stdin::lock`]: struct.Stdin.html#method.lock -/// [`Stdout::lock`]: struct.Stdout.html#method.lock -/// -/// ### Note: Safety and Usage +/// ## Note /// This function is not the recommended solution to get user input, it should /// only be used for simple actions. If you require more explicit control over /// getting user input, see the [`Stdin::read_line`] method. /// -/// [`Stdin::read_line`]: struct.Stdin.html#method.read_line +/// ## Panics +/// This function currently panics if it does not receive input, +/// or when input is empty without newline /// /// # Examples /// -/// ```rust +/// ``` /// use std::io::input_prompt; /// /// fn main() { /// let user_input = input_prompt("Please enter some text: "); /// /// println!("You typed: {}", user_input); -/// /// } /// ``` #[unstable( feature = "input_prompt", - reason = "this function may be replaced with a more general mechanism", + reason = "this function may be replaced with a \ + more general mechanism", issue = "none" )] pub fn input_prompt(prompt: &str) -> String { - stdout() - .write(prompt.as_bytes()) - .unwrap(); - return input(); + stdout().write(prompt.as_bytes()).unwrap(); + input() } -/// Equivalent to the [`input_prompt`] method except that a prompt is not printed before -/// capturing input. +/// Locks the current handle and reads a line of input, +/// returning a `String` containing the input. /// -/// [`input_prompt`]: #method.input_prompt +/// If you need more explicit control over +/// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. /// -/// ### Note: Safety and Usage +/// ## Note /// This function is not the recommended solution to get user input, it should /// only be used for simple actions. If you require more explicit control over /// getting user input, see the [`Stdin::read_line`] method. /// -/// [`Stdin::read_line`]: struct.Stdin.html#method.read_line +/// ## Panics +/// This function currently panics if it does not receive input, +/// or when input is empty without newline /// /// # Examples /// -/// ```rust +/// ``` /// use std::io::input; /// /// fn main() { @@ -935,12 +932,12 @@ pub fn input_prompt(prompt: &str) -> String { /// let user_input = input(); /// /// println!("You typed: {}", user_input); -/// /// } /// ``` #[unstable( feature = "input", - reason = "this function may be replaced with a more general mechanism", + reason = "this function may be replaced with a \ + more general mechanism", issue = "none" )] pub fn input() -> String { @@ -949,13 +946,13 @@ pub fn input() -> String { stdin() .read_line(&mut input) .expect("Failed to read from stdin"); - if input.chars().next_back().unwrap() == '\n' { + if input.as_bytes()[input.len()-1] == b'\n' { input.pop(); } - if input.chars().next_back().unwrap() == '\r' { + if input.as_bytes()[input.len()-1] == b'\r' { input.pop(); } - return input; + input } /// Resets the thread-local stderr handle to the specified writer From d1c652ba9c7c53a31aa8676b8a753941551f307a Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 13:17:54 -0500 Subject: [PATCH 13/69] Update stdio.rs docs fix --- src/libstd/io/stdio.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 91dcf0f1743ca..7521eb818e0bc 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -906,7 +906,8 @@ pub fn input_prompt(prompt: &str) -> String { input() } -/// Locks the current handle and reads a line of input, +/// Constructs a new handle to the standard input of the current +/// process, locks this handle and reads a line of input, /// returning a `String` containing the input. /// /// If you need more explicit control over From aaebbe3de5cc47e8db1d3773913944fc4ec58211 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 18:32:25 -0500 Subject: [PATCH 14/69] Update stdio.rs format corrections --- src/libstd/io/stdio.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 7521eb818e0bc..c44c10ce1451c 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -944,13 +944,11 @@ pub fn input_prompt(prompt: &str) -> String { pub fn input() -> String { let mut input = String::new(); let _ = stdout().flush(); - stdin() - .read_line(&mut input) - .expect("Failed to read from stdin"); - if input.as_bytes()[input.len()-1] == b'\n' { + stdin().read_line(&mut input).expect("Failed to read from stdin"); + if input.as_bytes()[input.len() - 1] == b'\n' { input.pop(); } - if input.as_bytes()[input.len()-1] == b'\r' { + if input.as_bytes()[input.len() - 1] == b'\r' { input.pop(); } input From 14f85a50d56719811b7e8eacc0f55e6d4a0d28e9 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 18:34:33 -0500 Subject: [PATCH 15/69] Update mod.rs formatting corrections --- src/libstd/io/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 0de7e0e0038ec..36802d5ec8453 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -284,11 +284,11 @@ pub use self::stdio::{stderr, stdin, stdout, Stderr, Stdin, Stdout}; pub use self::stdio::{StderrLock, StdinLock, StdoutLock}; #[unstable(feature = "print_internals", issue = "none")] pub use self::stdio::{_eprint, _print}; +#[unstable(feature = "input", issue = "none")] +pub use self::stdio::{input, input_prompt}; #[unstable(feature = "libstd_io_internals", issue = "42788")] #[doc(no_inline, hidden)] pub use self::stdio::{set_panic, set_print}; -#[unstable(feature = "input", issue = "none")] -pub use self::stdio::{input_prompt, input}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::util::{copy, empty, repeat, sink, Empty, Repeat, Sink}; From 5f107474672ef1ae333aa7b764b58cebec082be1 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 19:09:48 -0500 Subject: [PATCH 16/69] Update lib.rs enable functions --- src/libstd/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index bd585d39c242f..422dc5a6f790d 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -274,6 +274,8 @@ #![feature(global_asm)] #![feature(hash_raw_entry)] #![feature(hashmap_internals)] +#![feature(input)] +#![feature(input_prompt)] #![feature(int_error_internals)] #![feature(int_error_matching)] #![feature(into_future)] From 26eab4f69b96b5f65e623c6eb8c49422fa6242e3 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 19:53:07 -0500 Subject: [PATCH 17/69] Update stdio.rs enable feature within docs? --- src/libstd/io/stdio.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index c44c10ce1451c..5f748c75abaf2 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -887,6 +887,7 @@ impl fmt::Debug for StderrLock<'_> { /// # Examples /// /// ``` +/// #![feature(input_prompt)] /// use std::io::input_prompt; /// /// fn main() { @@ -925,6 +926,7 @@ pub fn input_prompt(prompt: &str) -> String { /// # Examples /// /// ``` +/// #![feature(input)] /// use std::io::input; /// /// fn main() { From e52d227ec57cfc247a7af5ad37fea0d9c01dcc64 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 21:11:03 -0500 Subject: [PATCH 18/69] Update stdio.rs Allow function to catch, input of `None` --- src/libstd/io/stdio.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 5f748c75abaf2..a43de92e1dd7d 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -946,14 +946,19 @@ pub fn input_prompt(prompt: &str) -> String { pub fn input() -> String { let mut input = String::new(); let _ = stdout().flush(); - stdin().read_line(&mut input).expect("Failed to read from stdin"); - if input.as_bytes()[input.len() - 1] == b'\n' { - input.pop(); - } - if input.as_bytes()[input.len() - 1] == b'\r' { - input.pop(); + let len = stdin().read_line(&mut input).expect("Failed to read from stdin"); + match len { + 0 => String::new(), + _ => { + if input.as_bytes()[input.len()-1] == b'\n' { + input.pop(); + } + if input.as_bytes()[input.len()-1] == b'\r' { + input.pop(); + } + input + } } - input } /// Resets the thread-local stderr handle to the specified writer From b819421e6057e675b4f1cd495ecf1142a6b1abd7 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 21:28:14 -0500 Subject: [PATCH 19/69] Update stdio.rs Format corrections --- src/libstd/io/stdio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index a43de92e1dd7d..0f3cc77bfb283 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -950,10 +950,10 @@ pub fn input() -> String { match len { 0 => String::new(), _ => { - if input.as_bytes()[input.len()-1] == b'\n' { + if input.as_bytes()[input.len() - 1] == b'\n' { input.pop(); } - if input.as_bytes()[input.len()-1] == b'\r' { + if input.as_bytes()[input.len() - 1] == b'\r' { input.pop(); } input From 92b09e714dd87e2123bc937b161b41f03c9cc4a5 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 22:43:58 -0500 Subject: [PATCH 20/69] Update stdio.rs Add link to `input_prompt()` in docs --- src/libstd/io/stdio.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 0f3cc77bfb283..e6ea9d7948d39 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -909,7 +909,8 @@ pub fn input_prompt(prompt: &str) -> String { /// Constructs a new handle to the standard input of the current /// process, locks this handle and reads a line of input, -/// returning a `String` containing the input. +/// returning a `String` containing the input. For automatic +/// prompt handling, see the [`input_prompt`] method. /// /// If you need more explicit control over /// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. From 53454c23f9dcfb66b2ebe80ea342ba91be0b9352 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 22:48:05 -0500 Subject: [PATCH 21/69] Update src/libstd/io/stdio.rs doc fixes Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index e6ea9d7948d39..ebab601519c58 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -875,7 +875,8 @@ impl fmt::Debug for StderrLock<'_> { /// Equivalent to the [`input`] method except that /// a prompt is printed before capturing input. /// -/// ## Note +/// # Note +/// /// This function is not the recommended solution to get user input, it should /// only be used for simple actions. If you require more explicit control over /// getting user input, see the [`Stdin::read_line`] method. From 319f2aee6343ccd3a0c4041786c6319c754158b1 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 22:49:08 -0500 Subject: [PATCH 22/69] Update src/libstd/io/stdio.rs doc fix Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index ebab601519c58..ff51793733d38 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -881,7 +881,8 @@ impl fmt::Debug for StderrLock<'_> { /// only be used for simple actions. If you require more explicit control over /// getting user input, see the [`Stdin::read_line`] method. /// -/// ## Panics +/// # Panics +/// /// This function currently panics if it does not receive input, /// or when input is empty without newline /// From 11df63a21d78ab1a1ed0e2267bd333ecdd693e2a Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 23:00:18 -0500 Subject: [PATCH 23/69] Update stdio.rs Add panic on `len == 0` --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index ff51793733d38..0c2b8d32ac8b9 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -951,7 +951,7 @@ pub fn input() -> String { let _ = stdout().flush(); let len = stdin().read_line(&mut input).expect("Failed to read from stdin"); match len { - 0 => String::new(), + 0 => panic!("invalid character!"), _ => { if input.as_bytes()[input.len() - 1] == b'\n' { input.pop(); From 0a3a6618a73992a3719f448c218a5159b88a3af5 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 23:02:11 -0500 Subject: [PATCH 24/69] Update src/libstd/io/stdio.rs doc fix Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 0c2b8d32ac8b9..98fa6d0eaba1c 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -917,7 +917,8 @@ pub fn input_prompt(prompt: &str) -> String { /// If you need more explicit control over /// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. /// -/// ## Note +/// # Note +/// /// This function is not the recommended solution to get user input, it should /// only be used for simple actions. If you require more explicit control over /// getting user input, see the [`Stdin::read_line`] method. From a164ea3703ed0d4ce204e21d47553ff0a4907afa Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 23:02:22 -0500 Subject: [PATCH 25/69] Update src/libstd/io/stdio.rs doc fix Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 98fa6d0eaba1c..dc6b4a4f71a27 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -923,7 +923,8 @@ pub fn input_prompt(prompt: &str) -> String { /// only be used for simple actions. If you require more explicit control over /// getting user input, see the [`Stdin::read_line`] method. /// -/// ## Panics +/// # Panics +/// /// This function currently panics if it does not receive input, /// or when input is empty without newline /// From 5bd01542e5935af6ca9c072866f098555a77abef Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 23:02:50 -0500 Subject: [PATCH 26/69] Update src/libstd/io/stdio.rs doc fix Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index dc6b4a4f71a27..f720afb9ee458 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -888,7 +888,7 @@ impl fmt::Debug for StderrLock<'_> { /// /// # Examples /// -/// ``` +/// ```no_run /// #![feature(input_prompt)] /// use std::io::input_prompt; /// From 800a5a9c6d71b26d5de9fb3a190633851bfeb699 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 23:03:06 -0500 Subject: [PATCH 27/69] Update src/libstd/io/stdio.rs doc fix Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index f720afb9ee458..c00a3a885fc22 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -930,7 +930,7 @@ pub fn input_prompt(prompt: &str) -> String { /// /// # Examples /// -/// ``` +/// ```no_run /// #![feature(input)] /// use std::io::input; /// From 9a0326f516e961c74b7f7f655026dc53d228af21 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 23:05:02 -0500 Subject: [PATCH 28/69] Update stdio.rs Changed `input_prompt` to `prompt` for simplicity --- src/libstd/io/stdio.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index c00a3a885fc22..9df578b8f02fd 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -889,22 +889,22 @@ impl fmt::Debug for StderrLock<'_> { /// # Examples /// /// ```no_run -/// #![feature(input_prompt)] -/// use std::io::input_prompt; +/// #![feature(prompt)] +/// use std::io::prompt; /// /// fn main() { -/// let user_input = input_prompt("Please enter some text: "); +/// let user_input = prompt("Please enter some text: "); /// /// println!("You typed: {}", user_input); /// } /// ``` #[unstable( - feature = "input_prompt", + feature = "prompt", reason = "this function may be replaced with a \ more general mechanism", issue = "none" )] -pub fn input_prompt(prompt: &str) -> String { +pub fn prompt(prompt: &str) -> String { stdout().write(prompt.as_bytes()).unwrap(); input() } @@ -912,7 +912,7 @@ pub fn input_prompt(prompt: &str) -> String { /// Constructs a new handle to the standard input of the current /// process, locks this handle and reads a line of input, /// returning a `String` containing the input. For automatic -/// prompt handling, see the [`input_prompt`] method. +/// prompt handling, see the [`prompt`] method. /// /// If you need more explicit control over /// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. From 8c1ea9d6e4582affd69759348243352cb923e76d Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 23:06:43 -0500 Subject: [PATCH 29/69] Update mod.rs function name change --- src/libstd/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 36802d5ec8453..e0f12c837b6c4 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -285,7 +285,7 @@ pub use self::stdio::{StderrLock, StdinLock, StdoutLock}; #[unstable(feature = "print_internals", issue = "none")] pub use self::stdio::{_eprint, _print}; #[unstable(feature = "input", issue = "none")] -pub use self::stdio::{input, input_prompt}; +pub use self::stdio::{input, prompt}; #[unstable(feature = "libstd_io_internals", issue = "42788")] #[doc(no_inline, hidden)] pub use self::stdio::{set_panic, set_print}; From e8a0939ee9414c45735111077efdf99ebc274ea6 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 10 Jul 2020 23:07:34 -0500 Subject: [PATCH 30/69] Update lib.rs function name change --- src/libstd/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 422dc5a6f790d..aaa2358b89b49 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -275,7 +275,6 @@ #![feature(hash_raw_entry)] #![feature(hashmap_internals)] #![feature(input)] -#![feature(input_prompt)] #![feature(int_error_internals)] #![feature(int_error_matching)] #![feature(into_future)] @@ -298,6 +297,7 @@ #![feature(panic_internals)] #![feature(panic_unwind)] #![feature(prelude_import)] +#![feature(prompt)] #![feature(ptr_internals)] #![feature(raw)] #![feature(raw_ref_macros)] From 320580d10c97550808b98ec04a848373ba4a22bb Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sat, 11 Jul 2020 00:32:29 -0500 Subject: [PATCH 31/69] Update stdio.rs Added cross platform whitespace trimming --- src/libstd/io/stdio.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 9df578b8f02fd..acc879f624baf 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -955,11 +955,11 @@ pub fn input() -> String { match len { 0 => panic!("invalid character!"), _ => { - if input.as_bytes()[input.len() - 1] == b'\n' { - input.pop(); - } - if input.as_bytes()[input.len() - 1] == b'\r' { + if input.ends_with('\n') { input.pop(); + if input.ends_with('\r') { + input.pop(); + } } input } From c97c8115a5e0f94cc3df5eae109c16a5cb3550f9 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sat, 11 Jul 2020 00:38:15 -0500 Subject: [PATCH 32/69] Update stdio.rs Added better examples in docs --- src/libstd/io/stdio.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index acc879f624baf..63a98610bd9b3 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -893,9 +893,9 @@ impl fmt::Debug for StderrLock<'_> { /// use std::io::prompt; /// /// fn main() { -/// let user_input = prompt("Please enter some text: "); +/// let name = prompt("What is your name?: "); /// -/// println!("You typed: {}", user_input); +/// println!("Your name is {}!", name); /// } /// ``` #[unstable( @@ -935,11 +935,11 @@ pub fn prompt(prompt: &str) -> String { /// use std::io::input; /// /// fn main() { -/// print!("Please enter some text: "); +/// print!("What is your name?: "); /// -/// let user_input = input(); +/// let name = input(); /// -/// println!("You typed: {}", user_input); +/// println!("Your name is {}!", name); /// } /// ``` #[unstable( From 7c59e80114e33bd37cb04b03f2065d39128e9c2d Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sat, 11 Jul 2020 11:14:28 -0500 Subject: [PATCH 33/69] Update stdio.rs fixed docs example --- src/libstd/io/stdio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 63a98610bd9b3..6fb51ffae3568 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -893,7 +893,7 @@ impl fmt::Debug for StderrLock<'_> { /// use std::io::prompt; /// /// fn main() { -/// let name = prompt("What is your name?: "); +/// let name = prompt("Enter name: "); /// /// println!("Your name is {}!", name); /// } @@ -935,7 +935,7 @@ pub fn prompt(prompt: &str) -> String { /// use std::io::input; /// /// fn main() { -/// print!("What is your name?: "); +/// print!("Enter name: "); /// /// let name = input(); /// From 9d6be00f561cc403f0aa3711f66b55f42080eeb3 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sat, 11 Jul 2020 18:35:52 -0500 Subject: [PATCH 34/69] Update stdio.rs Implemented straight forward whitespace remover. --- src/libstd/io/stdio.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 6fb51ffae3568..010f50e777206 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -954,15 +954,7 @@ pub fn input() -> String { let len = stdin().read_line(&mut input).expect("Failed to read from stdin"); match len { 0 => panic!("invalid character!"), - _ => { - if input.ends_with('\n') { - input.pop(); - if input.ends_with('\r') { - input.pop(); - } - } - input - } + _ => String::from(input.trim_end_matches('\n').trim_end_matches('\r')), } } From df08102ad62e9fa67bcea8e5662411fa77ec4a75 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sun, 12 Jul 2020 00:11:29 -0500 Subject: [PATCH 35/69] Update stdio.rs Streamlined whitespace stripping --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 010f50e777206..4bd17691593b6 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -954,7 +954,7 @@ pub fn input() -> String { let len = stdin().read_line(&mut input).expect("Failed to read from stdin"); match len { 0 => panic!("invalid character!"), - _ => String::from(input.trim_end_matches('\n').trim_end_matches('\r')), + _ => String::from(input.trim_end_matches(&['\n','\r'][..])), } } From 9bdd06f7f422bdd379499b8147d6ff843909cb7f Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sun, 12 Jul 2020 00:41:09 -0500 Subject: [PATCH 36/69] Update stdio.rs Checker complained because of ONE space... ok --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 4bd17691593b6..ed2b24fc008e9 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -954,7 +954,7 @@ pub fn input() -> String { let len = stdin().read_line(&mut input).expect("Failed to read from stdin"); match len { 0 => panic!("invalid character!"), - _ => String::from(input.trim_end_matches(&['\n','\r'][..])), + _ => String::from(input.trim_end_matches(&['\n', '\r'][..])), } } From 3db978139998bf8026c5a9e2819dd051dbbbaae7 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sun, 12 Jul 2020 09:59:19 -0500 Subject: [PATCH 37/69] Update src/libstd/io/stdio.rs Streamlined function Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index ed2b24fc008e9..13fc592f356d7 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -951,9 +951,8 @@ pub fn prompt(prompt: &str) -> String { pub fn input() -> String { let mut input = String::new(); let _ = stdout().flush(); - let len = stdin().read_line(&mut input).expect("Failed to read from stdin"); - match len { - 0 => panic!("invalid character!"), + match stdin().read_line(&mut input).expect("failed to read from stdin") { + 0 => panic!("unexpected end of file"), _ => String::from(input.trim_end_matches(&['\n', '\r'][..])), } } From f95ac818c53892ab8f2bf818666c99d187ba6ab2 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sun, 12 Jul 2020 09:59:45 -0500 Subject: [PATCH 38/69] Update src/libstd/io/stdio.rs doc fix Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 13fc592f356d7..b4031ac4be8e7 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -925,8 +925,8 @@ pub fn prompt(prompt: &str) -> String { /// /// # Panics /// -/// This function currently panics if it does not receive input, -/// or when input is empty without newline +/// This function currently panics if it does not receive input +/// or when input is empty (end of file / `ctrl + d`). /// /// # Examples /// From c01126a2975520e7dd5564a2b899c45d1e275972 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sun, 12 Jul 2020 10:00:18 -0500 Subject: [PATCH 39/69] Update src/libstd/io/mod.rs split into two features Co-authored-by: Ivan Tham --- src/libstd/io/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index e0f12c837b6c4..db8e846cb0491 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -285,7 +285,9 @@ pub use self::stdio::{StderrLock, StdinLock, StdoutLock}; #[unstable(feature = "print_internals", issue = "none")] pub use self::stdio::{_eprint, _print}; #[unstable(feature = "input", issue = "none")] -pub use self::stdio::{input, prompt}; +pub use self::stdio::input; +#[unstable(feature = "prompt", issue = "none")] +pub use self::stdio::prompt; #[unstable(feature = "libstd_io_internals", issue = "42788")] #[doc(no_inline, hidden)] pub use self::stdio::{set_panic, set_print}; From f3d3e21c4ff9d7b20817fda7e3b0458ffb9fa97a Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sun, 12 Jul 2020 10:16:51 -0500 Subject: [PATCH 40/69] Update mod.rs change use location --- src/libstd/io/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index db8e846cb0491..46c2f18a8cb7d 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -278,16 +278,16 @@ pub use self::buffered::{BufReader, BufWriter, LineWriter}; pub use self::cursor::Cursor; #[stable(feature = "rust1", since = "1.0.0")] pub use self::error::{Error, ErrorKind, Result}; +#[unstable(feature = "input", issue = "none")] +pub use self::stdio::input; +#[unstable(feature = "prompt", issue = "none")] +pub use self::stdio::prompt; #[stable(feature = "rust1", since = "1.0.0")] pub use self::stdio::{stderr, stdin, stdout, Stderr, Stdin, Stdout}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::stdio::{StderrLock, StdinLock, StdoutLock}; #[unstable(feature = "print_internals", issue = "none")] pub use self::stdio::{_eprint, _print}; -#[unstable(feature = "input", issue = "none")] -pub use self::stdio::input; -#[unstable(feature = "prompt", issue = "none")] -pub use self::stdio::prompt; #[unstable(feature = "libstd_io_internals", issue = "42788")] #[doc(no_inline, hidden)] pub use self::stdio::{set_panic, set_print}; From 7cb92bf8f91f8209b5b4ab868e206069f9b63ab7 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sun, 12 Jul 2020 10:24:41 -0500 Subject: [PATCH 41/69] Update stdio.rs better errors --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index b4031ac4be8e7..5c0519809b061 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -905,7 +905,7 @@ impl fmt::Debug for StderrLock<'_> { issue = "none" )] pub fn prompt(prompt: &str) -> String { - stdout().write(prompt.as_bytes()).unwrap(); + stdout().write(prompt.as_bytes()).expect("failed to write to stdout"); input() } From ba79fc5434dd1a8103e25453158f78e08fdc131c Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Sun, 12 Jul 2020 10:49:30 -0500 Subject: [PATCH 42/69] Update stdio.rs better errors pt2 --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 5c0519809b061..40d779adf7e89 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -950,7 +950,7 @@ pub fn prompt(prompt: &str) -> String { )] pub fn input() -> String { let mut input = String::new(); - let _ = stdout().flush(); + stdout().flush().expect("failed to flush stdout"); match stdin().read_line(&mut input).expect("failed to read from stdin") { 0 => panic!("unexpected end of file"), _ => String::from(input.trim_end_matches(&['\n', '\r'][..])), From c2f6e9ddedba79f37868ff165ea82db7b345571c Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Mon, 13 Jul 2020 15:14:58 -0500 Subject: [PATCH 43/69] Update stdio.rs Switched `String` to `Result` to let user handle the error. --- src/libstd/io/stdio.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 40d779adf7e89..2fe66a21343d0 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -5,7 +5,7 @@ use crate::io::prelude::*; use crate::cell::RefCell; use crate::fmt; use crate::io::lazy::Lazy; -use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter, Write}; +use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter, Result, Write}; use crate::sync::{Arc, Mutex, MutexGuard, Once}; use crate::sys::stdio; use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard}; @@ -893,7 +893,7 @@ impl fmt::Debug for StderrLock<'_> { /// use std::io::prompt; /// /// fn main() { -/// let name = prompt("Enter name: "); +/// let name = prompt("Enter name: ").expect("input failed!"); /// /// println!("Your name is {}!", name); /// } @@ -904,9 +904,9 @@ impl fmt::Debug for StderrLock<'_> { more general mechanism", issue = "none" )] -pub fn prompt(prompt: &str) -> String { +pub fn prompt(prompt: &str) -> Result { stdout().write(prompt.as_bytes()).expect("failed to write to stdout"); - input() + Ok(input()?) } /// Constructs a new handle to the standard input of the current @@ -948,12 +948,12 @@ pub fn prompt(prompt: &str) -> String { more general mechanism", issue = "none" )] -pub fn input() -> String { +pub fn input() -> Result { let mut input = String::new(); stdout().flush().expect("failed to flush stdout"); match stdin().read_line(&mut input).expect("failed to read from stdin") { 0 => panic!("unexpected end of file"), - _ => String::from(input.trim_end_matches(&['\n', '\r'][..])), + _ => Ok(String::from(input.trim_end_matches(&['\n', '\r'][..]))), } } From e094e5d3a33afbbd83d47dc50b8c9082c543a8e1 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Mon, 13 Jul 2020 15:18:29 -0500 Subject: [PATCH 44/69] Update stdio.rs doc fix --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 2fe66a21343d0..25cf2bd4c0672 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -937,7 +937,7 @@ pub fn prompt(prompt: &str) -> Result { /// fn main() { /// print!("Enter name: "); /// -/// let name = input(); +/// let name = input().expect("input failed!"); /// /// println!("Your name is {}!", name); /// } From dde8923641a0cdad42cfe845c633cce1cdeec8de Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Tue, 14 Jul 2020 12:51:01 -0500 Subject: [PATCH 45/69] Update stdio.rs error fixing --- src/libstd/io/stdio.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 25cf2bd4c0672..fa5eba4e703ab 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -872,8 +872,11 @@ impl fmt::Debug for StderrLock<'_> { } } -/// Equivalent to the [`input`] method except that -/// a prompt is printed before capturing input. +/// Equivalent to the [`input`] method except that a prompt +/// is printed before capturing input. This function returns +/// either success ([`Ok`]) or failure ([`Err`]) containing +/// the input. See the [`std::result`] module documentation +/// for details. /// /// # Note /// @@ -884,7 +887,7 @@ impl fmt::Debug for StderrLock<'_> { /// # Panics /// /// This function currently panics if it does not receive input, -/// or when input is empty without newline +/// or when input is empty (end of file / `ctrl + d`). /// /// # Examples /// @@ -905,14 +908,15 @@ impl fmt::Debug for StderrLock<'_> { issue = "none" )] pub fn prompt(prompt: &str) -> Result { - stdout().write(prompt.as_bytes()).expect("failed to write to stdout"); - Ok(input()?) + stdout().write(prompt.as_bytes())?; + input() } /// Constructs a new handle to the standard input of the current -/// process, locks this handle and reads a line of input, -/// returning a `String` containing the input. For automatic -/// prompt handling, see the [`prompt`] method. +/// process, locks this handle and reads a line of input, returning +/// either success ([`Ok`]) or failure ([`Err`]) containing the input. +/// See the [`std::result`] module documentation for details. For +/// automatic prompt handling, see the [`prompt`] method. /// /// If you need more explicit control over /// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. @@ -949,9 +953,9 @@ pub fn prompt(prompt: &str) -> Result { issue = "none" )] pub fn input() -> Result { + stdout().flush()?; let mut input = String::new(); - stdout().flush().expect("failed to flush stdout"); - match stdin().read_line(&mut input).expect("failed to read from stdin") { + match stdin().read_line(&mut input)? { 0 => panic!("unexpected end of file"), _ => Ok(String::from(input.trim_end_matches(&['\n', '\r'][..]))), } From 93cea0d896b157f6da3f3d0b633cfeddc1d45b85 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Tue, 14 Jul 2020 14:37:22 -0500 Subject: [PATCH 46/69] Update stdio.rs doc fix --- src/libstd/io/stdio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index fa5eba4e703ab..2e52e6bcfef4f 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -875,7 +875,7 @@ impl fmt::Debug for StderrLock<'_> { /// Equivalent to the [`input`] method except that a prompt /// is printed before capturing input. This function returns /// either success ([`Ok`]) or failure ([`Err`]) containing -/// the input. See the [`std::result`] module documentation +/// the input. See the [`io::result`] module documentation /// for details. /// /// # Note @@ -915,7 +915,7 @@ pub fn prompt(prompt: &str) -> Result { /// Constructs a new handle to the standard input of the current /// process, locks this handle and reads a line of input, returning /// either success ([`Ok`]) or failure ([`Err`]) containing the input. -/// See the [`std::result`] module documentation for details. For +/// See the [`io::result`] module documentation for details. For /// automatic prompt handling, see the [`prompt`] method. /// /// If you need more explicit control over From 8fa04d3cbe69ad4c7245205aad66c2c19b4e8763 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Tue, 14 Jul 2020 15:10:27 -0500 Subject: [PATCH 47/69] Update stdio.rs doc fix --- src/libstd/io/stdio.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 2e52e6bcfef4f..7e4ecdb195fcc 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -875,8 +875,7 @@ impl fmt::Debug for StderrLock<'_> { /// Equivalent to the [`input`] method except that a prompt /// is printed before capturing input. This function returns /// either success ([`Ok`]) or failure ([`Err`]) containing -/// the input. See the [`io::result`] module documentation -/// for details. +/// the input. /// /// # Note /// @@ -915,8 +914,7 @@ pub fn prompt(prompt: &str) -> Result { /// Constructs a new handle to the standard input of the current /// process, locks this handle and reads a line of input, returning /// either success ([`Ok`]) or failure ([`Err`]) containing the input. -/// See the [`io::result`] module documentation for details. For -/// automatic prompt handling, see the [`prompt`] method. +/// For automatic prompt handling, see the [`prompt`] method. /// /// If you need more explicit control over /// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. From 8b5c3313a7f155d6956f07bbf33193ef2f2682e3 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Tue, 14 Jul 2020 16:48:00 -0500 Subject: [PATCH 48/69] Update stdio.rs error fix --- src/libstd/io/stdio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 7e4ecdb195fcc..009d49210e0c1 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -5,7 +5,7 @@ use crate::io::prelude::*; use crate::cell::RefCell; use crate::fmt; use crate::io::lazy::Lazy; -use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter, Result, Write}; +use crate::io::{self, BufReader, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, LineWriter, Result, Write}; use crate::sync::{Arc, Mutex, MutexGuard, Once}; use crate::sys::stdio; use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard}; @@ -954,7 +954,7 @@ pub fn input() -> Result { stdout().flush()?; let mut input = String::new(); match stdin().read_line(&mut input)? { - 0 => panic!("unexpected end of file"), + 0 => Err(Error::new(ErrorKind::UnexpectedEof, "input reached eof unexpectedly.")), _ => Ok(String::from(input.trim_end_matches(&['\n', '\r'][..]))), } } From a5e7d8dce3186c664082b8ec5a20f8606d4b062f Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Tue, 14 Jul 2020 17:46:22 -0500 Subject: [PATCH 49/69] Update stdio.rs format fix --- src/libstd/io/stdio.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 009d49210e0c1..c3a2872565133 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -5,7 +5,9 @@ use crate::io::prelude::*; use crate::cell::RefCell; use crate::fmt; use crate::io::lazy::Lazy; -use crate::io::{self, BufReader, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, LineWriter, Result, Write}; +use crate::io::{ + self, BufReader, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, LineWriter, Result, Write +}; use crate::sync::{Arc, Mutex, MutexGuard, Once}; use crate::sys::stdio; use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard}; From 95ef71a2a284a77f13b3689e6d6e85975c65cc20 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Tue, 14 Jul 2020 17:59:32 -0500 Subject: [PATCH 50/69] Update stdio.rs ONE!! COMMA!!! --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index c3a2872565133..8783c009c835a 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -6,7 +6,7 @@ use crate::cell::RefCell; use crate::fmt; use crate::io::lazy::Lazy; use crate::io::{ - self, BufReader, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, LineWriter, Result, Write + self, BufReader, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, LineWriter, Result, Write, }; use crate::sync::{Arc, Mutex, MutexGuard, Once}; use crate::sys::stdio; From 136c19febdf17a369d4abf6134fdd8fadfeb46cd Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Wed, 15 Jul 2020 14:15:23 -0500 Subject: [PATCH 51/69] Update stdio.rs docs fix --- src/libstd/io/stdio.rs | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 8783c009c835a..cdef50380bc4f 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -877,18 +877,13 @@ impl fmt::Debug for StderrLock<'_> { /// Equivalent to the [`input`] method except that a prompt /// is printed before capturing input. This function returns /// either success ([`Ok`]) or failure ([`Err`]) containing -/// the input. +/// the input. See the [`io::Result`] module documentation +/// for details. /// /// # Note /// -/// This function is not the recommended solution to get user input, it should -/// only be used for simple actions. If you require more explicit control over -/// getting user input, see the [`Stdin::read_line`] method. -/// -/// # Panics -/// -/// This function currently panics if it does not receive input, -/// or when input is empty (end of file / `ctrl + d`). +/// If you require more explicit control over capturing +/// user input, see the [`Stdin::read_line`] method. /// /// # Examples /// @@ -916,21 +911,16 @@ pub fn prompt(prompt: &str) -> Result { /// Constructs a new handle to the standard input of the current /// process, locks this handle and reads a line of input, returning /// either success ([`Ok`]) or failure ([`Err`]) containing the input. -/// For automatic prompt handling, see the [`prompt`] method. +/// See the [`io::Result`] module documentation for details. For +/// automatic prompt handling, see the [`prompt`] method. /// /// If you need more explicit control over /// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. /// /// # Note /// -/// This function is not the recommended solution to get user input, it should -/// only be used for simple actions. If you require more explicit control over -/// getting user input, see the [`Stdin::read_line`] method. -/// -/// # Panics -/// -/// This function currently panics if it does not receive input -/// or when input is empty (end of file / `ctrl + d`). +/// If you require more explicit control over capturing +/// user input, see the [`Stdin::read_line`] method. /// /// # Examples /// From 39fd999a183f5824847ea292298cdd66c7f5e838 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Thu, 16 Jul 2020 17:08:34 -0500 Subject: [PATCH 52/69] Update stdio.rs docs fix --- src/libstd/io/stdio.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index cdef50380bc4f..6a76d0e66543d 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -904,14 +904,13 @@ impl fmt::Debug for StderrLock<'_> { issue = "none" )] pub fn prompt(prompt: &str) -> Result { - stdout().write(prompt.as_bytes())?; + stdout().write_all(prompt.as_bytes())?; input() } -/// Constructs a new handle to the standard input of the current -/// process, locks this handle and reads a line of input, returning -/// either success ([`Ok`]) or failure ([`Err`]) containing the input. -/// See the [`io::Result`] module documentation for details. For +/// Reads a line from standard input, returning either success +/// ([`Ok`]) or failure ([`Err`]) containing the input. See the +/// [`io::Result`] module documentation for details. For /// automatic prompt handling, see the [`prompt`] method. /// /// If you need more explicit control over From 4d39e9da3a9e94872b952d28829326b4d786e752 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 17 Jul 2020 14:08:00 -0500 Subject: [PATCH 53/69] Update src/libstd/io/stdio.rs Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 6a76d0e66543d..7d38c736dc79b 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -874,11 +874,11 @@ impl fmt::Debug for StderrLock<'_> { } } -/// Equivalent to the [`input`] method except that a prompt -/// is printed before capturing input. This function returns -/// either success ([`Ok`]) or failure ([`Err`]) containing -/// the input. See the [`io::Result`] module documentation -/// for details. +/// Prints `prompt` and reads a [`String`] from +/// [standard input](Stdin). The trailing newline is stripped. +/// Errors on EOF (Ctrl-D). +/// +/// Equivalent to [`input`] except it prints prompts. /// /// # Note /// From 53eb268939c0275cbd6523c60e25eec4c87ce330 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 17 Jul 2020 14:08:19 -0500 Subject: [PATCH 54/69] Update src/libstd/io/stdio.rs Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 7d38c736dc79b..0c369aa843b9a 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -899,8 +899,7 @@ impl fmt::Debug for StderrLock<'_> { /// ``` #[unstable( feature = "prompt", - reason = "this function may be replaced with a \ - more general mechanism", + reason = "this function may be replaced with a more general mechanism", issue = "none" )] pub fn prompt(prompt: &str) -> Result { From 9c2f0c5934627261d6a093e933772750b7de2b84 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 17 Jul 2020 14:08:38 -0500 Subject: [PATCH 55/69] Update src/libstd/io/stdio.rs Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 0c369aa843b9a..c03c0293a8a2f 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -891,11 +891,8 @@ impl fmt::Debug for StderrLock<'_> { /// #![feature(prompt)] /// use std::io::prompt; /// -/// fn main() { -/// let name = prompt("Enter name: ").expect("input failed!"); -/// -/// println!("Your name is {}!", name); -/// } +/// let name = prompt("Enter name: ").expect("input failed"); +/// println!("Your name is {}!", name); /// ``` #[unstable( feature = "prompt", From 92a002d380c5311e14e534145e6eb35ad64f6ab1 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 17 Jul 2020 14:08:58 -0500 Subject: [PATCH 56/69] Update src/libstd/io/stdio.rs Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index c03c0293a8a2f..632a08ce14073 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -904,10 +904,10 @@ pub fn prompt(prompt: &str) -> Result { input() } -/// Reads a line from standard input, returning either success -/// ([`Ok`]) or failure ([`Err`]) containing the input. See the -/// [`io::Result`] module documentation for details. For -/// automatic prompt handling, see the [`prompt`] method. +/// Reads a [`String`] from [standard input](Stdin). The +/// trailing newline is stripped. Errors on EOF (Ctrl-D). +/// +/// Equivalent to [`prompt`] without a prompt. /// /// If you need more explicit control over /// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. From 834af36aad80affe9fbc0041d477ba96c6115c32 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 17 Jul 2020 14:09:12 -0500 Subject: [PATCH 57/69] Update src/libstd/io/stdio.rs Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 632a08ce14073..56e03fc9b030f 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -941,7 +941,7 @@ pub fn input() -> Result { stdout().flush()?; let mut input = String::new(); match stdin().read_line(&mut input)? { - 0 => Err(Error::new(ErrorKind::UnexpectedEof, "input reached eof unexpectedly.")), + 0 => Err(Error::new(ErrorKind::UnexpectedEof, "input reached eof unexpectedly")), _ => Ok(String::from(input.trim_end_matches(&['\n', '\r'][..]))), } } From a028a0c77be670d52f65e01686ef13491393e86f Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 17 Jul 2020 14:10:50 -0500 Subject: [PATCH 58/69] Update stdio.rs doc fix --- src/libstd/io/stdio.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 56e03fc9b030f..640a61ca24441 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -909,12 +909,10 @@ pub fn prompt(prompt: &str) -> Result { /// /// Equivalent to [`prompt`] without a prompt. /// -/// If you need more explicit control over -/// locking, see the [`Stdin::lock`] and [`Stdout::lock`] methods. -/// /// # Note -/// -/// If you require more explicit control over capturing +/// For more explicit control over locking, see the +/// [`Stdin::lock`] and [`Stdout::lock`] methods. If +/// you require more explicit control over capturing /// user input, see the [`Stdin::read_line`] method. /// /// # Examples From 312668d536e31b211c535cac1b50df377de9129a Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 17 Jul 2020 17:41:15 -0500 Subject: [PATCH 59/69] Update stdio.rs Merged both functions --- src/libstd/io/stdio.rs | 50 ++++++------------------------------------ 1 file changed, 7 insertions(+), 43 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 640a61ca24441..a6cff8da6e736 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -874,11 +874,9 @@ impl fmt::Debug for StderrLock<'_> { } } -/// Prints `prompt` and reads a [`String`] from +/// Prints the given `str` and reads a [`String`] from /// [standard input](Stdin). The trailing newline is stripped. -/// Errors on EOF (Ctrl-D). -/// -/// Equivalent to [`input`] except it prints prompts. +/// Gives an error on EOF (Ctrl-D). /// /// # Note /// @@ -888,54 +886,20 @@ impl fmt::Debug for StderrLock<'_> { /// # Examples /// /// ```no_run -/// #![feature(prompt)] -/// use std::io::prompt; -/// -/// let name = prompt("Enter name: ").expect("input failed"); -/// println!("Your name is {}!", name); -/// ``` -#[unstable( - feature = "prompt", - reason = "this function may be replaced with a more general mechanism", - issue = "none" -)] -pub fn prompt(prompt: &str) -> Result { - stdout().write_all(prompt.as_bytes())?; - input() -} - -/// Reads a [`String`] from [standard input](Stdin). The -/// trailing newline is stripped. Errors on EOF (Ctrl-D). -/// -/// Equivalent to [`prompt`] without a prompt. -/// -/// # Note -/// For more explicit control over locking, see the -/// [`Stdin::lock`] and [`Stdout::lock`] methods. If -/// you require more explicit control over capturing -/// user input, see the [`Stdin::read_line`] method. -/// -/// # Examples -/// -/// ```no_run /// #![feature(input)] /// use std::io::input; /// -/// fn main() { -/// print!("Enter name: "); -/// -/// let name = input().expect("input failed!"); +/// let name = input("Enter name: ").expect("input failed"); /// -/// println!("Your name is {}!", name); -/// } +/// println!("Your name is {}!", name); /// ``` #[unstable( feature = "input", - reason = "this function may be replaced with a \ - more general mechanism", + reason = "this function may be replaced with a more general mechanism", issue = "none" )] -pub fn input() -> Result { +pub fn input(prompt: &str) -> Result { + stdout().write_all(prompt.as_bytes())?; stdout().flush()?; let mut input = String::new(); match stdin().read_line(&mut input)? { From 9a64f6e61f5b5f0d91b69d5c8fae802b1c3d3ebc Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 17 Jul 2020 17:41:44 -0500 Subject: [PATCH 60/69] Update mod.rs remove excess code --- src/libstd/io/mod.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 46c2f18a8cb7d..6a5d015723d6c 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -280,8 +280,6 @@ pub use self::cursor::Cursor; pub use self::error::{Error, ErrorKind, Result}; #[unstable(feature = "input", issue = "none")] pub use self::stdio::input; -#[unstable(feature = "prompt", issue = "none")] -pub use self::stdio::prompt; #[stable(feature = "rust1", since = "1.0.0")] pub use self::stdio::{stderr, stdin, stdout, Stderr, Stdin, Stdout}; #[stable(feature = "rust1", since = "1.0.0")] From 004d0db7c6550c71c5d4d76e2cac966d73ce9447 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Fri, 17 Jul 2020 17:42:18 -0500 Subject: [PATCH 61/69] Update lib.rs removed unused feature --- src/libstd/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index aaa2358b89b49..83db285e142bd 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -297,7 +297,6 @@ #![feature(panic_internals)] #![feature(panic_unwind)] #![feature(prelude_import)] -#![feature(prompt)] #![feature(ptr_internals)] #![feature(raw)] #![feature(raw_ref_macros)] From ea79d084ac5041ad4d151cf2f8a0740d768ed05c Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Mon, 20 Jul 2020 20:11:12 -0500 Subject: [PATCH 62/69] Update src/libstd/io/stdio.rs remove double locking Co-authored-by: Joshua Nelson --- src/libstd/io/stdio.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index a6cff8da6e736..e48a6b9e0446a 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -899,8 +899,9 @@ impl fmt::Debug for StderrLock<'_> { issue = "none" )] pub fn input(prompt: &str) -> Result { - stdout().write_all(prompt.as_bytes())?; - stdout().flush()?; + let mut stdout = stdout().lock(); + stdout.write_all(prompt.as_bytes())?; + stdout.flush()?; let mut input = String::new(); match stdin().read_line(&mut input)? { 0 => Err(Error::new(ErrorKind::UnexpectedEof, "input reached eof unexpectedly")), From ed8a44ab47a523735ff01f03cb470e2c10add3af Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Mon, 20 Jul 2020 20:15:18 -0500 Subject: [PATCH 63/69] Update stdio.rs docs fix --- src/libstd/io/stdio.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index e48a6b9e0446a..994b0989db2ee 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -889,9 +889,14 @@ impl fmt::Debug for StderrLock<'_> { /// #![feature(input)] /// use std::io::input; /// -/// let name = input("Enter name: ").expect("input failed"); +/// fn main() -> std::io::Result<()> { /// -/// println!("Your name is {}!", name); +/// let name = input("Enter name: ")?; +/// +/// println!("Your name is {}!", name); +/// +/// Ok(()) +/// } /// ``` #[unstable( feature = "input", From d87d86851efc13e93620d71e209b291fa1f8a563 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Mon, 20 Jul 2020 20:16:21 -0500 Subject: [PATCH 64/69] Update stdio.rs docs fix --- src/libstd/io/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 994b0989db2ee..c70612fb3d79a 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -876,7 +876,7 @@ impl fmt::Debug for StderrLock<'_> { /// Prints the given `str` and reads a [`String`] from /// [standard input](Stdin). The trailing newline is stripped. -/// Gives an error on EOF (Ctrl-D). +/// Gives an error on EOF (end of file). /// /// # Note /// From 898c99d30ce00bd60c4b3c9673e5178aa2c44550 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Mon, 20 Jul 2020 20:21:38 -0500 Subject: [PATCH 65/69] Update stdio.rs lock fix --- src/libstd/io/stdio.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index c70612fb3d79a..f0d01c34fc2d9 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -904,9 +904,8 @@ impl fmt::Debug for StderrLock<'_> { issue = "none" )] pub fn input(prompt: &str) -> Result { - let mut stdout = stdout().lock(); - stdout.write_all(prompt.as_bytes())?; - stdout.flush()?; + stdout().write_all(prompt.as_bytes())?; + stdout().flush()?; let mut input = String::new(); match stdin().read_line(&mut input)? { 0 => Err(Error::new(ErrorKind::UnexpectedEof, "input reached eof unexpectedly")), From 44d703552f0c20f94d993cea044fd1b5d17dce38 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Mon, 20 Jul 2020 20:59:45 -0500 Subject: [PATCH 66/69] Update stdio.rs fix double lock, again --- src/libstd/io/stdio.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index f0d01c34fc2d9..9934c582ed2d6 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -904,8 +904,10 @@ impl fmt::Debug for StderrLock<'_> { issue = "none" )] pub fn input(prompt: &str) -> Result { - stdout().write_all(prompt.as_bytes())?; - stdout().flush()?; + let stdout = stdout(); + let mut lock = stdout.lock(); + lock.write_all(prompt.as_bytes())?; + lock.flush()?; let mut input = String::new(); match stdin().read_line(&mut input)? { 0 => Err(Error::new(ErrorKind::UnexpectedEof, "input reached eof unexpectedly")), From e78837f8e0d0250094a69005ad11623f042d6238 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Mon, 20 Jul 2020 23:01:15 -0500 Subject: [PATCH 67/69] Update src/libstd/io/stdio.rs Co-authored-by: Ivan Tham --- src/libstd/io/stdio.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 9934c582ed2d6..472dbe892df13 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -890,7 +890,6 @@ impl fmt::Debug for StderrLock<'_> { /// use std::io::input; /// /// fn main() -> std::io::Result<()> { -/// /// let name = input("Enter name: ")?; /// /// println!("Your name is {}!", name); From 28ebe131460073bfa2eea8c1114a0250d2d75c75 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Wed, 22 Jul 2020 20:59:21 -0500 Subject: [PATCH 68/69] Update stdio.rs docs fix --- src/libstd/io/stdio.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 472dbe892df13..2615b23c0d092 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -887,10 +887,10 @@ impl fmt::Debug for StderrLock<'_> { /// /// ```no_run /// #![feature(input)] -/// use std::io::input; +/// use std::io; /// -/// fn main() -> std::io::Result<()> { -/// let name = input("Enter name: ")?; +/// fn main() -> Result<()> { +/// let name = io::input("Enter name: ")?; /// /// println!("Your name is {}!", name); /// From 7cb9a0509934801c1e2869f1ead93257e3fc9762 Mon Sep 17 00:00:00 2001 From: sHaDoW Date: Wed, 22 Jul 2020 21:29:34 -0500 Subject: [PATCH 69/69] Update stdio.rs docs stuff --- src/libstd/io/stdio.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 2615b23c0d092..3a3127dba5594 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -889,12 +889,10 @@ impl fmt::Debug for StderrLock<'_> { /// #![feature(input)] /// use std::io; /// -/// fn main() -> Result<()> { -/// let name = io::input("Enter name: ")?; +/// fn main() { +/// let name = io::input("Enter name: ").expect("input failed!"); /// /// println!("Your name is {}!", name); -/// -/// Ok(()) /// } /// ``` #[unstable(