Skip to content

Commit ed3816a

Browse files
committed
test: add test for impl From<HashMap> for Extensions, fix bug in ExtWrapper
1 parent c273220 commit ed3816a

File tree

2 files changed

+58
-68
lines changed

2 files changed

+58
-68
lines changed

object_store/src/extensions.rs

+56-65
Original file line numberDiff line numberDiff line change
@@ -53,50 +53,11 @@ impl Extensions {
5353
self.inner.insert(id, a);
5454
}
5555

56-
fn set_ext_wrapped<T: Any + Send + Sync + std::fmt::Debug + 'static>(&mut self, t: T) {
57-
let id = TypeId::of::<T>();
58-
//println!("inserting: {id:?}: {}", std::any::type_name::<T>());
59-
let a = Arc::new(ExtWrapper::new(t));
60-
self.inner.insert(id, a);
61-
}
62-
6356
pub(crate) fn get_ext<T: Any + Send + Sync + 'static>(&self) -> Option<Arc<T>> {
6457
let id = TypeId::of::<T>();
65-
// println!(
66-
// "looking for : {id:?}: {}",
67-
// std::any::type_name::<T>()
68-
// );
6958
self.inner.get(&id).map(|e| {
70-
// let id = e.type_id();
71-
// println!(
72-
// "found value : {id:?}: {}",
73-
// std::any::type_name_of_val(e)
74-
// );
75-
let v = Arc::clone(&e);
76-
// let id = v.type_id();
77-
// println!(
78-
// "cloned value : {id:?}: {}",
79-
// std::any::type_name_of_val(&v)
80-
// );
81-
let v = v.as_any();
82-
// let id = v.type_id();
83-
// println!(
84-
// "as_any value : {id:?}: {}",
85-
// std::any::type_name_of_val(&v)
86-
// );
87-
let v: Arc<T> =
88-
Arc::downcast::<T>(v).expect("must be able to downcast to type if found in map");
89-
// let id = v.type_id();
90-
// println!(
91-
// "found value : {id:?}: {}",
92-
// std::any::type_name_of_val(&v)
93-
// );
94-
// let id = v.as_ref().type_id();
95-
// println!(
96-
// "found value (inner): {id:?}: {}",
97-
// std::any::type_name_of_val(v.as_ref())
98-
// );
99-
v
59+
let v = Arc::clone(&e).as_any();
60+
Arc::downcast::<T>(v).unwrap()
10061
})
10162
}
10263
}
@@ -105,7 +66,7 @@ impl From<HashMap<TypeId, Ext>> for Extensions {
10566
fn from(other: HashMap<TypeId, Ext>) -> Self {
10667
let mut s = Self::default();
10768
for (k, v) in other {
108-
let v = Arc::new(ExtWrapper::new(v));
69+
let v = Arc::new(ExtWrapper { inner: v });
10970
s.inner.insert(k, v);
11071
}
11172
s
@@ -120,18 +81,17 @@ impl From<HashMap<TypeId, Ext>> for Extensions {
12081
/// trait objects; because [`std::cmp::PartialEq`] has `Self` as a generic type parameter, the type
12182
/// system won't let us set it as a trait bound on incoming trait objects when constructing
12283
/// ExtWrapper.
123-
#[derive(Debug)]
124-
struct ExtWrapper<T> {
125-
inner: Arc<T>,
84+
struct ExtWrapper {
85+
inner: Arc<dyn Any + Send + Sync + 'static>,
12686
}
12787

128-
impl<T: std::fmt::Debug + Send + Sync> ExtWrapper<T> {
129-
fn new(v: T) -> Self {
130-
Self { inner: Arc::new(v) }
88+
impl std::fmt::Debug for ExtWrapper {
89+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90+
write!(f, "ExtWrapper2")
13191
}
13292
}
13393

134-
impl<T: std::fmt::Debug + Send + Sync + 'static> Extension for ExtWrapper<T> {
94+
impl Extension for ExtWrapper {
13595
fn as_any(self: Arc<Self>) -> Ext {
13696
self.inner.clone()
13797
}
@@ -159,20 +119,8 @@ mod test {
159119
}
160120

161121
fn partial_eq(self: Arc<Self>, other: Ext) -> bool {
162-
let self_id = self.type_id();
163-
let other_id = other.type_id();
164-
println!("MyExt.partial_eq");
165-
println!(
166-
" self : {self_id:?}, {}",
167-
std::any::type_name_of_val(self.as_ref())
168-
);
169-
println!(
170-
" other : {other_id:?}, {}",
171-
std::any::type_name_of_val(other.as_ref())
172-
);
173122
other
174123
.downcast_ref::<Self>()
175-
.inspect(|v| println!("{v:?}"))
176124
.map(|other| self.x == other.x)
177125
.unwrap_or_default()
178126
}
@@ -183,8 +131,6 @@ mod test {
183131
let mut exts1 = Extensions::default();
184132
let myext1 = MyExt { x: 0 };
185133
exts1.set_ext(myext1);
186-
let t1 = TypeId::of::<MyExt>();
187-
println!("type id of MyExt: {t1:?}");
188134

189135
let mut exts2 = Extensions::default();
190136
let myext2 = MyExt { x: 1 };
@@ -205,6 +151,28 @@ mod test {
205151
);
206152
}
207153

154+
impl Extensions {
155+
fn set_ext_wrapped<T: Any + Send + Sync + std::fmt::Debug + 'static>(&mut self, t: T) {
156+
let id = TypeId::of::<T>();
157+
let a = Arc::new(ExtWrapper { inner: Arc::new(t) });
158+
self.inner.insert(id, a);
159+
}
160+
}
161+
162+
#[test]
163+
fn not_equal_if_missing_entry() {
164+
let mut exts1 = Extensions::default();
165+
exts1.set_ext_wrapped(0);
166+
exts1.set_ext_wrapped(String::from("meow"));
167+
168+
let mut exts2 = Extensions::default();
169+
exts2.set_ext_wrapped(1);
170+
171+
assert_ne!(
172+
exts1, exts2,
173+
"two different Extensions cannot be equal if they don't carry the same types"
174+
);
175+
}
208176
#[test]
209177
fn equality_ext_wrapper() {
210178
let mut exts1 = Extensions::default();
@@ -248,14 +216,37 @@ mod test {
248216
fn one_instance_of_same_type_ext_wrapper() {
249217
let mut exts = Extensions::default();
250218

251-
println!("validate ExtWrapper<i32> with value 0");
219+
println!("validate ExtWrapper with value 0");
252220
exts.set_ext_wrapped(0i32);
253221
let expected = exts.get_ext::<i32>().expect("must get a value");
254222
assert_eq!(0, *expected, "return the same instance we just added");
255223

256-
println!("validate replacing previous ExtWrapper<i32> with value 1");
224+
println!("validate replacing previous ExtWrapper with value 1");
257225
exts.set_ext_wrapped(1i32);
258226
let expected = exts.get_ext::<i32>().expect("must get a value");
259227
assert_eq!(1, *expected, "return the same instance we just added");
260228
}
229+
230+
#[test]
231+
fn from_hashmap_of_exts() {
232+
let mut map: HashMap<TypeId, Ext> = HashMap::new();
233+
234+
let v = 0i32;
235+
let id = v.type_id();
236+
assert!(map.insert(id, Arc::new(v)).is_none());
237+
238+
let s: String = "meow".to_string();
239+
let id = s.type_id();
240+
assert!(map.insert(id, Arc::new(s)).is_none());
241+
242+
assert!(map.len() == 2);
243+
244+
let exts: Extensions = map.into();
245+
246+
let v = exts.get_ext::<i32>().expect("must get a value");
247+
assert_eq!(0i32, *v.as_ref());
248+
249+
let v = exts.get_ext::<String>().expect("must get a value");
250+
assert_eq!(String::from("meow"), *v.as_ref());
251+
}
261252
}

object_store/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,8 @@ pub struct GetOptions {
965965
/// <https://datatracker.ietf.org/doc/html/rfc9110#name-head>
966966
pub head: bool,
967967

968-
/// Implementation-specific extensions. but
969-
/// intended for use by [`ObjectStore`] implementations that need to pass context-specific
970-
/// information (like tracing spans) via trait methods.
968+
/// Implementation-specific extensions. Intended for use by [`ObjectStore`] implementations
969+
/// that need to pass context-specific information (like tracing spans) via trait methods.
971970
///
972971
/// These extensions are ignored entirely by backends offered through this crate.
973972
pub extensions: Extensions,

0 commit comments

Comments
 (0)