Return owned Args from Handler instead of borrowed

This commit is contained in:
Félix Saparelli 2019-10-28 18:55:39 +13:00
parent 720ff44b71
commit 6c653e8e52
No known key found for this signature in database
GPG Key ID: 25940898BB90EA51
2 changed files with 13 additions and 11 deletions

View File

@ -3,5 +3,5 @@ extern crate watchexec;
use watchexec::{cli, error, run}; use watchexec::{cli, error, run};
fn main() -> error::Result<()> { fn main() -> error::Result<()> {
run(cli::get_args()?) run(&cli::get_args()?)
} }

View File

@ -59,15 +59,18 @@ pub trait Handler {
/// - `Ok(false)`: everything is fine but we should gracefully stop. /// - `Ok(false)`: everything is fine but we should gracefully stop.
fn on_update(&self, ops: &[PathOp]) -> Result<bool>; fn on_update(&self, ops: &[PathOp]) -> Result<bool>;
/// Handler implementations must return a customized watchexec Args reference for use in the /// Called once by `watch` at the very start.
/// calling watcher. ///
fn args(&self) -> &Args; /// 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. /// Starts watching, and calls a handler when something happens.
/// ///
/// Given an argument structure and a `Handler` type, starts the watcher /// Given an argument structure and a `Handler` type, starts the watcher loop, blocking until done.
/// loop (blocking until done).
pub fn watch<H>(handler: &H) -> Result<()> pub fn watch<H>(handler: &H) -> Result<()>
where where
H: Handler, H: Handler,
@ -189,8 +192,8 @@ impl<'a> ExecHandler<'a> {
} }
impl<'a> Handler for ExecHandler<'a> { impl<'a> Handler for ExecHandler<'a> {
fn args(&self) -> &Args { fn args(&self) -> Args {
&self.args self.args.clone()
} }
// Only returns Err() on lock poisoning. // Only returns Err() on lock poisoning.
@ -252,9 +255,8 @@ impl<'a> Handler for ExecHandler<'a> {
} }
} }
pub fn run(args: Args) -> Result<()> { pub fn run(args: &Args) -> Result<()> {
let handler = ExecHandler::new(&args)?; watch(&ExecHandler::new(args)?)
watch(&handler)
} }
fn wait_fs(rx: &Receiver<Event>, filter: &NotificationFilter, debounce: u64) -> Vec<PathOp> { fn wait_fs(rx: &Receiver<Event>, filter: &NotificationFilter, debounce: u64) -> Vec<PathOp> {