Struct Watcher

Source
pub struct Watcher<F, Fut>
where F: Fn(Snapshot) -> Fut, Fut: Future<Output = ()>,
{ /* private fields */ }
Expand description

A file watcher that monitors changes in a list of files and triggers a callback when changes are detected.

§Generics

  • F: A closure or function that takes a Snapshot and produces a future.
  • Fut: The type of the future returned by the callback.

§Fields

  • files: A list of FileInfo containing metadata about the monitored files.
  • callback: A closure or function to execute when changes are detected.
  • poll_interval: The interval (in milliseconds) at which files are polled for changes.

Implementations§

Source§

impl<F, Fut> Watcher<F, Fut>
where F: Fn(Snapshot) -> Fut, Fut: Future<Output = ()>,

Source

pub async fn new( paths: Vec<PathBuf>, poll_interval: u64, callback: F, ) -> Result<Self, Error>

Creates a new Watcher instance.

§Parameters
  • paths: A list of paths to the files to monitor.
  • poll_interval: The interval (in milliseconds) at which files are polled for changes.
  • callback: A closure or function to execute when changes are detected.
§Returns
  • Ok(Self): A new instance of Watcher if all files are successfully initialized.
  • Err(Error): An error if any file cannot be initialized (e.g., failed to read metadata).
§Errors
  • Returns an error with ErrorKind::ErrorInitializingWatcher if a file’s metadata cannot be read.
Source

pub async fn watch(&mut self) -> Result<(), Error>

Starts monitoring the files for changes.

§Behavior
  • Periodically checks the modification time of each file.
  • If any file has been modified since the last check, triggers the callback with a Snapshot.
  • Continues indefinitely until the task is canceled.
§Returns
  • Ok(()): This function only exits if the task is canceled or an error occurs.
  • Err(Error): An error if reading a file’s metadata or content fails.
§Errors
  • Returns an error with ErrorKind::ErrorReadingFile if file metadata or content cannot be read.
Source

pub async fn snapshot(&self) -> Result<Snapshot, Error>

Generates a snapshot of the current state of the monitored files.

§Returns
  • Ok(Snapshot): A snapshot containing the data of all monitored files.
  • Err(Error): An error if any file cannot be read.
§Errors
  • Returns an error with ErrorKind::ErrorReadingFile if a file cannot be read.

Auto Trait Implementations§

§

impl<F, Fut> Freeze for Watcher<F, Fut>
where F: Freeze,

§

impl<F, Fut> RefUnwindSafe for Watcher<F, Fut>
where F: RefUnwindSafe,

§

impl<F, Fut> Send for Watcher<F, Fut>
where F: Send,

§

impl<F, Fut> Sync for Watcher<F, Fut>
where F: Sync,

§

impl<F, Fut> Unpin for Watcher<F, Fut>
where F: Unpin,

§

impl<F, Fut> UnwindSafe for Watcher<F, Fut>
where F: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.