From e53d772668d92f4c0316b42c6c97586b2556bc1e Mon Sep 17 00:00:00 2001 From: KiSetsuFu-PuLiN Date: Tue, 22 Sep 2026 01:15:08 +0800 Subject: [PATCH] feat: implement WriteToAsyncRef for () --- dir-structure-tools/tests/async_tests.rs | 22 ++++++++++++++++++++++ dir-structure/src/traits/asy.rs | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/dir-structure-tools/tests/async_tests.rs b/dir-structure-tools/tests/async_tests.rs index 1029ea4..d0274fc 100644 --- a/dir-structure-tools/tests/async_tests.rs +++ b/dir-structure-tools/tests/async_tests.rs @@ -121,6 +121,28 @@ async fn deferred_read_or_own_write_to_async_ref() { assert_eq!(fs::read_to_string(d.join("out2.txt")).unwrap(), "f2"); } +/// `()` writes as a no-op through `WriteToAsyncRef`, so `DirChildren<(), _>` can be +/// used as a placeholder entry type that satisfies the write bound without real data. +#[tokio::test] +async fn unit_write_to_async_ref_is_no_op() { + use dir_structure::traits::vfs::VfsCore; + use dir_structure_tools::NoFilter; + + let vfs = TokioFsVfs; + + let p = test_dir("unit_write_to_async_ref_is_no_op"); + ().write_to_async_ref(p.join("noop.txt"), Pin::new(&vfs)) + .await + .unwrap(); + assert!(!p.join("noop.txt").exists()); + + let skeleton: DirChildren<(), NoFilter, ::Path> = DirChildren::default(); + skeleton + .write_to_async_ref(p.join("dir"), Pin::new(&vfs)) + .await + .unwrap(); +} + #[tokio::test] async fn read_all_directory_files() { #[derive(dir_structure::DirStructureAsync)] diff --git a/dir-structure/src/traits/asy.rs b/dir-structure/src/traits/asy.rs index 062d52f..50fb76e 100644 --- a/dir-structure/src/traits/asy.rs +++ b/dir-structure/src/traits/asy.rs @@ -113,3 +113,25 @@ impl<'a, Vfs: WriteSupportingVfsAsync + ?Sized + 'a> WriteToAsync<'a, Vfs> for ( future::ready(Ok(())) } } + +#[cfg(feature = "async")] +#[cfg_attr(docsrs, doc(cfg(feature = "async")))] +impl<'a, Vfs: WriteSupportingVfsAsync + ?Sized + 'a> WriteToAsyncRef<'a, Vfs> for () { + type Future<'b> + = future::Ready> + where + Self: 'b, + 'a: 'b, + Vfs: 'b; + + fn write_to_async_ref<'b>( + &'b self, + _path: <::Path as PathType>::OwnedPath, + _vfs: Pin<&'b Vfs>, + ) -> Self::Future<'b> + where + 'a: 'b, + { + future::ready(Ok(())) + } +}