Favour a default function instead of StorageExt

This commit is contained in:
Hirrolot 2022-03-21 18:09:36 +06:00
parent c6f0cd9404
commit 0b6e7acf9f
3 changed files with 11 additions and 19 deletions

View file

@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- The `StorageExt` trait with a single function `erase` that returns `ErasedStorage`.
- The `Storage::erase` default function that returns `ErasedStorage`.
- `ErasedStorage`, a storage with an erased error type.
## 0.7.1 - 2022-03-09

View file

@ -72,23 +72,13 @@ pub trait Storage<D> {
self: Arc<Self>,
chat_id: i64,
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>;
}
/// A storage with an erased error type.
pub type ErasedStorage<D> = Arc<dyn Storage<D, Error = Box<dyn std::error::Error>>>;
/// Extension methods for working with [`Storage`].
pub trait StorageExt<D> {
/// Returns [`ErasedStorage`].
fn erase(self: Arc<Self>) -> ErasedStorage<D>;
}
impl<S, D> StorageExt<D> for S
where
S: Storage<D> + Send + Sync + 'static,
S::Error: std::error::Error + 'static,
{
fn erase(self: Arc<Self>) -> ErasedStorage<D> {
/// Erases [`Self::Error`] to [`std::error::Error`].
fn erase(self: Arc<Self>) -> ErasedStorage<D>
where
Self: Sized + Send + Sync + 'static,
Self::Error: std::error::Error + 'static,
{
struct Eraser<S>(Arc<S>);
impl<D, S> Storage<D> for Eraser<S>
@ -140,6 +130,9 @@ where
}
}
/// A storage with an erased error type.
pub type ErasedStorage<D> = Arc<dyn Storage<D, Error = Box<dyn std::error::Error>>>;
#[cfg(test)]
mod tests {
use super::*;

View file

@ -92,8 +92,7 @@ pub use crate::dispatching::dialogue::{RedisStorage, RedisStorageError};
pub use crate::dispatching::dialogue::{SqliteStorage, SqliteStorageError};
pub use crate::dispatching::dialogue::{
serializer, ErasedStorage, InMemStorage, InMemStorageError, Serializer, Storage, StorageExt,
TraceStorage,
serializer, ErasedStorage, InMemStorage, InMemStorageError, Serializer, Storage, TraceStorage,
};
pub use get_chat_id::GetChatId;