shared_files/
traits.rs

1//! Contains public traits.
2
3use std::path::PathBuf;
4use tokio::io::{AsyncRead, AsyncWrite};
5
6/// Trait for types used as a file storage backend.
7#[async_trait::async_trait]
8pub trait SharedFileType: AsyncRead + AsyncWrite {
9    /// The type created when producing a reader or writer. Typically `Self`.
10    type Type;
11
12    /// The error type.
13    type OpenError;
14
15    /// The error type.
16    type SyncError;
17
18    /// Opens a new [`Type`](Self::Type) instance in read-only mode.
19    async fn open_ro(&self) -> Result<Self::Type, Self::OpenError>;
20
21    /// Opens a new [`Type`](Self::Type) instance in read-write mode.
22    async fn open_rw(&self) -> Result<Self::Type, Self::OpenError>;
23
24    /// Synchronizes data and metadata with the underlying buffer.
25    async fn sync_all(&self) -> Result<(), Self::SyncError>;
26
27    /// Synchronizes data with the underlying buffer.
28    async fn sync_data(&self) -> Result<(), Self::SyncError>;
29}
30
31/// Trait for types that can be newly constructed asynchronously.
32#[async_trait::async_trait]
33pub trait AsyncNewFile {
34    /// The type created on success.
35    type Target;
36    /// The error type.
37    type Error;
38
39    /// Creates a new instance of the type [`Target`](AsyncNewFile::Target).
40    async fn new_async() -> Result<Self::Target, Self::Error>;
41}
42
43/// Trait for types that can be newly constructed asynchronously.
44pub trait NewFile {
45    /// The type created on success.
46    type Target;
47    /// The error type.
48    type Error;
49
50    /// Creates a new instance of the type [`Target`](AsyncNewFile::Target).
51    fn new() -> Result<Self::Target, Self::Error>;
52}
53
54/// Trait for types that can synchronously determine the file path.
55pub trait FilePath {
56    /// Obtains the path of the temporary file.
57    fn file_path(&self) -> &PathBuf;
58}