[][src]Struct fwatch::Watcher

pub struct Watcher<W: Watchable> { /* fields omitted */ }

A watcher instance.

An instance of Watcher keeps track of a vector of watchables and their corresponding states.

Methods

impl<W: Watchable> Watcher<W>[src]

pub fn new() -> Self[src]

Create a new watcher instance.

Examples

use fwatch::{BasicTarget, Watcher};

fn main() {
    let mut watcher: Watcher<BasicTarget> = Watcher::new();
}

pub fn add_target(&mut self, target: W)[src]

Adds a target to the watcher.

Examples

use fwatch::{BasicTarget, Watcher};

fn main() {
    let mut watcher : Watcher<BasicTarget> = Watcher::new();

    // Watch the "foo.txt" path
    watcher.add_target(BasicTarget::new("foo.txt"));
}

pub fn remove_target(&mut self, index: usize) -> bool[src]

Remove a target from the watcher.

This function will panic if index is greater than the size of self.states() / self.targets().

Examples

use fwatch::{BasicTarget, Watcher};

fn main() {
    let mut watcher : Watcher<BasicTarget> = Watcher::new();

    // Inserts "foo.txt" at index 0
    watcher.add_target(BasicTarget::new("foo.txt"));

    // Remove "foo.txt" from the watcher
    assert!(watcher.remove_target(0));
}

pub fn get_state(&self, index: usize) -> Option<&WatchState>[src]

Attempt to get the state corresponding to the given target index.

Note that this doesn't update the current state.

Examples

use fwatch::{BasicTarget, Watcher, WatchState};

fn main() {
    let mut watcher : Watcher<BasicTarget> = Watcher::new();

    // Watch a file that doesn't exist
    watcher.add_target(BasicTarget::new("does_not_exist.txt"));
    assert_eq!(watcher.get_state(0).unwrap(), &WatchState::DoesNotExist);

    // Watch a file that does exist
    watcher.add_target(BasicTarget::new("exists.txt"));
    assert_ne!(watcher.get_state(1).unwrap(), &WatchState::DoesNotExist);
}

pub fn get_path(&self, index: usize) -> Option<&PathBuf>[src]

Attempt to get the path corresponding to the given target index.

Examples

use fwatch::{BasicTarget, Watcher, WatchState};

fn main() {
    let mut watcher : Watcher<BasicTarget> = Watcher::new();
    watcher.add_target(BasicTarget::new("foo.txt"));

    let path = watcher.get_path(0).unwrap();
    assert_eq!(path.to_str().unwrap(), "foo.txt");
}

pub fn watch(&mut self) -> Vec<Transition>[src]

Observe any state transitions in our targets.

Returns a vector containing the observed state transition for each target.

Examples

use fwatch::{BasicTarget, Watcher, Transition};

fn main() {
    let mut watcher : Watcher<BasicTarget> = Watcher::new();

    // Watch a file that doesn't exist
    watcher.add_target(BasicTarget::new("does_not_exist.txt"));

    let results = watcher.watch();

    for (index, transition) in watcher.watch().into_iter().enumerate() {
        // Get a reference to the path and state of the current target
        let path = watcher.get_path(index).unwrap();
        let state = watcher.get_state(index).unwrap();

        match transition {
            Transition::Created => { /* The watched file has been created */ },
            Transition::Modified => { /* The watched file has been modified */ },
            Transition::Deleted => { /* The watched file has been deleted */ },
            Transition::None => { /* None of the above transitions were observed */ },
        }
    }
}

Trait Implementations

impl<W: Default + Watchable> Default for Watcher<W>[src]

impl<W: Debug + Watchable> Debug for Watcher<W>[src]

Auto Trait Implementations

impl<W> Send for Watcher<W> where
    W: Send

impl<W> Sync for Watcher<W> where
    W: Sync

impl<W> Unpin for Watcher<W> where
    W: Unpin

impl<W> UnwindSafe for Watcher<W> where
    W: UnwindSafe

impl<W> RefUnwindSafe for Watcher<W> where
    W: RefUnwindSafe

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]