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<Path> is the standard for APIs which have file path parameters, as
it works across both owned and un-owned paths. Also, AsRef<Path> 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<Path> as input.

Signed-off-by: Kyle Wood <https://github.com/DemonWav>
This commit is contained in:
Kyle Wood 2020-08-20 23:51:41 -07:00 committed by GitHub
parent 6f8f9bd75d
commit 811f7783ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<LogWatcher, io::Error> {
let f = match File::open(filename.clone()) {
pub fn register<P: AsRef<Path>>(filename: P) -> Result<LogWatcher, io::Error> {
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() {