1 use std::{
2     fs::File,
3     io,
4     thread,
5     time::Duration,
6 };
7 
8 use futures_util::StreamExt;
9 use inotify::{
10     Inotify,
11     WatchMask,
12 };
13 use tempfile::TempDir;
14 
15 #[tokio::main]
main() -> Result<(), io::Error>16 async fn main() -> Result<(), io::Error> {
17     let inotify = Inotify::init()
18         .expect("Failed to initialize inotify");
19 
20     let dir = TempDir::new()?;
21 
22     inotify.watches().add(dir.path(), WatchMask::CREATE | WatchMask::MODIFY)?;
23 
24     thread::spawn::<_, Result<(), io::Error>>(move || {
25         loop {
26             File::create(dir.path().join("file"))?;
27             thread::sleep(Duration::from_millis(500));
28         }
29     });
30 
31     let mut buffer = [0; 1024];
32     let mut stream = inotify.into_event_stream(&mut buffer)?;
33 
34     while let Some(event_or_error) = stream.next().await {
35         println!("event: {:?}", event_or_error?);
36     }
37 
38     Ok(())
39 }
40