bevy/crates/bevy_asset/src/filesystem_watcher.rs
Joshua J. Bouw 9f4c8b1b9a
Fix errors and panics to typical Rust conventions (#968)
Fix errors and panics to typical Rust conventions
2020-12-02 11:31:16 -08:00

26 lines
903 B
Rust

use crossbeam_channel::Receiver;
use notify::{Event, RecommendedWatcher, RecursiveMode, Result, Watcher};
use std::path::Path;
/// Watches for changes to assets on the filesystem. This is used by the `AssetServer` to reload them
pub struct FilesystemWatcher {
pub watcher: RecommendedWatcher,
pub receiver: Receiver<Result<Event>>,
}
impl Default for FilesystemWatcher {
fn default() -> Self {
let (sender, receiver) = crossbeam_channel::unbounded();
let watcher: RecommendedWatcher = Watcher::new_immediate(move |res| {
sender.send(res).expect("Watch event send failure.");
})
.expect("Failed to create filesystem watcher.");
FilesystemWatcher { watcher, receiver }
}
}
impl FilesystemWatcher {
pub fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
self.watcher.watch(path, RecursiveMode::Recursive)
}
}