1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Storage backends.

use std::io::{Read, Write};

use uuid::Uuid;

#[cfg(feature = "directory")]
pub use directory::*;
pub use error::*;
pub use memory::*;

#[cfg(feature = "directory")]
mod directory;
mod error;
mod memory;

/// Bytes storage backend.
///
/// Each entry is identified by a unique [id](https://docs.rs/uuid/*/uuid/struct.Uuid.html).
/// Storage implementations may choose to lock entries while they are read or written to.
pub trait Storage {
    /// Reader.
    type Read: Read;
    /// Writer.
    type Write: Write;
    /// Iterator.
    type Iterator: Iterator<Item=Result<Uuid>>;

    /// Create a new entry in this storage, returning his id and a writer.
    ///
    /// Returned id is guaranteed to be unique (inside this specific instance of storage, that is).
    fn new(&mut self) -> Result<(Uuid, Self::Write)>;

    /// Provide a reader to an entry, if it exists.
    fn read(&self, entry: Uuid) -> Result<Self::Read>;

    /// Provide a writer to an entry. If it exists, this writer overrides (and truncates) the bytes.
    /// If it doesn't exists, a new entry is created.
    fn write(&mut self, entry: Uuid) -> Result<Self::Write>;

    /// Provide a writer to an entry, only if it exists. This writer override (and truncates) the
    /// bytes.
    fn overwrite(&mut self, entry: Uuid) -> Result<Self::Write>;

    /// Delete an entry.
    ///
    /// This does not fail if the entry doesn't exists. Instead, this returns a boolean : if true,
    /// the entry was deleted, and if false, the entry was not found.
    fn delete(&mut self, entry: Uuid) -> Result<bool>;

    /// Empties this storage.
    ///
    /// Everything in this storage will be deleted. Use at your own risks.
    ///
    /// This does not fail if the storage was already empty.
    fn clear(&mut self) -> Result<()>;

    /// Provide an iterator listing the entries currently in this storage.
    ///
    /// The returned iterator is lazy and doesn't lock anything, meaning returned entries might not
    /// even exist when consumed.
    fn iter(&self) -> Result<Self::Iterator>;
}