dir_structure/traits/
asy.rs

1//! Asynchronous reading / writing traits.
2
3use std::future;
4use std::future::Future;
5use std::pin::Pin;
6
7use crate::error::VfsResult;
8use crate::traits::async_vfs::VfsAsync;
9use crate::traits::async_vfs::WriteSupportingVfsAsync;
10use crate::traits::vfs::PathType;
11use crate::traits::vfs::VfsCore;
12
13/// Trait for types / structures that can be read from disk asynchronously.
14///
15/// `async` version of [`ReadFrom`](crate::traits::sync::ReadFrom).
16#[cfg(feature = "async")]
17#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
18pub trait ReadFromAsync<'a, Vfs: VfsAsync + ?Sized + 'a>: Sized + 'a {
19    /// The future type returned by the async read function.
20    type Future: Future<Output = VfsResult<Self, Vfs>> + Send + Unpin + 'a
21    where
22        Self: 'a;
23
24    /// Asynchronously reads the structure from the specified path,
25    /// which can be either a file or a directory.
26    fn read_from_async(
27        path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath,
28        vfs: Pin<&'a Vfs>,
29    ) -> Self::Future;
30}
31
32/// Trait for types / structures that can be written to disk asynchronously.
33///
34/// The difference between this and [`WriteToAsyncRef`] is that this trait takes in
35/// owned data instead of a reference.
36#[cfg(feature = "async")]
37#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
38pub trait WriteToAsync<'a, Vfs: WriteSupportingVfsAsync + ?Sized + 'a> {
39    /// The future type returned by the async write function.
40    type Future: Future<Output = VfsResult<(), Vfs>> + Send + Unpin + 'a;
41
42    /// Asynchronously writes the structure to the specified path.
43    fn write_to_async(
44        self,
45        path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath,
46        vfs: Pin<&'a Vfs>,
47    ) -> Self::Future;
48}
49
50/// Trait for types / structures that can be written to disk asynchronously.
51///
52/// The difference between this and [`WriteToAsync`] is that this trait takes in
53/// a reference instead of owned data.
54///
55/// This is an async equivalent of [`WriteTo`](crate::traits::sync::WriteTo).
56#[cfg(feature = "async")]
57#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
58pub trait WriteToAsyncRef<'r, Vfs: WriteSupportingVfsAsync + ?Sized + 'r> {
59    /// The future type returned by the async write function.
60    type Future<'a>: Future<Output = VfsResult<(), Vfs>> + Send + Unpin + 'a
61    where
62        Self: 'a,
63        'r: 'a,
64        Vfs: 'a;
65
66    /// Asynchronously writes the structure to the specified path.
67    fn write_to_async_ref<'a>(
68        &'a self,
69        path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath,
70        vfs: Pin<&'a Vfs>,
71    ) -> Self::Future<'a>
72    where
73        'r: 'a;
74}
75
76/// Async equivalent of [`FromRefForWriter`](crate::traits::sync::FromRefForWriter).
77#[cfg(feature = "async")]
78#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
79pub trait FromRefForWriterAsync<'a, Vfs: WriteSupportingVfsAsync + ?Sized + 'a> {
80    /// The inner type to cast.
81    type Inner: ?Sized;
82    /// The reference type to cast to.
83    type Wr: WriteToAsync<'a, Vfs> + 'a;
84
85    /// Casts the reference to the inner type to a [`WriteToAsync`]
86    /// reference type.
87    fn from_ref_for_writer_async(value: &'a Self::Inner) -> Self::Wr;
88}
89
90#[cfg(feature = "async")]
91#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
92impl<'a, Vfs: VfsAsync + ?Sized + 'a> ReadFromAsync<'a, Vfs> for () {
93    type Future = future::Ready<VfsResult<Self, Vfs>>;
94
95    fn read_from_async(
96        _path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath,
97        _vfs: Pin<&'a Vfs>,
98    ) -> Self::Future {
99        future::ready(Ok(()))
100    }
101}
102
103#[cfg(feature = "async")]
104#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
105impl<'a, Vfs: WriteSupportingVfsAsync + ?Sized + 'a> WriteToAsync<'a, Vfs> for () {
106    type Future = future::Ready<VfsResult<(), Vfs>>;
107
108    fn write_to_async(
109        self,
110        _path: <<Vfs as VfsCore>::Path as PathType>::OwnedPath,
111        _vfs: Pin<&'a Vfs>,
112    ) -> Self::Future {
113        future::ready(Ok(()))
114    }
115}