Skip to content

Commit

Permalink
feat: expose user-friendly version of read_value!
Browse files Browse the repository at this point in the history
  • Loading branch information
statiolake committed Apr 29, 2024
1 parent cedb4ec commit e6a053b
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions proconio/src/lib.rs
Expand Up @@ -682,18 +682,18 @@ macro_rules! input {
};
}

/// read input from stdin interactively.
/// Read input from specified source interactively.
///
/// this macro is alias of:
/// This macro is an alias of:
/// ```text
/// let source = procontio::source::line::LineSource::new(BufReader::new(std::io::stdin()))
/// let source = procontio::source::line::LineSource::new(BufReader::new(std::io::stdin()));
/// input! {
/// from &mut source,
/// (mut) variable: type,
/// ...
/// }
/// ```
/// read the documet of [input!](input) for further information.
/// read the document of [input!](input) for further information.
#[macro_export]
macro_rules! input_interactive {
($($rest:tt)*) => {
Expand Down Expand Up @@ -782,6 +782,38 @@ macro_rules! read_value {
};
}

/// Interactive version of `read_value!` macro.
///
/// This macro is equivalent to do the following:
///
/// ```text
/// let source = procontio::source::line::LineSource::new(BufReader::new(std::io::stdin()));
/// let variable = read_value!(from &mut source, type);
/// ```
///
/// Read the document of [read_value!](read_value) for further information.
#[macro_export]
macro_rules! read_value_interactive {
($($rest:tt)*) => {
let mut locked_stdin = $crate::STDIN_SOURCE
.get_or_init(|| {
std::sync::Mutex::new($crate::StdinSource::Interactive(
$crate::source::line::LineSource::new(std::io::BufReader::new(std::io::stdin())),
))
})
.lock()
.expect(concat!(
"failed to lock the stdin; please re-run this program. ",
"If this issue repeatedly occur, this is a bug in `proconio`. ",
"Please report this issue from ",
"<https://github.com/statiolake/proconio-rs/issues>."
));
let __res = $crate::read_value!(from &mut *locked_stdin, $($rest)*);
drop(locked_stdin); // release the lock
__res
};
}

/// Checks if some of tokens are left on stdin.
///
/// This is useful when the number of test cases is not specified like ICPC problems.
Expand Down

0 comments on commit e6a053b

Please sign in to comment.