From 6c653e8e528c2248559cc7dccf62c61878fe9f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Mon, 28 Oct 2019 18:55:39 +1300 Subject: [PATCH] Return owned Args from Handler instead of borrowed --- src/main.rs | 2 +- src/run.rs | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7834023..b206983 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,5 +3,5 @@ extern crate watchexec; use watchexec::{cli, error, run}; fn main() -> error::Result<()> { - run(cli::get_args()?) + run(&cli::get_args()?) } diff --git a/src/run.rs b/src/run.rs index c1bdb12..f2c5196 100644 --- a/src/run.rs +++ b/src/run.rs @@ -59,15 +59,18 @@ pub trait Handler { /// - `Ok(false)`: everything is fine but we should gracefully stop. fn on_update(&self, ops: &[PathOp]) -> Result; - /// Handler implementations must return a customized watchexec Args reference for use in the - /// calling watcher. - fn args(&self) -> &Args; + /// Called once by `watch` at the very start. + /// + /// Not called again; any changes will never be picked up. + /// + /// The `Args` instance should be created using `ArgsBuilder` rather than direct initialisation + /// to resist potential breaking changes (see semver policy on crate root). + fn args(&self) -> Args; } /// Starts watching, and calls a handler when something happens. /// -/// Given an argument structure and a `Handler` type, starts the watcher -/// loop (blocking until done). +/// Given an argument structure and a `Handler` type, starts the watcher loop, blocking until done. pub fn watch(handler: &H) -> Result<()> where H: Handler, @@ -189,8 +192,8 @@ impl<'a> ExecHandler<'a> { } impl<'a> Handler for ExecHandler<'a> { - fn args(&self) -> &Args { - &self.args + fn args(&self) -> Args { + self.args.clone() } // Only returns Err() on lock poisoning. @@ -252,9 +255,8 @@ impl<'a> Handler for ExecHandler<'a> { } } -pub fn run(args: Args) -> Result<()> { - let handler = ExecHandler::new(&args)?; - watch(&handler) +pub fn run(args: &Args) -> Result<()> { + watch(&ExecHandler::new(args)?) } fn wait_fs(rx: &Receiver, filter: &NotificationFilter, debounce: u64) -> Vec {