From 811f7783ab4c24a290655fb8bd5a09f4cc501af8 Mon Sep 17 00:00:00 2001 From: Kyle Wood Date: Thu, 20 Aug 2020 23:51:41 -0700 Subject: [PATCH] Allow any generic Path as input to register (#7) * Allow any generic Path as input to register Previously only String was allowed, which can be annoying for clients which already have a PathBuf or Path object. The generic type AsRef is the standard for APIs which have file path parameters, as it works across both owned and un-owned paths. Also, AsRef allows Strings and OS specific strings to be used directly as well. The filename parameter itself is only used for calling File::open(), which itself also takes in an AsRef as input. Signed-off-by: Kyle Wood --- src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3833a7e..79ae79b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use std::io::BufReader; use std::io::ErrorKind; use std::io::SeekFrom; use std::os::unix::fs::MetadataExt; +use std::path::Path; use std::thread::sleep; use std::time::Duration; @@ -22,8 +23,8 @@ pub struct LogWatcher { } impl LogWatcher { - pub fn register(filename: String) -> Result { - let f = match File::open(filename.clone()) { + pub fn register>(filename: P) -> Result { + let f = match File::open(&filename) { Ok(x) => x, Err(err) => return Err(err), }; @@ -37,7 +38,7 @@ impl LogWatcher { let pos = metadata.len(); reader.seek(SeekFrom::Start(pos)).unwrap(); Ok(LogWatcher { - filename: filename, + filename: filename.as_ref().to_string_lossy().to_string(), inode: metadata.ino(), pos: pos, reader: reader, @@ -50,7 +51,7 @@ impl LogWatcher { F: FnMut(String) -> LogWatcherAction, { loop { - match File::open(self.filename.clone()) { + match File::open(&self.filename) { Ok(x) => { let f = x; let metadata = match f.metadata() {