Skip to content

Commit

Permalink
Merge pull request #664 from florian-g2/main
Browse files Browse the repository at this point in the history
FileIdCache: Allow flexible handle instead of direct borrow for file ids
  • Loading branch information
dfaust authored Feb 8, 2025
2 parents 512bf70 + 2f48155 commit 1a34031
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## debouncer-full 0.6.0 (unreleased)
- FEATURE: allow `FileIdCache` trait implementations to choose ownership of the returned file-ids

## file-id 0.2.3 (unreleased)
- CHANGE: implement `AsRef<FileId>` for `FileId`

## notify 8.0.0 (2025-01-10)

- CHANGE: update notify-types to version 2.0.0
Expand Down
6 changes: 6 additions & 0 deletions file-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ impl FileId {
}
}

impl AsRef<FileId> for FileId {
fn as_ref(&self) -> &FileId {
self
}
}

/// Get the `FileId` for the file or directory at `path`
#[cfg(target_family = "unix")]
pub fn get_file_id(path: impl AsRef<Path>) -> io::Result<FileId> {
Expand Down
9 changes: 4 additions & 5 deletions notify-debouncer-full/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{
collections::HashMap,
path::{Path, PathBuf},
};

use file_id::{get_file_id, FileId};
use notify::RecursiveMode;
use walkdir::WalkDir;
Expand All @@ -14,7 +13,7 @@ pub trait FileIdCache {
/// Get a `FileId` from the cache for a given `path`.
///
/// If the path is not cached, `None` should be returned and there should not be any attempt to read the file ID from disk.
fn cached_file_id(&self, path: &Path) -> Option<&FileId>;
fn cached_file_id(&self, path: &Path) -> Option<impl AsRef<FileId>>;

/// Add a new path to the cache or update its value.
///
Expand Down Expand Up @@ -64,7 +63,7 @@ impl FileIdMap {
}

impl FileIdCache for FileIdMap {
fn cached_file_id(&self, path: &Path) -> Option<&FileId> {
fn cached_file_id(&self, path: &Path) -> Option<impl AsRef<FileId>> {
self.paths.get(path)
}

Expand Down Expand Up @@ -104,8 +103,8 @@ impl NoCache {
}

impl FileIdCache for NoCache {
fn cached_file_id(&self, _path: &Path) -> Option<&FileId> {
None
fn cached_file_id(&self, _path: &Path) -> Option<impl AsRef<FileId>> {
Option::<&FileId>::None
}

fn add_path(&mut self, _path: &Path, _recursive_mode: RecursiveMode) {}
Expand Down
4 changes: 2 additions & 2 deletions notify-debouncer-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl<T: FileIdCache> DebounceDataInner<T> {
let path = &event.paths[0];

// store event
let file_id = self.cache.cached_file_id(path).cloned();
let file_id = self.cache.cached_file_id(path).map(|id| *id.as_ref());
self.rename_event = Some((DebouncedEvent::new(event.clone(), time), file_id));

self.cache.remove_path(path);
Expand Down Expand Up @@ -372,7 +372,7 @@ impl<T: FileIdCache> DebounceDataInner<T> {
.and_then(|from_file_id| {
self.cache
.cached_file_id(&event.paths[0])
.map(|to_file_id| from_file_id == to_file_id)
.map(|to_file_id| from_file_id == to_file_id.as_ref())
})
.unwrap_or_default();

Expand Down

0 comments on commit 1a34031

Please sign in to comment.