Skip to content

Commit

Permalink
Auto merge of rust-lang#86847 - tlyu:stdin-forwarders, r=joshtriplett
Browse files Browse the repository at this point in the history
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.

Replaces rust-lang#86412.
~~Based on rust-lang#86846 to get the tracking issue number for the `stdio_locked` feature.~~ Rebased after merge, so it's only one commit now.

r? `@joshtriplett`
`@rustbot` label +A-io +C-enhancement +D-newcomer-roadblock +T-libs-api
  • Loading branch information
bors committed Jul 21, 2021
2 parents ac575b6 + 339ce4f commit cf932aa
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit cf932aa

Please sign in to comment.