-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
show!
macro; update trait bound about writting; update tests.
- Loading branch information
1 parent
51c9a35
commit 4bf54d0
Showing
15 changed files
with
175 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use iof::read; | ||
|
||
/// Some examples of reading from standard input using the `read` function. | ||
fn main() { | ||
// Read a single integer from input. | ||
let n: u32 = read(); | ||
assert_eq!(n, 42); | ||
|
||
// Read a string from input. | ||
let s: String = read(); | ||
assert_eq!(s, "Hello!"); | ||
|
||
// Read an array of integers from input. | ||
let arr: [u32; 4] = read(); | ||
assert_eq!(arr, [1, 2, 3, 4]); | ||
|
||
// Read a nested array of integers from input. | ||
let arr: [[u32; 2]; 2] = read(); | ||
assert_eq!(arr, [[1, 2], [3, 4]]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
42 | ||
Hello! | ||
1 2 3 4 | ||
1 2 | ||
3 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ Hello, World! | |
.@/ | ||
#$$ | ||
1 2 3 | ||
1 2 3 4 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use super::WriteInto; | ||
use crate::stdout; | ||
use std::io::{self, Write}; | ||
|
||
/// Configuration for the writer. | ||
pub struct Writer<'sep, 'end, 'buf> { | ||
index: usize, | ||
sep: &'sep str, | ||
end: &'end str, | ||
buf: Option<&'buf mut dyn Write>, | ||
} | ||
|
||
impl<'sep, 'end, 'buf> Writer<'sep, 'end, 'buf> { | ||
/// Create a new writer. | ||
pub fn new() -> Self { | ||
let sep = " "; | ||
let end = "\n"; | ||
let buf = None; | ||
Self { | ||
index: 0, | ||
sep, | ||
end, | ||
buf, | ||
} | ||
} | ||
/// Set the separator. | ||
pub fn sep(&mut self, sep: &'sep str) -> &mut Self { | ||
self.sep = sep; | ||
self | ||
} | ||
/// Set the end. | ||
pub fn end(&mut self, end: &'end str) -> &mut Self { | ||
self.end = end; | ||
self | ||
} | ||
/// Set the buffer. | ||
pub fn buf(&mut self, buf: &'buf mut impl Write) -> &mut Self { | ||
self.buf = Some(buf); | ||
self | ||
} | ||
/// Write a value. | ||
pub fn write<V: WriteInto>(&mut self, value: &V) -> io::Result<&mut Self> { | ||
match self.buf.as_mut() { | ||
Some(buf) => { | ||
if self.index > 0 { | ||
self.sep.try_write_into(buf)?; | ||
} | ||
value.try_write_into(buf)?; | ||
} | ||
None => { | ||
let buf = &mut stdout(); | ||
if self.index > 0 { | ||
self.sep.try_write_into(buf)?; | ||
} | ||
value.try_write_into(buf)?; | ||
} | ||
} | ||
self.index += 1; | ||
Ok(self) | ||
} | ||
/// Finish writing. | ||
pub fn finish(&mut self) -> io::Result<()> { | ||
if let Some(buf) = self.buf.as_mut() { | ||
self.end.try_write_into(buf) | ||
} else { | ||
let buf = &mut stdout(); | ||
self.end.try_write_into(buf) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.