waynr commented on code in PR #7160:
URL: https://github.com/apache/arrow-rs/pull/7160#discussion_r1964569162
##########
object_store/src/extensions.rs:
##########
@@ -0,0 +1,35 @@
+use std::{
+ any::{Any, TypeId},
+ collections::HashMap,
+ sync::Arc,
+};
+
+type ExtensionMap = HashMap<TypeId, Ext>;
+type Ext = Arc<dyn Any + Send + Sync + 'static>;
+
+#[derive(Debug, Default, Clone)]
+pub struct Extensions {
+ inner: ExtensionMap,
+}
+
+impl Extensions {
+ fn set_ext<T: Send + Sync + 'static>(&mut self, t: T) {
+ let id = t.type_id();
+ let a = Arc::new(t);
+ self.inner.insert(id, a);
+ }
+
+ fn get_ext<T: Send + Sync + 'static>(&self) -> Option<Arc<T>> {
+ let id = TypeId::of::<T>();
+ self.inner
+ .get(&id)
+ .cloned()
+ .map(|e| Arc::downcast(e).expect("TypeId is always unique"))
+ }
+}
+
+impl From<HashMap<TypeId, Ext>> for Extensions {
Review Comment:
> I would prefer if we don't use Any directly for the extensions but a
proper trait. This also allows you to properly implement Debug and even
PartialEq:
@crepererum I've been attempting to make this work, but am having trouble
reconciling the need to convert from the extensions stored on the [datafusion
`SessionConfig`](https://docs.rs/datafusion/latest/datafusion/execution/config/struct.SessionConfig.html)
type to this type for the sake of integrating the changes in this PR into
datafusion.
I've got a set of changes locally that take your suggested approach of
storing a `HashMap<TypeId, Arc<dyn Extension>>`, but when I write tests to
validate conversion from the `HashMap<TypeId, Arc<dyn Any + Send + Sync +
'static>>` used by datafusions session config, I'm able to get it to compile
but I'm not able to extract instances of a given type using the
`Extensions.get_ext<T>(&self)` method.
You say you have a preference for this approach, but I'm wondering just how
strong that preference is. In https://github.com/apache/arrow-rs/pull/7152 your
rationale is that you want `PartialEq` and `Eq` for testing -- is that all?
Does it really matter for testing purpose if the `Extensions` type is included
in comparisons?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]