-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathread_file.rs
42 lines (32 loc) · 1.22 KB
/
read_file.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::{env, io, str};
use a10::fs::OpenOptions;
use a10::{AsyncFd, Ring, SubmissionQueue};
mod runtime;
fn main() -> io::Result<()> {
// Create a new I/O uring.
let mut ring = Ring::new(1)?;
let path = env::args()
.nth(1)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "missing argument"))?;
// Create our future that reads the file.
let read_file_future = read_file(ring.submission_queue().clone(), path);
// Use our fake runtime to poll the future, this basically polls the future
// and the `a10::Ring` in a loop.
let data = runtime::block_on(&mut ring, read_file_future)?;
// We'll print the response (using ol' fashioned blocking I/O).
let data = str::from_utf8(&data).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("file doesn't contain UTF-8: {err}"),
)
})?;
println!("{data}");
Ok(())
}
async fn read_file(sq: SubmissionQueue, path: String) -> io::Result<Vec<u8>> {
// Open a file for reading.
let file: AsyncFd = OpenOptions::new().open(sq, path.into()).await?;
// Read some bytes from the file.
let buf = file.read(Vec::with_capacity(32 * 1024)).await?;
Ok(buf)
}