Struct hotwatch::Hotwatch

source ·
pub struct Hotwatch { /* private fields */ }
Expand description

A non-blocking hotwatch instance.

Watching begins as soon as Self::watch is called, and occurs in a background thread. The background thread runs until this is dropped.

Dropping this will also unwatch everything.

Implementations§

source§

impl Hotwatch

source

pub fn new() -> Result<Self, Error>

Creates a new non-blocking hotwatch instance.

Errors

This will fail if the underlying notify instance fails to initialize.

Examples
use hotwatch::Hotwatch;

let hotwatch = Hotwatch::new().expect("hotwatch failed to initialize");
source

pub fn new_with_custom_delay(delay: Duration) -> Result<Self, Error>

Using Hotwatch::new will give you a default delay of 2 seconds. This method allows you to specify your own value.

Notes

A delay of over 30 seconds will prevent repetitions of previous events on macOS.

source

pub fn watch<P, F>(&mut self, path: P, handler: F) -> Result<(), Error>where P: AsRef<Path>, F: 'static + FnMut(Event) + Send,

Watch a path and register a handler to it.

When watching a directory, that handler will receive all events for all directory contents, even recursing through subdirectories.

Only the most specific applicable handler will be called. In other words, if you’re watching “dir” and “dir/file1”, then only the latter handler will fire for changes to file1.

Note that handlers will be run in hotwatch’s watch thread, so you’ll have to use move if the closure captures anything.

Errors

Watching will fail if the path can’t be read, returning Error::Io.

Examples
use hotwatch::{Hotwatch, Event, EventKind};

let mut hotwatch = Hotwatch::new().expect("hotwatch failed to initialize!");
hotwatch.watch("README.md", |event: Event| {
    if let EventKind::Modify(_) = event.kind {
        println!("{:?} changed!", event.paths[0]);
    }
}).expect("failed to watch file!");
source

pub fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error>

Stop watching a path.

Errors

This will fail if the path wasn’t being watched, or if the path couldn’t be unwatched for some platform-specific internal reason.

Trait Implementations§

source§

impl Debug for Hotwatch

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.