-
Notifications
You must be signed in to change notification settings - Fork 18
/
async.rs
44 lines (31 loc) · 1004 Bytes
/
async.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
43
44
extern crate linefeed;
use std::io;
use std::time::{Duration, Instant};
use linefeed::{Interface, ReadResult};
// Per-step timeout
const TIMEOUT: Duration = Duration::from_millis(500);
// Total time limit
const TIME_LIMIT: Duration = Duration::from_secs(5);
fn main() -> io::Result<()> {
let interface = Interface::new("async-demo")?;
interface.set_prompt("async-demo> ")?;
println!("This is a demo of linefeed's asynchronous operation.");
println!("This demo will terminate in {} seconds.", TIME_LIMIT.as_secs());
println!();
let start = Instant::now();
loop {
if let Some(res) = interface.read_line_step(Some(TIMEOUT))? {
println!("read input: {:?}", res);
if let ReadResult::Eof = res {
break;
}
}
if start.elapsed() >= TIME_LIMIT {
interface.cancel_read_line()?;
println!();
println!("Time's up!");
break;
}
}
Ok(())
}