iof/stdio.rs
1//! Standard input/output streams.
2//!
3//! This module provides utilities for reading from standard input and writing to standard output.
4//!
5//! Calling functions in this module will lock the standard input/output streams, so it is not
6//! recommended to use this module in a multi-threaded environment.
7use crate::InputStream;
8use std::{
9 io::{self, BufReader, Stdin},
10 sync::{LazyLock, Mutex, MutexGuard},
11};
12
13pub(crate) mod read_into;
14pub(crate) mod stream;
15
16/// Standard input stream.
17pub(crate) static STDIN: LazyLock<Mutex<InputStream<BufReader<Stdin>>>> =
18 LazyLock::new(|| Mutex::new(InputStream::new(BufReader::new(io::stdin()))));
19
20/// Get an exclusive handle to the standard input stream.
21///
22/// See [io::stdin] and [io::Stdin::lock] for more information.
23///
24/// # Panics
25///
26/// This function will panic if the standard input stream is already locked by current thread,
27/// or if the standard input stream is poisoned.
28#[inline]
29pub fn stdin() -> MutexGuard<'static, InputStream<BufReader<Stdin>>> {
30 STDIN.lock().unwrap()
31}
32
33/// Get an exclusive handle to the standard output stream.
34///
35/// See [io::stdout] and [io::Stdout::lock] for more information.
36#[inline]
37pub fn stdout() -> io::StdoutLock<'static> {
38 io::stdout().lock()
39}