waynr commented on code in PR #7160:
URL: https://github.com/apache/arrow-rs/pull/7160#discussion_r1964728359
##########
object_store/src/extensions.rs:
##########
@@ -0,0 +1,252 @@
+use std::{
+ any::{Any, TypeId},
+ collections::HashMap,
+ sync::Arc,
+};
+
+/// Trait that must be implemented by extensions.
+pub trait Extension: std::fmt::Debug + Send + Sync {
+ /// Return a &Any for this type.
+ fn as_any(self: Arc<Self>) -> Ext;
+
+ /// Ensure that [`Extensions`] can implement [`std::cmp::PartialEq`] by
requiring [`Extension`]
+ /// implementors to implement a dyn-compatible partial equality operation.
+ ///
+ /// This is necessary because [`std::cmp::PartialEq`] uses a `Self` type
parameter, which
+ /// violates dyn-compatibility rules:
+ ///
https://doc.rust-lang.org/error_codes/E0038.html#trait-uses-self-as-a-type-parameter-in-the-supertrait-listing
+ fn partial_eq(self: Arc<Self>, other: Ext) -> bool;
+}
+
+type ExtensionMap = HashMap<TypeId, Arc<dyn Extension>>;
+type Ext = Arc<dyn Any + Send + Sync + 'static>;
+
+/// Type that holds opaque instances of types referenced by their
[`std::any::TypeId`].
+///
+/// Types are stored as instances of the [`Extension`] trait in order to
enable implementation of
+/// [`std::fmt::Debug`] and [`std::cmp::PartialEq`].
+#[derive(Debug, Default, Clone)]
+pub struct Extensions {
+ inner: ExtensionMap,
+}
+
+impl PartialEq for Extensions {
+ fn eq(&self, other: &Self) -> bool {
+ for (k, v) in &self.inner {
+ if let Some(ov) = other.inner.get(&k) {
+ if !v.clone().partial_eq(ov.clone().as_any()) {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ true
+ }
+}
+
+impl Extensions {
+ pub(crate) fn set_ext<T: Extension + 'static>(&mut self, t: T) {
+ let id = t.type_id();
+ let a = Arc::new(t);
+ self.inner.insert(id, a);
+ }
+
+ pub(crate) fn get_ext<T: Any + Send + Sync + 'static>(&self) ->
Option<Arc<T>> {
+ let id = TypeId::of::<T>();
+ self.inner.get(&id).map(|e| {
+ let v = Arc::clone(&e).as_any();
+ Arc::downcast::<T>(v).unwrap()
+ })
+ }
+}
+
+impl From<HashMap<TypeId, Ext>> for Extensions {
+ fn from(other: HashMap<TypeId, Ext>) -> Self {
+ let mut s = Self::default();
+ for (k, v) in other {
+ let v = Arc::new(ExtWrapper { inner: v });
+ s.inner.insert(k, v);
+ }
+ s
+ }
+}
+
+/// Type that implements [`Extension`] for the sake of converting to
[`Extensions`] from external
+/// instances of `HashMap<TypeId, Ext>'.
+///
+/// NOTE: Different instances of the same type held by ExtWrapper are
considered equal for the sake
+/// of the `Extensions.partial_eq` trait since its intended use case is for
wrapping dyn-compatible
+/// trait objects; because [`std::cmp::PartialEq`] has `Self` as a generic
type parameter, the type
+/// system won't let us set it as a trait bound on incoming trait objects when
constructing
+/// ExtWrapper.
+struct ExtWrapper {
+ inner: Arc<dyn Any + Send + Sync + 'static>,
+}
+
+impl std::fmt::Debug for ExtWrapper {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "ExtWrapper2")
+ }
+}
+
+impl Extension for ExtWrapper {
+ fn as_any(self: Arc<Self>) -> Ext {
+ self.inner.clone()
+ }
+
+ fn partial_eq(self: Arc<Self>, _: Ext) -> bool {
+ // this is necessary because ExtWrapper is a generic impl of
Extensions necessary for
+ // converting from external Arc<dyn Any ...> types where none of the
trait bounts can
+ // implement PartialEq due to dyn-compatibility requirements.
+ true
Review Comment:
@crepererum I don't know if this violates the spirit of your preference for
making `Extension` trait implementations also implement `PartialEq` and
`Debug`, but this was the best I could come up with that would allow me to
build a `Extensions` instance from a `HashMap<TypeId, Arc<dyn Any + Send + Sync
+ 'static>>`. I should have a PR up shortly demonstrating the use of this in
datafusion.
--
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]