Skip to content

Commit

Permalink
Add RecommendedWatcher type to select best impl
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Dec 20, 2014
1 parent b24a533 commit a9c60eb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
16 changes: 14 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ name = "notify"
version = "1.0.0"
authors = ["Fe虂lix Saparelli <felix@passcod.name>"]

description = "Cross-platform filesystem notification library"
homepage = "https://github.com/passcod/rsnotify"
repository = "https://github.com/passcod/rsnotify"

readme = "README.md"
license = "CC0-1.0"
keywords = [
"events",
"filesystem",
"notify",
"watch",
]

[dependencies]
time = "*"
#inotify = "*"

[dependencies.inotify]
[target.x86_64-unknown-linux-gnu.dependencies.inotify]
git = "https://github.com/hannobraun/inotify-rs.git"
46 changes: 26 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ _Cross-platform filesystem notification library for Rust._
## Install

```toml
[dependencies.notify]
git = "https://github.com/passcod/rsnotify.git"
[dependencies]
notify = "^1.0"
```

## Usage

```rust
extern crate notify;

// Create a channel to receive the events
let (tx, rx) = channel();

// Select the recommended implementation for this platform
match notify::new(tx) {
Ok(watcher) => {
// Watch files!
watcher.watch(Path::new("/path/to/foo"));

// Receive events!
println!("{}", rx.recv());
},
Err(e) => println!("Uh oh: {}", e)
use notify::{RecommendedWatcher, Error, Watcher};

fn main() {
let (tx, rx) = channel();
let mut w: Result<RecommendedWatcher, Error> = Watcher::new(tx);
match w {
Ok(mut watcher) => {
watcher.watch(&Path::new("/home/test/notify"));
match rx.recv() {
_ => println!("Recv.")
}
},
Err(e) => println!("Error")
}
}
```

## Platforms

- Linux / Android: inotify
- All platforms: polling
- All platforms: polling (only `op::WRITE`)

### Todo

Expand All @@ -42,12 +43,17 @@ match notify::new(tx) {
- BSD / OS X / iOS: kqueue
- Solaris 11: FEN

### Tests
## Known Bugs

Nothing is tested yet.
- inotify backend panics when dropped
- polling backend only handles `op::WRITE`s
- see `TODO` comments in the code for more

## Origins

Inspired by Go's [fsnotify](https://github.com/go-fsnotify/fsnotify), born out of need for [cargo watch](https://github.com/passcod/cargo-watch), and general frustration at the non-existence of C/Rust cross-platform notify libraries.
Inspired by Go's [fsnotify](https://github.com/go-fsnotify/fsnotify), born out
of need for [cargo watch](https://github.com/passcod/cargo-watch), and general
frustration at the non-existence of C/Rust cross-platform notify libraries.

Written from scratch by [F茅lix Saparelli](https://passcod.name), and released in the Public Domain.
Written from scratch by [F茅lix Saparelli](https://passcod.name), and released
in the Public Domain.
19 changes: 13 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ pub trait Watcher {
fn unwatch(&mut self, &Path) -> Result<(), Error>;
}

#[cfg(target_os = "linux")]
pub fn new(tx: Sender<Event>) -> Result<INotifyWatcher, Error> {
Watcher::new(tx)
}
#[cfg(target_os = "linux")] pub type RecommendedWatcher = INotifyWatcher;
#[cfg(not(any(target_os = "linux")))] pub type RecommendedWatcher = PollWatcher;

#[cfg(not(any(target_os = "linux")))]
pub fn new(tx: Sender<Event>) -> Result<PollWatcher, Error> {
pub fn new(tx: Sender<Event>) -> Result<RecommendedWatcher, Error> {
Watcher::new(tx)
}

Expand All @@ -71,4 +68,14 @@ fn new_poll() {
Ok(_) => assert!(true),
Err(_) => assert!(false)
}
}

#[test]
fn new_recommended() {
let (tx, rx) = channel();
let w: Result<RecommendedWatcher, Error> = Watcher::new(tx);
match w {
Ok(_) => assert!(true),
Err(_) => assert!(false)
}
}

0 comments on commit a9c60eb

Please sign in to comment.