Skip to content

Commit a65700d

Browse files
committed
test: add std and tokio tests
Also fixed examples.
1 parent 6aeb68f commit a65700d

File tree

5 files changed

+571
-31
lines changed

5 files changed

+571
-31
lines changed

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,37 @@ The loop is the glue between coroutines and runtimes. It makes the coroutine pro
2525
### Read a directory synchronously
2626

2727
```rust,ignore
28-
use io_fs::{coroutines::ReadDir, runtimes::std::handle};
28+
use std::{path::PathBuf};
2929
30-
let mut output = None;
30+
use io_fs::{coroutines::read_dir::ReadDir, error::FsResult, runtimes::std::handle};
31+
32+
let mut arg = None;
3133
let mut coroutine = ReadDir::new("/tmp");
3234
33-
let entries = loop {
34-
match coroutine.resume(output) {
35-
Ok(entries) => break entries,
36-
Err(io) => output = Some(handle(io).unwrap()),
35+
let paths = loop {
36+
match coroutine.resume(arg) {
37+
FsResult::Ok(paths) => break paths,
38+
FsResult::Err(err) => panic!("{err}"),
39+
FsResult::Io(io) => arg = Some(handle(io).unwrap()),
3740
}
3841
};
3942
40-
println!("Entries inside /tmp:");
43+
println!("Entries inside {}:", path.display());
4144
4245
for path in paths {
4346
println!(" - {}", path.display());
4447
}
4548
```
4649

47-
*See complete example at [./examples/std-read-dir.rs](https://github.com/pimalaya/io-fs/blob/master/examples/std-read-dir.rs).*
50+
*See complete examples at [./examples](https://github.com/pimalaya/io-fs/blob/master/examples).*
4851

49-
### More examples
52+
### Real-world examples
5053

5154
Have a look at projects built on the top of this library:
5255

53-
- [io-vdir](https://github.com/pimalaya/io-vdir): Set of I/O-free Rust coroutines and runtimes to manage [Vdir](https://vdirsyncer.pimutils.org/en/stable/vdir.html) filesystems.
56+
- [io-vdir](https://github.com/pimalaya/io-vdir): Set of I/O-free Rust coroutines to manage [Vdir](https://vdirsyncer.pimutils.org/en/stable/vdir.html) filesystems.
57+
- [io-addressbook](https://github.com/pimalaya/io-addressbook): Set of I/O-free Rust coroutines and clients to manage contacts.
58+
- [Cardamum](https://github.com/pimalaya/cardamum): CLI to manage contacts.
5459

5560
## Sponsoring
5661

examples/std-read-dir.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
#![cfg(feature = "std")]
22

3-
use std::io::{stdin, stdout, Write as _};
3+
use std::{
4+
env,
5+
io::{stdin, stdout, Write as _},
6+
path::PathBuf,
7+
};
48

5-
use io_fs::{coroutines::ReadDir, runtimes::std::handle};
9+
use io_fs::{coroutines::read_dir::ReadDir, error::FsResult, runtimes::std::handle};
610

711
fn main() {
8-
env_logger::init();
12+
let _ = env_logger::try_init();
913

10-
let path = read_line("Which directory to read?");
14+
let path: PathBuf = match env::var("DIR") {
15+
Ok(dir) => dir.into(),
16+
Err(_) => read_line("Directory to read?").into(),
17+
};
1118

12-
let mut output = None;
19+
let mut arg = None;
1320
let mut coroutine = ReadDir::new(&path);
1421

1522
let paths = loop {
16-
match coroutine.resume(output) {
17-
Ok(paths) => break paths,
18-
Err(io) => output = Some(handle(io).unwrap()),
23+
match coroutine.resume(arg) {
24+
FsResult::Ok(paths) => break paths,
25+
FsResult::Err(err) => panic!("{err}"),
26+
FsResult::Io(io) => arg = Some(handle(io).unwrap()),
1927
}
2028
};
2129

22-
println!("Entries inside {path}:");
30+
println!("Entries inside {}:", path.display());
2331

2432
for path in paths {
2533
println!(" - {}", path.display());

examples/tokio-create-many-files.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
#![cfg(feature = "tokio")]
22

33
use std::{
4+
env,
45
io::{stdin, stdout, Write as _},
56
time::Instant,
67
};
78

8-
use io_fs::{coroutines::CreateFiles, runtimes::tokio::handle};
9+
use io_fs::{coroutines::create_files::CreateFiles, error::FsResult, runtimes::tokio::handle};
910
use tempdir::TempDir;
1011

1112
#[tokio::main]
1213
async fn main() {
13-
env_logger::init();
14+
let _ = env_logger::try_init();
1415

15-
let tmp = TempDir::new("tokio-create-files").unwrap();
16+
let tmp = TempDir::new("tokio-create-many-files").unwrap();
1617

17-
let n = read_line("How many temp files to create?")
18-
.parse::<usize>()
19-
.unwrap();
18+
let n: usize = match env::var("N") {
19+
Ok(n) => n.parse().unwrap(),
20+
Err(_) => read_line("How many temp files to create?").parse().unwrap(),
21+
};
2022

2123
let start = Instant::now();
2224

23-
let mut output = None;
24-
let mut coroutine = CreateFiles::new(
25-
(0..n).map(|n| (tmp.path().join(n.to_string()), b"Hello, world!".to_vec())),
26-
);
25+
let mut arg = None;
26+
let mut coroutine =
27+
CreateFiles::new((0..n).map(|n| (tmp.path().join(n.to_string()), *b"Hello, world!")));
2728

28-
while let Err(io) = coroutine.resume(output) {
29-
output = Some(handle(io).await.unwrap());
29+
loop {
30+
match coroutine.resume(arg) {
31+
FsResult::Ok(()) => break,
32+
FsResult::Err(err) => panic!("{err}"),
33+
FsResult::Io(io) => arg = Some(handle(io).await.unwrap()),
34+
}
3035
}
3136

3237
let duration = start.elapsed();

0 commit comments

Comments
 (0)