refactor: impl Sharder for Arc-wrapped Sharder
Allows any Sharder wrapped in an Arc to be used as a Sharder impl,
allowing the same Sharder instance to be shared across independent code
modules.
use data_types::DatabaseName;
-use std::fmt::Debug;
+use std::{fmt::Debug, sync::Arc};
/// A [`Sharder`] implementation is responsible for mapping an opaque payload
/// for a given table name & namespace to an output type.
@@ -21,3 +21,35 @@ pub trait Sharder<P>: Debug + Send + Sync {
/// Map the specified `payload` to a shard.
fn shard(&self, table: &str, namespace: &DatabaseName<'_>, payload: &P) -> Self::Item;
}
+
+impl<T, P> Sharder<P> for Arc<T>
+where
+ T: Sharder<P>,
+{
+ type Item = T::Item;
+
+ fn shard(&self, table: &str, namespace: &DatabaseName<'_>, payload: &P) -> Self::Item {
+ (**self).shard(table, namespace, payload)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use mutable_batch::MutableBatch;
+
+ use crate::JumpHash;
+
+ use super::*;
+
+ #[test]
+ fn test_arc_wrapped_sharder() {
+ let hasher: Arc<dyn Sharder<MutableBatch, Item = Arc<u32>>> =
+ Arc::new(JumpHash::new((0..10_u32).map(Arc::new)));
+
+ let _ = hasher.shard(
+ "table",
+ &DatabaseName::try_from("namespace").unwrap(),
+ &MutableBatch::default(),
+ );
+ }
+}
