diff options
| author | Taylor Yu <tlyu@mit.edu> | 2021-06-17 13:39:13 -0500 |
|---|---|---|
| committer | Taylor Yu <tlyu@mit.edu> | 2021-07-12 23:43:42 -0500 |
| commit | 339ce4fee86ede86d6cdde514faf6f62aed16027 (patch) | |
| tree | 0bab7eb7884a5b712acf962e25e8958eb3f9fa52 | |
| parent | 14c0c3e55d311dd47aad792ef316bd5dde778886 (diff) | |
| download | rust-339ce4fee86ede86d6cdde514faf6f62aed16027.tar.gz rust-339ce4fee86ede86d6cdde514faf6f62aed16027.zip | |
add Stdin::lines, Stdin::split forwarder methods
Add forwarder methods `Stdin::lines` and `Stdin::split`, which consume and lock a `Stdin` handle, and forward on to the corresponding `BufRead` methods. This should make it easier for beginners to use those iterator constructors without explicitly dealing with locks or lifetimes.
| -rw-r--r-- | library/std/src/io/stdio.rs | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 206687e38fb..65ad7d88e22 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -7,7 +7,7 @@ use crate::io::prelude::*; use crate::cell::{Cell, RefCell}; use crate::fmt; -use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter}; +use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter, Lines, Split}; use crate::lazy::SyncOnceCell; use crate::pin::Pin; use crate::sync::atomic::{AtomicBool, Ordering}; @@ -446,6 +446,49 @@ impl Stdin { pub fn into_locked(self) -> StdinLock<'static> { self.lock_any() } + + /// Consumes this handle and returns an iterator over input lines. + /// + /// For detailed semantics of this method, see the documentation on + /// [`BufRead::lines`]. + /// + /// # Examples + /// + /// ```no_run + /// #![feature(stdin_forwarders)] + /// use std::io; + /// + /// let lines = io::stdin().lines(); + /// for line in lines { + /// println!("got a line: {}", line.unwrap()); + /// } + /// ``` + #[unstable(feature = "stdin_forwarders", issue = "87096")] + pub fn lines(self) -> Lines<StdinLock<'static>> { + self.into_locked().lines() + } + + /// Consumes this handle and returns an iterator over input bytes, + /// split at the specified byte value. + /// + /// For detailed semantics of this method, see the documentation on + /// [`BufRead::split`]. + /// + /// # Examples + /// + /// ```no_run + /// #![feature(stdin_forwarders)] + /// use std::io; + /// + /// let splits = io::stdin().split(b'-'); + /// for split in splits { + /// println!("got a chunk: {}", String::from_utf8_lossy(&split.unwrap())); + /// } + /// ``` + #[unstable(feature = "stdin_forwarders", issue = "87096")] + pub fn split(self, byte: u8) -> Split<StdinLock<'static>> { + self.into_locked().split(byte) + } } #[stable(feature = "std_debug", since = "1.16.0")] |
