Skip to content

Commit

Permalink
Remove unnecessary allocation/clone (#8)
Browse files Browse the repository at this point in the history
* Remove unnecessary allocation/clone

Not needed in util.rs, can done as well with references

* Update CHANGELOG
  • Loading branch information
izderadicka authored Oct 12, 2021
1 parent 153ed7d commit 66d0d3d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# Version 0.4.6 (2021-10-09)

- Removed unnecessary allocation from util::handler_for_event

# Version 0.4.5 (2020-11-25)

- The blocking implementation of `watch` now also accepts `FnMut` instead of `Fn`.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hotwatch"
version = "0.4.5"
version = "0.4.6"
authors = ["Francesca Lovebloom <[email protected]>"]
edition = "2018"
description = "A Rust library for conveniently watching and handling file changes."
Expand Down
21 changes: 12 additions & 9 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::Event;
use std::{collections::HashMap, path::PathBuf};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};

pub fn log_event(e: &Event) {
log::debug!("received event 🎉: {:#?}", e);
Expand All @@ -13,30 +16,30 @@ pub fn handler_for_event<'a, H>(
e: &Event,
handlers: &'a mut HashMap<PathBuf, H>,
) -> Option<&'a mut H> {
fn path_from_event(e: &Event) -> Option<PathBuf> {
fn path_from_event(e: &Event) -> Option<&Path> {
match e {
Event::NoticeWrite(p)
| Event::NoticeRemove(p)
| Event::Create(p)
| Event::Write(p)
| Event::Chmod(p)
| Event::Remove(p)
| Event::Rename(p, _) => Some(p.clone()),
| Event::Rename(p, _) => Some(p.as_path()),
_ => None,
}
}

fn find_handler<'a, H>(
mut path: PathBuf,
path: &Path,
handlers: &'a mut HashMap<PathBuf, H>,
) -> Option<&'a mut H> {
let mut poppable = true;
while poppable {
let mut remaining_path = Some(path);
while let Some(path) = remaining_path {
log::debug!("matching against {:?}", path);
if handlers.contains_key(&path) {
return handlers.get_mut(&path);
if handlers.contains_key(path) {
return handlers.get_mut(path);
}
poppable = path.pop();
remaining_path = path.parent();
}
None
}
Expand Down

0 comments on commit 66d0d3d

Please sign in to comment.