Skip to content

Commit 5fe65ea

Browse files
authored
Use Option type (#5)
1 parent 7cb2f79 commit 5fe65ea

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "stdin-nonblocking"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["Jeremy Harris <[email protected]>"]
55
description = "Dependency-less non-blocking stdin reader using background threads. Supports streaming and immediate fallback defaults."
66
repository = "https://github.com/jzombie/rust-stdin-nonblocking"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use stdin_nonblocking::get_stdin_or_default;
3434
let input = get_stdin_or_default(Some(b"fallback_value"));
3535

3636
// Input is always `Vec<u8>`, ensuring binary safety.
37-
assert_eq!(input, b"fallback_value".to_vec());
37+
assert_eq!(input, Some(b"fallback_value".to_vec()));
3838
```
3939

4040
### Read `stdin` as Stream

src/bin/test_binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn main() {
77

88
// Print raw binary data instead of Debug format
99
io::stdout()
10-
.write_all(&input)
10+
.write_all(&input.unwrap())
1111
.expect("Failed to write output");
1212
}

src/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(doctest)]
2+
doc_comment::doctest!("../README.md");
3+
14
use std::io::{self, IsTerminal, Read};
25
use std::sync::mpsc::{self, Receiver, Sender};
36
use std::thread;
@@ -77,26 +80,29 @@ pub fn spawn_stdin_stream() -> Receiver<Vec<u8>> {
7780
/// * `default` - An optional fallback value returned if no input is available.
7881
///
7982
/// # Returns
80-
/// * `Vec<u8>` - The full stdin input (or default value as bytes).
83+
/// * `Option<Vec<u8>>` - The full stdin input (or default value as bytes).
8184
///
8285
/// # Example
8386
/// ```
8487
/// use stdin_nonblocking::get_stdin_or_default;
8588
///
8689
/// let input = get_stdin_or_default(Some(b"fallback_value"));
8790
///
88-
/// assert_eq!(input, b"fallback_value".to_vec());
91+
/// assert_eq!(input, Some(b"fallback_value".to_vec()));
8992
/// ```
90-
pub fn get_stdin_or_default(default: Option<&[u8]>) -> Vec<u8> {
93+
pub fn get_stdin_or_default(default: Option<&[u8]>) -> Option<Vec<u8>> {
9194
let stdin_channel = spawn_stdin_stream();
9295

9396
// Give the reader thread a short time to capture any available input
9497
thread::sleep(Duration::from_millis(50));
9598

9699
if let Ok(data) = stdin_channel.try_recv() {
97-
return data;
100+
return Some(data);
98101
}
99102

103+
// Return `None` if no default
104+
default?;
105+
100106
// No input available, return the default value
101-
default.unwrap_or(b"").to_vec()
107+
Some(default.unwrap_or(b"").to_vec())
102108
}

0 commit comments

Comments
 (0)