-
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.
Refactored read_into and write_into; test examples in documentation.
- Loading branch information
1 parent
5895885
commit 1c727bf
Showing
43 changed files
with
1,490 additions
and
1,034 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
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,15 @@ | ||
use iof::{get_line, read}; | ||
|
||
fn main() { | ||
// Read a single string from input. | ||
let s: String = read!(); | ||
assert_eq!(s, "42"); | ||
|
||
// Read a line of string from input. | ||
let s: String = get_line(); | ||
assert_eq!(s, ""); | ||
|
||
// Read a line of string from input. | ||
let s: String = get_line(); | ||
assert_eq!(s, "abc"); | ||
} |
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,2 @@ | ||
42 | ||
abc |
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,11 @@ | ||
use iof::{get_line_some, read}; | ||
|
||
fn main() { | ||
// Read a single string from input. | ||
let s: String = read!(); | ||
assert_eq!(s, "42"); | ||
|
||
// Read a non-empty line of string from input. | ||
let s: String = get_line_some(); | ||
assert_eq!(s, "abc"); | ||
} |
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,2 @@ | ||
42 | ||
abc |
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,44 @@ | ||
use iof::{read, Mat}; | ||
|
||
fn main() { | ||
// Read a single integer from input. | ||
let n: u32 = read!(); | ||
assert_eq!(n, 42); | ||
|
||
// Read a single string from input. | ||
let n: String = read!(); | ||
assert_eq!(n, "abc"); | ||
|
||
// Read a vector of characters from input. | ||
let v: Vec<char> = read!(); | ||
assert_eq!(v, ['d', 'e', 'f']); | ||
|
||
// Read a tuple from input. | ||
// Equivalent to: | ||
// let l: u32 = read!(); | ||
// let m: f64 = read!(); | ||
// let n: String = read!(); | ||
let (l, m, n): (u32, f64, String) = read!(); | ||
assert_eq!(l, 0); | ||
assert_eq!(m, 0.3); | ||
assert_eq!(n, "lmn"); | ||
|
||
// Read a vector of integers from input. | ||
let v: Vec<u32> = read!(3); | ||
assert_eq!(v, [1, 2, 3]); | ||
|
||
// Read a matrix of integers from input. | ||
let m: Mat<u32> = read!(2, 3); | ||
assert_eq!(m, [[1, 2, 3], [4, 5, 6]]); | ||
|
||
// Read a matrix of characters from input. | ||
let m: Mat<char> = read!(3, 5); | ||
assert_eq!( | ||
m, | ||
[ | ||
['.', '@', '/', '#', '$'], | ||
['!', '@', '#', '!', '@'], | ||
['*', '&', '@', ':', ','] | ||
] | ||
); | ||
} |
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,8 @@ | ||
42 abc def | ||
0 0.3 lmn | ||
1 2 3 | ||
1 2 3 | ||
4 5 6 | ||
.@/#$ | ||
!@#!@ | ||
*&@:, |
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,41 @@ | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
|
||
static COUNT: AtomicUsize = AtomicUsize::new(0); | ||
pub struct Tracked(Box<(usize, usize, usize)>); | ||
impl Tracked { | ||
pub(super) fn new(i: usize, j: usize) -> Self { | ||
loop { | ||
let n = COUNT.load(Ordering::Relaxed); | ||
if COUNT | ||
.compare_exchange(n, n + 1, Ordering::Relaxed, Ordering::Relaxed) | ||
.is_ok() | ||
{ | ||
// Well, we shouldn't use println! without a lock in real code. | ||
println!("Creating Tracked({n}) for ({i}, {j})"); | ||
return Tracked(Box::new((n, i, j))); | ||
} | ||
} | ||
} | ||
} | ||
impl Drop for Tracked { | ||
fn drop(&mut self) { | ||
loop { | ||
let n = COUNT.load(Ordering::Relaxed); | ||
assert!(n > 0); | ||
if COUNT | ||
.compare_exchange(n, n - 1, Ordering::Relaxed, Ordering::Relaxed) | ||
.is_ok() | ||
{ | ||
break; | ||
} | ||
} | ||
let (n, i, j) = *self.0; | ||
// Well, we shouldn't use println! without a lock in real code. | ||
println!("Dropping Tracked({n}) for ({i}, {j})"); | ||
} | ||
} | ||
|
||
/// Ensure all elements are dropped. | ||
pub(super) fn check() { | ||
assert_eq!(COUNT.load(Ordering::Relaxed), 0); | ||
} |
Oops, something went wrong.