chore: Add Clone impl for StoreConfig

… by storing the stores inside Arc's instead of Box'es.
This commit is contained in:
Jonas Platte
2022-06-17 12:42:32 +02:00
committed by Jonas Platte
parent a4e4bfe833
commit 00a20f325b
4 changed files with 18 additions and 17 deletions

View File

@@ -111,7 +111,7 @@ impl BaseClient {
let store = config.state_store.map(Store::new).unwrap_or_else(Store::open_memory_store);
#[cfg(feature = "e2e-encryption")]
let crypto_store =
config.crypto_store.unwrap_or_else(|| Box::new(MemoryCryptoStore::default())).into();
config.crypto_store.unwrap_or_else(|| Arc::new(MemoryCryptoStore::default()));
BaseClient {
store,

View File

@@ -32,6 +32,10 @@ macro_rules! statestore_integration_tests {
($($name:ident)*) => {
$(
mod $name {
use std::{
collections::{BTreeMap, BTreeSet},
sync::Arc,
};
#[cfg(feature = "experimental-timeline")]
use futures_util::StreamExt;
@@ -73,8 +77,6 @@ macro_rules! statestore_integration_tests {
};
use serde_json::{json, Value as JsonValue};
use std::collections::{BTreeMap, BTreeSet};
#[cfg(feature = "experimental-timeline")]
use $crate::{
http::Response,
@@ -93,7 +95,6 @@ macro_rules! statestore_integration_tests {
use super::get_store;
fn user_id() -> &'static UserId {
user_id!("@example:localhost")
}
@@ -114,7 +115,7 @@ macro_rules! statestore_integration_tests {
}
/// Populate the given `StateStore`.
pub(crate) async fn populated_store(inner: Box<dyn StateStore>) -> StoreResult<Store> {
pub(crate) async fn populated_store(inner: Arc<dyn StateStore>) -> StoreResult<Store> {
let mut changes = StateChanges::default();
let store = Store::new(inner);
@@ -304,7 +305,7 @@ macro_rules! statestore_integration_tests {
let user_id = user_id();
let inner_store = get_store().await?;
let store = populated_store(Box::new(inner_store)).await?;
let store = populated_store(Arc::new(inner_store)).await?;
assert!(store.get_sync_token().await?.is_some());
assert!(store.get_presence_event(user_id).await?.is_some());
@@ -581,7 +582,7 @@ macro_rules! statestore_integration_tests {
async fn test_persist_invited_room() -> StoreResult<()> {
let stripped_room_id = stripped_room_id();
let inner_store = get_store().await?;
let store = populated_store(Box::new(inner_store)).await?;
let store = populated_store(Arc::new(inner_store)).await?;
assert_eq!(store.get_stripped_room_infos().await?.len(), 1);
assert!(store.get_stripped_room(stripped_room_id).is_some());
@@ -597,7 +598,7 @@ macro_rules! statestore_integration_tests {
let inner_store = get_store().await?;
let stripped_room_id = stripped_room_id();
let store = populated_store(Box::new(inner_store)).await?;
let store = populated_store(Arc::new(inner_store)).await?;
store.remove_room(room_id).await?;

View File

@@ -392,7 +392,7 @@ pub struct Store {
impl Store {
/// Create a new Store with the default `MemoryStore`
pub fn open_memory_store() -> Self {
let inner = Box::new(MemoryStore::new());
let inner = Arc::new(MemoryStore::new());
Self::new(inner)
}
@@ -400,9 +400,9 @@ impl Store {
impl Store {
/// Create a new store, wrappning the given `StateStore`
pub fn new(inner: Box<dyn StateStore>) -> Self {
pub fn new(inner: Arc<dyn StateStore>) -> Self {
Self {
inner: inner.into(),
inner,
session: Default::default(),
sync_token: Default::default(),
rooms: Default::default(),
@@ -651,11 +651,11 @@ impl StateChanges {
///
/// let store_config = StoreConfig::new();
/// ```
#[derive(Default)]
#[derive(Clone, Default)]
pub struct StoreConfig {
#[cfg(feature = "e2e-encryption")]
pub(crate) crypto_store: Option<Box<dyn CryptoStore>>,
pub(crate) state_store: Option<Box<dyn StateStore>>,
pub(crate) crypto_store: Option<Arc<dyn CryptoStore>>,
pub(crate) state_store: Option<Arc<dyn StateStore>>,
}
#[cfg(not(tarpaulin_include))]
@@ -677,13 +677,13 @@ impl StoreConfig {
/// The crypto store must be opened before being set.
#[cfg(feature = "e2e-encryption")]
pub fn crypto_store(mut self, store: impl CryptoStore + 'static) -> Self {
self.crypto_store = Some(Box::new(store));
self.crypto_store = Some(Arc::new(store));
self
}
/// Set a custom implementation of a `StateStore`.
pub fn state_store(mut self, store: impl StateStore + 'static) -> Self {
self.state_store = Some(Box::new(store));
self.state_store = Some(Arc::new(store));
self
}
}

View File

@@ -200,7 +200,7 @@ impl Printer {
impl Inspector {
fn new(database_path: &str, json: bool, color: bool) -> Self {
let printer = Printer::new(json, color);
let store = Store::new(Box::new(
let store = Store::new(Arc::new(
StateStore::open_with_path(database_path).expect("Can't open sled database"),
));