Skip to content

Commit cf932aa

Browse files
committed
Auto merge of #86847 - tlyu:stdin-forwarders, r=joshtriplett
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 #86412. ~~Based on #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
2 parents ac575b6 + 339ce4f commit cf932aa

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

library/std/src/io/stdio.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::io::prelude::*;
77

88
use crate::cell::{Cell, RefCell};
99
use crate::fmt;
10-
use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter};
10+
use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter, Lines, Split};
1111
use crate::lazy::SyncOnceCell;
1212
use crate::pin::Pin;
1313
use crate::sync::atomic::{AtomicBool, Ordering};
@@ -446,6 +446,49 @@ impl Stdin {
446446
pub fn into_locked(self) -> StdinLock<'static> {
447447
self.lock_any()
448448
}
449+
450+
/// Consumes this handle and returns an iterator over input lines.
451+
///
452+
/// For detailed semantics of this method, see the documentation on
453+
/// [`BufRead::lines`].
454+
///
455+
/// # Examples
456+
///
457+
/// ```no_run
458+
/// #![feature(stdin_forwarders)]
459+
/// use std::io;
460+
///
461+
/// let lines = io::stdin().lines();
462+
/// for line in lines {
463+
/// println!("got a line: {}", line.unwrap());
464+
/// }
465+
/// ```
466+
#[unstable(feature = "stdin_forwarders", issue = "87096")]
467+
pub fn lines(self) -> Lines<StdinLock<'static>> {
468+
self.into_locked().lines()
469+
}
470+
471+
/// Consumes this handle and returns an iterator over input bytes,
472+
/// split at the specified byte value.
473+
///
474+
/// For detailed semantics of this method, see the documentation on
475+
/// [`BufRead::split`].
476+
///
477+
/// # Examples
478+
///
479+
/// ```no_run
480+
/// #![feature(stdin_forwarders)]
481+
/// use std::io;
482+
///
483+
/// let splits = io::stdin().split(b'-');
484+
/// for split in splits {
485+
/// println!("got a chunk: {}", String::from_utf8_lossy(&split.unwrap()));
486+
/// }
487+
/// ```
488+
#[unstable(feature = "stdin_forwarders", issue = "87096")]
489+
pub fn split(self, byte: u8) -> Split<StdinLock<'static>> {
490+
self.into_locked().split(byte)
491+
}
449492
}
450493

451494
#[stable(feature = "std_debug", since = "1.16.0")]

0 commit comments

Comments
 (0)