From dd7afaa64aa54fac5bd30404eb44ccd9d68f31c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20misaki=20masa?= Date: Wed, 27 Aug 2025 23:52:09 +0800 Subject: [PATCH] refactor: `Url::pair()` without allocation (#3116) --- yazi-actor/src/mgr/cd.rs | 4 ++-- yazi-actor/src/mgr/copy.rs | 2 +- yazi-actor/src/mgr/create.rs | 6 +++--- yazi-actor/src/mgr/follow.rs | 2 +- yazi-actor/src/mgr/leave.rs | 4 ++-- yazi-actor/src/mgr/open.rs | 2 +- yazi-actor/src/mgr/rename.rs | 10 +++++----- yazi-actor/src/mgr/reveal.rs | 8 ++++---- yazi-actor/src/mgr/update_files.rs | 2 +- yazi-binding/src/url.rs | 2 +- yazi-boot/src/boot.rs | 2 +- yazi-cli/src/shared/shared.rs | 4 ++-- yazi-core/src/mgr/yanked.rs | 2 +- yazi-core/src/tab/selected.rs | 24 +++++++++++------------- yazi-fs/src/fns.rs | 4 +--- yazi-fs/src/op.rs | 6 +++--- yazi-plugin/src/utils/cache.rs | 2 +- yazi-scheduler/src/file/file.rs | 2 +- yazi-scheduler/src/prework/prework.rs | 2 +- yazi-shared/src/url/buf.rs | 17 ++++++++--------- yazi-shared/src/url/cov.rs | 2 +- yazi-shared/src/url/cow.rs | 18 ++++++++++++++---- yazi-shared/src/url/scheme.rs | 2 +- yazi-shared/src/url/url.rs | 8 +++----- yazi-watcher/src/backend/backend.rs | 2 +- yazi-watcher/src/watcher.rs | 6 +++--- 26 files changed, 74 insertions(+), 71 deletions(-) diff --git a/yazi-actor/src/mgr/cd.rs b/yazi-actor/src/mgr/cd.rs index 1caaa625..7b9a2160 100644 --- a/yazi-actor/src/mgr/cd.rs +++ b/yazi-actor/src/mgr/cd.rs @@ -52,7 +52,7 @@ impl Actor for Cd { tab.history.insert(rep.url.to_owned(), rep); // Parent - if let Some(parent) = opt.target.parent_url() { + if let Some(parent) = opt.target.parent() { tab.parent = Some(tab.history.remove_or(&parent)); } @@ -83,7 +83,7 @@ impl Cd { return MgrProxy::cd(&url); } - if let Some(p) = url.parent_url() { + if let Some(p) = url.parent() { FilesOp::Upserting(p.into(), [(url.urn().to_owned(), file)].into()).emit(); } MgrProxy::reveal(&url); diff --git a/yazi-actor/src/mgr/copy.rs b/yazi-actor/src/mgr/copy.rs index 5a845e0c..9c6c2e3e 100644 --- a/yazi-actor/src/mgr/copy.rs +++ b/yazi-actor/src/mgr/copy.rs @@ -33,7 +33,7 @@ impl Actor for Copy { s.push(opt.separator.transform(&u.os_str())); } "dirname" => { - if let Some(p) = u.parent_url() { + if let Some(p) = u.parent() { s.push(opt.separator.transform(&p.os_str())); } } diff --git a/yazi-actor/src/mgr/create.rs b/yazi-actor/src/mgr/create.rs index 786ead16..094e8007 100644 --- a/yazi-actor/src/mgr/create.rs +++ b/yazi-actor/src/mgr/create.rs @@ -50,9 +50,9 @@ impl Create { && let Some((parent, urn)) = real.pair() { ok_or_not_found(provider::remove_file(&new).await)?; - FilesOp::Deleting(parent.into(), [urn].into()).emit(); + FilesOp::Deleting(parent.into(), [urn.into()].into()).emit(); provider::create(&new).await?; - } else if let Some(parent) = new.parent_url() { + } else if let Some(parent) = new.parent() { provider::create_dir_all(&parent).await.ok(); ok_or_not_found(provider::remove_file(&new).await)?; provider::create(&new).await?; @@ -64,7 +64,7 @@ impl Create { && let Some((parent, urn)) = real.pair() { let file = File::new(&real).await?; - FilesOp::Upserting(parent.into(), [(urn, file)].into()).emit(); + FilesOp::Upserting(parent.into(), [(urn.into(), file)].into()).emit(); MgrProxy::reveal(&real); } diff --git a/yazi-actor/src/mgr/follow.rs b/yazi-actor/src/mgr/follow.rs index e1cd3c3b..8802b6d2 100644 --- a/yazi-actor/src/mgr/follow.rs +++ b/yazi-actor/src/mgr/follow.rs @@ -17,7 +17,7 @@ impl Actor for Follow { let Some(file) = cx.hovered() else { succ!() }; let Some(link_to) = &file.link_to else { succ!() }; - if let Some(p) = file.url.parent_url() { + if let Some(p) = file.url.parent() { act!(mgr:reveal, cx, clean_url(p.join(link_to))) } else { succ!() diff --git a/yazi-actor/src/mgr/leave.rs b/yazi-actor/src/mgr/leave.rs index 8aa7a04a..4c1935d8 100644 --- a/yazi-actor/src/mgr/leave.rs +++ b/yazi-actor/src/mgr/leave.rs @@ -15,9 +15,9 @@ impl Actor for Leave { fn act(cx: &mut Ctx, _: Self::Options) -> Result { let url = cx .hovered() - .and_then(|h| h.url.parent_url()) + .and_then(|h| h.url.parent()) .filter(|u| u != cx.cwd()) - .or_else(|| cx.cwd().parent_url()); + .or_else(|| cx.cwd().parent()); let Some(mut url) = url else { succ!() }; if url.is_search() { diff --git a/yazi-actor/src/mgr/open.rs b/yazi-actor/src/mgr/open.rs index 99fd190c..75c0e1a1 100644 --- a/yazi-actor/src/mgr/open.rs +++ b/yazi-actor/src/mgr/open.rs @@ -111,7 +111,7 @@ impl Actor for OpenDo { impl Open { fn guess_folder(cx: &Ctx, url: &UrlBuf) -> bool { - let Some(p) = url.parent_url() else { + let Some(p) = url.parent() else { return true; }; diff --git a/yazi-actor/src/mgr/rename.rs b/yazi-actor/src/mgr/rename.rs index e50887b4..1d97c294 100644 --- a/yazi-actor/src/mgr/rename.rs +++ b/yazi-actor/src/mgr/rename.rs @@ -48,7 +48,7 @@ impl Actor for Rename { return; } - let new = old.parent_url().unwrap().join(name); + let new = old.parent().unwrap().join(name); if opt.force || !maybe_exists(&new).await || provider::must_identical(&old, &new).await { Self::r#do(tab, old, new).await.ok(); } else if ConfirmProxy::show(ConfirmCfg::overwrite(&new)).await { @@ -72,7 +72,7 @@ impl Rename { && let Some((parent, urn)) = u.pair() { ok_or_not_found(provider::rename(&u, &new).await)?; - FilesOp::Deleting(parent.to_owned(), [urn].into()).emit(); + FilesOp::Deleting(parent.to_owned(), [urn.into()].into()).emit(); } let new = provider::casefold(&new).await?; @@ -80,10 +80,10 @@ impl Rename { let file = File::new(&new).await?; if new_p == old_p { - FilesOp::Upserting(old_p.into(), [(old_n, file)].into()).emit(); + FilesOp::Upserting(old_p.into(), [(old_n.into(), file)].into()).emit(); } else { - FilesOp::Deleting(old_p.into(), [old_n].into()).emit(); - FilesOp::Upserting(new_p.into(), [(new_n, file)].into()).emit(); + FilesOp::Deleting(old_p.into(), [old_n.into()].into()).emit(); + FilesOp::Upserting(new_p.into(), [(new_n.into(), file)].into()).emit(); } MgrProxy::reveal(&new); diff --git a/yazi-actor/src/mgr/reveal.rs b/yazi-actor/src/mgr/reveal.rs index 635fe207..4619532d 100644 --- a/yazi-actor/src/mgr/reveal.rs +++ b/yazi-actor/src/mgr/reveal.rs @@ -21,17 +21,17 @@ impl Actor for Reveal { // Try to hover on the child file let tab = cx.tab_mut(); - render!(tab.current.hover(child.as_urn())); + render!(tab.current.hover(child)); // If the child is not hovered, which means it doesn't exist, // create a dummy file - if !opt.no_dummy && tab.hovered().is_none_or(|f| &child != f.urn()) { - let op = FilesOp::Creating(parent.into(), vec![File::from_dummy(opt.target, None)]); + if !opt.no_dummy && tab.hovered().is_none_or(|f| child != f.urn()) { + let op = FilesOp::Creating(parent.into(), vec![File::from_dummy(&opt.target, None)]); tab.current.update_pub(tab.id, op); } // Now, we can safely hover on the target - act!(mgr:hover, cx, Some(child))?; + act!(mgr:hover, cx, Some(child.into()))?; act!(mgr:peek, cx)?; act!(mgr:watch, cx)?; diff --git a/yazi-actor/src/mgr/update_files.rs b/yazi-actor/src/mgr/update_files.rs index 92b46451..d2219931 100644 --- a/yazi-actor/src/mgr/update_files.rs +++ b/yazi-actor/src/mgr/update_files.rs @@ -101,7 +101,7 @@ impl UpdateFiles { fn update_history(cx: &mut Ctx, op: FilesOp) -> Result { let tab = &mut cx.tab_mut(); - let leave = tab.parent.as_ref().and_then(|f| f.url.parent_url().map(|p| (p, f.url.urn()))).is_some_and( + let leave = tab.parent.as_ref().and_then(|f| f.url.parent().map(|p| (p, f.url.urn()))).is_some_and( |(p, n)| matches!(op, FilesOp::Deleting(ref parent, ref urns) if *parent == p && urns.contains(n)), ); diff --git a/yazi-binding/src/url.rs b/yazi-binding/src/url.rs index bc3b3643..207ab075 100644 --- a/yazi-binding/src/url.rs +++ b/yazi-binding/src/url.rs @@ -102,7 +102,7 @@ impl UserData for Url { cached_field!(fields, ext, |lua, me| { me.ext().map(|s| lua.create_string(s.as_encoded_bytes())).transpose() }); - cached_field!(fields, parent, |_, me| Ok(me.parent_url().map(Self::new))); + cached_field!(fields, parent, |_, me| Ok(me.parent().map(Self::new))); cached_field!(fields, urn, |_, me| Ok(Urn::new(me.urn()))); cached_field!(fields, base, |_, me| Ok(me.base().map(Self::new))); cached_field!(fields, domain, |lua, me| { diff --git a/yazi-boot/src/boot.rs b/yazi-boot/src/boot.rs index 47a6a67a..db2e6149 100644 --- a/yazi-boot/src/boot.rs +++ b/yazi-boot/src/boot.rs @@ -32,7 +32,7 @@ impl Boot { }; if provider::metadata(&entry).await.is_ok_and(|m| m.is_file()) { - (parent.into(), child) + (parent.into(), child.into()) } else { (entry, UrnBuf::default()) } diff --git a/yazi-cli/src/shared/shared.rs b/yazi-cli/src/shared/shared.rs index f3e42c1c..d1d1dbb0 100644 --- a/yazi-cli/src/shared/shared.rs +++ b/yazi-cli/src/shared/shared.rs @@ -34,10 +34,10 @@ pub async fn copy_and_seal(from: &Path, to: &Path) -> io::Result<()> { pub async fn remove_sealed(p: &Path) -> io::Result<()> { #[cfg(windows)] { - let mut perm = tokio::fs::metadata(p).await?.permissions(); + let mut perm = Local::metadata(p).await?.permissions(); perm.set_readonly(false); tokio::fs::set_permissions(p, perm).await?; } - tokio::fs::remove_file(p).await + Local::remove_file(p).await } diff --git a/yazi-core/src/mgr/yanked.rs b/yazi-core/src/mgr/yanked.rs index 0e11b53d..89e26f30 100644 --- a/yazi-core/src/mgr/yanked.rs +++ b/yazi-core/src/mgr/yanked.rs @@ -51,7 +51,7 @@ impl Yanked { let mut it = u.components(); it.next_back().is_some() && it.covariant(&dir.components()) - && u.parent_url().is_some_and(|p| p == *dir) + && u.parent().is_some_and(|p| p == *dir) }) } diff --git a/yazi-core/src/tab/selected.rs b/yazi-core/src/tab/selected.rs index 20898562..acb273e7 100644 --- a/yazi-core/src/tab/selected.rs +++ b/yazi-core/src/tab/selected.rs @@ -35,10 +35,9 @@ impl Selected { T: Into>, { let mut grouped: HashMap<_, Vec<_>> = Default::default(); - for url in urls { - let u = url.into(); - if let Some(p) = u.parent_url() { - grouped.entry(p).or_default().push(u); + for url in urls.into_iter().map(Into::into) { + if let Some(p) = url.parent() { + grouped.entry(p).or_default().push(url); } } grouped.into_values().map(|v| self.add_same(v)).sum() @@ -57,14 +56,14 @@ impl Selected { } // If it has appeared as a child - let mut parent = urls[0].parent_url(); + let mut parent = urls[0].parent(); let mut parents = vec![]; while let Some(u) = parent { if self.inner.contains_key(&UrlCov::new(&u)) { return 0; } - parent = u.parent_url(); + parent = u.parent(); parents.push(u); } @@ -88,10 +87,9 @@ impl Selected { T: Into>, { let mut grouped: HashMap<_, Vec<_>> = Default::default(); - for url in urls { - let u = url.into(); - if let Some(p) = u.parent_url() { - grouped.entry(p).or_default().push(u); + for url in urls.into_iter().map(Into::into) { + if let Some(p) = url.parent() { + grouped.entry(p).or_default().push(url); } } @@ -116,8 +114,8 @@ impl Selected { return 0; } - // FIXME: use UrlCov::parent_url() instead - let mut parent = first.parent_url(); + // FIXME: use UrlCov::parent() instead + let mut parent = first.parent(); while let Some(u) = parent { let n = self.parents.get_mut(&UrlCov::new(&u)).unwrap(); @@ -126,7 +124,7 @@ impl Selected { self.parents.remove(&UrlCov::new(&u)); } - parent = u.parent_url(); + parent = u.parent(); } count } diff --git a/yazi-fs/src/fns.rs b/yazi-fs/src/fns.rs index 83ef617a..0f0ae499 100644 --- a/yazi-fs/src/fns.rs +++ b/yazi-fs/src/fns.rs @@ -1,5 +1,3 @@ -// FIXME: VFS - use anyhow::Result; use tokio::{io, select, sync::{mpsc, oneshot}, time}; use yazi_shared::url::{Component, Url, UrlBuf}; @@ -159,7 +157,7 @@ pub fn max_common_root(urls: &[UrlBuf]) -> usize { return urls[0].components().count() - 1; } - let mut it = urls.iter().map(|u| u.parent_url()); + let mut it = urls.iter().map(|u| u.parent()); let Some(first) = it.next().unwrap() else { return 0; // The first URL has no parent }; diff --git a/yazi-fs/src/op.rs b/yazi-fs/src/op.rs index c0a8c6fd..018eeea3 100644 --- a/yazi-fs/src/op.rs +++ b/yazi-fs/src/op.rs @@ -54,8 +54,8 @@ impl FilesOp { pub fn rename(map: HashMap) { let mut parents: HashMap<_, (HashSet<_>, HashMap<_, _>)> = Default::default(); for (o, n) in map { - let Some(o_p) = o.parent_url() else { continue }; - let Some(n_p) = n.url.parent_url() else { continue }; + let Some(o_p) = o.parent() else { continue }; + let Some(n_p) = n.url.parent() else { continue }; if o_p != n_p { parents.entry_ref(&o_p).or_default().0.insert(o.urn().to_owned()); } @@ -126,7 +126,7 @@ impl FilesOp { } else if maybe_exists(cwd).await { Self::IOErr(cwd.clone(), kind).emit(); } else if let Some((p, n)) = cwd.pair() { - Self::Deleting(p.into(), [n].into()).emit(); + Self::Deleting(p.into(), [n.into()].into()).emit(); } } diff --git a/yazi-plugin/src/utils/cache.rs b/yazi-plugin/src/utils/cache.rs index 353cbe79..c566fdd4 100644 --- a/yazi-plugin/src/utils/cache.rs +++ b/yazi-plugin/src/utils/cache.rs @@ -11,7 +11,7 @@ impl Utils { pub(super) fn file_cache(lua: &Lua) -> mlua::Result { lua.create_function(|_, t: Table| { let file: FileRef = t.raw_get("file")?; - if file.url.parent_url() == Some(yazi_shared::url::Url::regular(&YAZI.preview.cache_dir)) { + if file.url.parent() == Some(yazi_shared::url::Url::regular(&YAZI.preview.cache_dir)) { return Ok(None); } diff --git a/yazi-scheduler/src/file/file.rs b/yazi-scheduler/src/file/file.rs index f9050465..f8eefbbf 100644 --- a/yazi-scheduler/src/file/file.rs +++ b/yazi-scheduler/src/file/file.rs @@ -77,7 +77,7 @@ impl File { }; let src = if task.relative { - path_relative_to(provider::canonicalize(&task.to.parent_url().unwrap()).await?.loc, &src)? + path_relative_to(provider::canonicalize(&task.to.parent().unwrap()).await?.loc, &src)? } else { src }; diff --git a/yazi-scheduler/src/prework/prework.rs b/yazi-scheduler/src/prework/prework.rs index 3b481fb7..fdbd046e 100644 --- a/yazi-scheduler/src/prework/prework.rs +++ b/yazi-scheduler/src/prework/prework.rs @@ -90,7 +90,7 @@ impl Prework { } } - let parent = buf[0].0.parent_url().unwrap(); + let parent = buf[0].0.parent().unwrap(); FilesOp::Size( parent.into(), HashMap::from_iter(buf.into_iter().map(|(u, s)| (u.urn().to_owned(), s))), diff --git a/yazi-shared/src/url/buf.rs b/yazi-shared/src/url/buf.rs index 32178b66..5acbdece 100644 --- a/yazi-shared/src/url/buf.rs +++ b/yazi-shared/src/url/buf.rs @@ -3,7 +3,6 @@ use std::{borrow::Cow, ffi::OsStr, fmt::{Debug, Formatter}, hash::BuildHasher, p use anyhow::Result; use serde::{Deserialize, Serialize}; -use super::UrnBuf; use crate::{loc::LocBuf, pool::Pool, url::{Components, Display, Encode, EncodeTilded, Scheme, Uri, Url, UrlCow, Urn}}; #[derive(Clone, Default, Eq, Ord, PartialOrd, PartialEq, Hash)] @@ -96,7 +95,7 @@ impl UrlBuf { pub fn covariant(&self, other: &Self) -> bool { self.as_url().covariant(other) } #[inline] - pub fn parent_url(&self) -> Option> { self.as_url().parent_url() } + pub fn parent(&self) -> Option> { self.as_url().parent() } #[inline] pub fn starts_with<'a>(&self, base: impl Into>) -> bool { @@ -156,7 +155,7 @@ impl UrlBuf { } #[inline] - pub fn pair(&self) -> Option<(Url<'_>, UrnBuf)> { self.as_url().pair() } + pub fn pair(&self) -> Option<(Url<'_>, &Urn)> { self.as_url().pair() } #[inline] pub fn hash_u64(&self) -> u64 { foldhash::fast::FixedState::default().hash_one(self) } @@ -312,7 +311,7 @@ mod tests { } #[test] - fn test_parent_url() -> anyhow::Result<()> { + fn test_parent() -> anyhow::Result<()> { crate::init_tests(); let cases = [ // Regular @@ -339,7 +338,7 @@ mod tests { for (path, expected) in cases { let path: UrlBuf = path.parse()?; - assert_eq!(path.parent_url().map(|u| format!("{:?}", u)).as_deref(), expected); + assert_eq!(path.parent().map(|u| format!("{:?}", u)).as_deref(), expected); } Ok(()) @@ -355,7 +354,7 @@ mod tests { let u = u.into_search("kw"); assert_eq!(format!("{u:?}"), "search://kw//root"); - assert_eq!(format!("{:?}", u.parent_url().unwrap()), "/"); + assert_eq!(format!("{:?}", u.parent().unwrap()), "/"); let u = u.join("examples"); assert_eq!(format!("{u:?}"), format!("search://kw:1:1//root{S}examples")); @@ -363,13 +362,13 @@ mod tests { let u = u.join("README.md"); assert_eq!(format!("{u:?}"), format!("search://kw:2:2//root{S}examples{S}README.md")); - let u = u.parent_url().unwrap(); + let u = u.parent().unwrap(); assert_eq!(format!("{u:?}"), format!("search://kw:1:1//root{S}examples")); - let u = u.parent_url().unwrap(); + let u = u.parent().unwrap(); assert_eq!(format!("{u:?}"), "search://kw//root"); - let u = u.parent_url().unwrap(); + let u = u.parent().unwrap(); assert_eq!(format!("{u:?}"), "/"); Ok(()) diff --git a/yazi-shared/src/url/cov.rs b/yazi-shared/src/url/cov.rs index d6f56ef3..6023210a 100644 --- a/yazi-shared/src/url/cov.rs +++ b/yazi-shared/src/url/cov.rs @@ -95,5 +95,5 @@ impl UrlBufCov { pub fn as_url(&self) -> UrlCov<'_> { UrlCov::from(self) } #[inline] - pub fn parent_url(&self) -> Option { self.0.parent_url().map(Into::into) } + pub fn parent(&self) -> Option { self.0.parent().map(Into::into) } } diff --git a/yazi-shared/src/url/cow.rs b/yazi-shared/src/url/cow.rs index 0ad98abc..90046025 100644 --- a/yazi-shared/src/url/cow.rs +++ b/yazi-shared/src/url/cow.rs @@ -3,7 +3,7 @@ use std::{borrow::Cow, path::{Path, PathBuf}}; use anyhow::Result; use percent_encoding::percent_decode; -use crate::{IntoOsStr, loc::{Loc, LocBuf}, url::{Components, Scheme, Url, UrlBuf, UrnBuf}}; +use crate::{IntoOsStr, loc::{Loc, LocBuf}, url::{Components, Scheme, Url, UrlBuf, Urn}}; #[derive(Debug)] pub enum UrlCow<'a> { @@ -35,6 +35,10 @@ impl From> for UrlBuf { fn from(value: UrlCow<'_>) -> Self { value.into_owned() } } +impl From<&UrlCow<'_>> for UrlBuf { + fn from(value: &UrlCow<'_>) -> Self { value.as_url().into() } +} + impl<'a> TryFrom<&'a [u8]> for UrlCow<'a> { type Error = anyhow::Error; @@ -107,6 +111,7 @@ impl UrlCow<'_> { } } + #[inline] pub fn as_url(&self) -> Url<'_> { match self { UrlCow::Borrowed(u) => u.as_url(), @@ -115,13 +120,18 @@ impl UrlCow<'_> { } #[inline] - pub fn into_owned(self) -> UrlBuf { self.as_url().into() } + pub fn into_owned(self) -> UrlBuf { + match self { + UrlCow::Borrowed(u) => u.into(), + UrlCow::Owned(u) => u, + } + } #[inline] - pub fn parent_url(&self) -> Option> { self.as_url().parent_url() } + pub fn parent(&self) -> Option> { self.as_url().parent() } #[inline] - pub fn pair(&self) -> Option<(Url<'_>, UrnBuf)> { self.as_url().pair() } + pub fn pair(&self) -> Option<(Url<'_>, &Urn)> { self.as_url().pair() } pub fn parse(bytes: &[u8]) -> Result<(Scheme, Cow<'_, Path>, Option<(usize, usize)>)> { let mut skip = 0; diff --git a/yazi-shared/src/url/scheme.rs b/yazi-shared/src/url/scheme.rs index 92929136..0a952384 100644 --- a/yazi-shared/src/url/scheme.rs +++ b/yazi-shared/src/url/scheme.rs @@ -162,7 +162,7 @@ mod tests { use super::*; #[test] - fn test_decode_port() -> Result<()> { + fn test_decode_ports() -> Result<()> { fn assert(s: &str, len: usize, uri: Option, urn: Option) -> Result<()> { let mut n = usize::MAX; let port = Scheme::decode_ports(s.as_bytes(), &mut n)?; diff --git a/yazi-shared/src/url/url.rs b/yazi-shared/src/url/url.rs index 2398e334..fd8bf544 100644 --- a/yazi-shared/src/url/url.rs +++ b/yazi-shared/src/url/url.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, ffi::OsStr, fmt::{Debug, Formatter}, path::Path}; use hashbrown::Equivalent; -use crate::{loc::{Loc, LocBuf}, url::{Components, Encode, Scheme, Uri, UrlBuf, Urn, UrnBuf}}; +use crate::{loc::{Loc, LocBuf}, url::{Components, Encode, Scheme, Uri, UrlBuf, Urn}}; #[derive(Clone, Eq, Hash, PartialEq)] pub struct Url<'a> { @@ -121,7 +121,7 @@ impl<'a> Url<'a> { }) } - pub fn parent_url(&self) -> Option { + pub fn parent(&self) -> Option { use Scheme as S; let parent = self.loc.parent()?; @@ -177,9 +177,7 @@ impl<'a> Url<'a> { } #[inline] - pub fn pair(&self) -> Option<(Url<'a>, UrnBuf)> { - Some((self.parent_url()?, self.loc.urn().to_owned())) - } + pub fn pair(&self) -> Option<(Url<'a>, &'a Urn)> { Some((self.parent()?, self.loc.urn())) } #[inline] pub fn as_path(&self) -> Option<&'a Path> { diff --git a/yazi-watcher/src/backend/backend.rs b/yazi-watcher/src/backend/backend.rs index b43da5d3..f71715e3 100644 --- a/yazi-watcher/src/backend/backend.rs +++ b/yazi-watcher/src/backend/backend.rs @@ -51,7 +51,7 @@ impl Backend { { let (mut todo, watched) = (HashSet::new(), WATCHED.read()); for url in urls.into_iter().map(Into::into) { - let Some(parent) = url.parent_url() else { continue }; + let Some(parent) = url.parent() else { continue }; if todo.contains(&parent) { todo.insert(url); } else if watched.contains(&parent) diff --git a/yazi-watcher/src/watcher.rs b/yazi-watcher/src/watcher.rs index 7a586bce..3fc77778 100644 --- a/yazi-watcher/src/watcher.rs +++ b/yazi-watcher/src/watcher.rs @@ -61,18 +61,18 @@ impl Watcher { for u in urls { let Some((parent, urn)) = u.pair() else { continue }; let Ok(file) = File::new(&u).await else { - ops.push(FilesOp::Deleting(parent.into(), [urn].into())); + ops.push(FilesOp::Deleting(parent.into(), [urn.into()].into())); continue; }; if let Some(p) = file.url.as_path() && !local::must_case_match(p).await { - ops.push(FilesOp::Deleting(parent.into(), [urn].into())); + ops.push(FilesOp::Deleting(parent.into(), [urn.into()].into())); continue; } - ops.push(FilesOp::Upserting(parent.into(), [(urn, file)].into())); + ops.push(FilesOp::Upserting(parent.into(), [(urn.into(), file)].into())); } FilesOp::mutate(ops);