Trait StorageBackend

Source
pub trait StorageBackend<K, V>
where K: Eq + Hash + Clone + Serialize + DeserializeOwned + Send + Sync + 'static, V: Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
{ // Required methods fn load_all<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<HashMap<K, V>, PersistentError>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn save<'life0, 'async_trait>( &'life0 self, key: K, value: V, ) -> Pin<Box<dyn Future<Output = Result<(), PersistentError>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn delete<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 K, ) -> Pin<Box<dyn Future<Output = Result<(), PersistentError>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; // Provided method fn flush<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), PersistentError>> + Send + 'async_trait>> where Self: Sync + 'async_trait, 'life0: 'async_trait { ... } }
Expand description

A trait for implementing storage backends for PersistentMap.

This trait defines the interface that all storage backends must implement. It provides methods for loading, saving, and deleting key-value pairs.

§Type Parameters

  • K: The key type, which must be hashable, serializable, and cloneable
  • V: The value type, which must be serializable and cloneable

§Examples

Implementing a custom backend:

use persistent_map::{StorageBackend, PersistentError, Result};
use std::collections::HashMap;
use serde::{Serialize, de::DeserializeOwned};
use std::hash::Hash;

struct MyCustomBackend {
    // Your backend-specific fields
}

#[async_trait::async_trait]
impl<K, V> StorageBackend<K, V> for MyCustomBackend
where
    K: Eq + Hash + Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
    V: Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
{
    async fn load_all(&self) -> Result<HashMap<K, V>, PersistentError> {
        // Implementation for loading all key-value pairs
    }

    async fn save(&self, key: K, value: V) -> Result<(), PersistentError> {
        // Implementation for saving a key-value pair
    }

    async fn delete(&self, key: &K) -> Result<(), PersistentError> {
        // Implementation for deleting a key-value pair
    }
}

Required Methods§

Source

fn load_all<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<HashMap<K, V>, PersistentError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Load all key-value pairs from the storage backend.

This method is called when initializing a PersistentMap to populate the in-memory map with existing data.

Source

fn save<'life0, 'async_trait>( &'life0 self, key: K, value: V, ) -> Pin<Box<dyn Future<Output = Result<(), PersistentError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Save a key-value pair to the storage backend.

This method is called whenever a key-value pair is inserted into the map.

Source

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 K, ) -> Pin<Box<dyn Future<Output = Result<(), PersistentError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete a key-value pair from the storage backend.

This method is called whenever a key-value pair is removed from the map.

Provided Methods§

Source

fn flush<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), PersistentError>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Flush any buffered writes to the storage backend.

This method is optional and has a default implementation that does nothing. Backends that buffer writes should override this method to ensure data is persisted.

Implementors§

Source§

impl<K, V> StorageBackend<K, V> for InMemoryBackend
where K: Eq + Hash + Clone + Serialize + DeserializeOwned + Send + Sync + 'static, V: Clone + Serialize + DeserializeOwned + Send + Sync + 'static,

Source§

impl<K, V> StorageBackend<K, V> for SqliteBackend
where K: Eq + Hash + Clone + Serialize + DeserializeOwned + Send + Sync + 'static + ToString + FromStr, <K as FromStr>::Err: Error + Send + Sync + 'static, V: Clone + Serialize + DeserializeOwned + Send + Sync + 'static,

Implementation of the StorageBackend trait for SqliteBackend.

This implementation provides methods for loading, saving, and deleting key-value pairs from a SQLite database.