1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
use crate::dispatcher::{DispatchRule, FailureScore, MatchScore};
/// An refinement of [`std::any::Any`] with an associated name (tag) and serialization.
///
/// This type represents deserialized inputs returned from [`crate::Input::try_deserialize`]
/// and is passed to beckend benchmarks for matching and execution.
#[derive(Debug)]
pub struct Any {
any: Box<dyn SerializableAny>,
tag: &'static str,
}
/// The score given unsuccessful downcasts in [`Any::try_match`].
pub const MATCH_FAIL: FailureScore = FailureScore(10_000);
impl Any {
/// Construct a new [`Any`] around `any` and associate it with the name `tag`.
///
/// The tag is included as merely a debugging and readability aid and usually should
/// belong to a [`crate::Input::tag`] that generated `any`.
pub fn new<T>(any: T, tag: &'static str) -> Self
where
T: serde::Serialize + std::fmt::Debug + 'static,
{
Self {
any: Box::new(any),
tag,
}
}
/// A lower level API for constructing an [`Any`] that decouples the serialized
/// representation from the inmemory representation.
///
/// When serialized, the **exact** representation of `repr` will be used.
///
/// This is useful in some contexts where as part of input resolution, a fully resolved
/// input struct contains elements that are not serializable.
///
/// Like [`Any::new`], the tag is included for debugging and readability.
pub fn raw<T>(any: T, repr: serde_json::Value, tag: &'static str) -> Self
where
T: std::fmt::Debug + 'static,
{
Self {
any: Box::new(Raw::new(any, repr)),
tag,
}
}
/// Return the benchmark tag associated with this benchmarks.
pub fn tag(&self) -> &'static str {
self.tag
}
/// Return the Rust [`std::any::TypeId`] for the contained object.
pub fn type_id(&self) -> std::any::TypeId {
self.any.as_any().type_id()
}
/// Return `true` if the runtime value is `T`. Otherwise, return false.
///
/// ```rust
/// use diskann_benchmark_runner::any::Any;
///
/// let value = Any::new(42usize, "usize");
/// assert!(value.is::<usize>());
/// assert!(!value.is::<u32>());
/// ```
#[must_use = "this function has no side effects"]
pub fn is<T>(&self) -> bool
where
T: std::any::Any,
{
self.any.as_any().is::<T>()
}
/// Return a reference to the contained object if it's runtime type is `T`.
///
/// Otherwise return `None`.
///
/// ```rust
/// use diskann_benchmark_runner::any::Any;
///
/// let value = Any::new(42usize, "usize");
/// assert_eq!(*value.downcast_ref::<usize>().unwrap(), 42);
/// assert!(value.downcast_ref::<u32>().is_none());
/// ```
pub fn downcast_ref<T>(&self) -> Option<&T>
where
T: std::any::Any,
{
self.any.as_any().downcast_ref::<T>()
}
/// Attempt to downcast self to `T` and if succssful, try matching `&T` with `U` using
/// [`crate::dispatcher::DispatchRule`].
///
/// Otherwise, return `Err(diskann_benchmark_runner::any::MATCH_FAIL)`.
///
/// ```rust
/// use diskann_benchmark_runner::{
/// any::Any,
/// dispatcher::{self, MatchScore, FailureScore},
/// utils::datatype::{self, DataType, Type},
/// };
///
/// let value = Any::new(DataType::Float32, "datatype");
///
/// // A successful down cast and successful match.
/// assert_eq!(
/// value.try_match::<DataType, Type<f32>>().unwrap(),
/// MatchScore(0),
/// );
///
/// // A successful down cast but unsuccessful match.
/// assert_eq!(
/// value.try_match::<DataType, Type<f64>>().unwrap_err(),
/// datatype::MATCH_FAIL,
/// );
///
/// // An unsuccessful down cast.
/// let value = Any::new(0usize, "usize");
/// assert_eq!(
/// value.try_match::<DataType, Type<f32>>().unwrap_err(),
/// diskann_benchmark_runner::any::MATCH_FAIL,
/// );
/// ```
pub fn try_match<'a, T, U>(&'a self) -> Result<MatchScore, FailureScore>
where
U: DispatchRule<&'a T>,
T: 'static,
{
if let Some(cast) = self.downcast_ref::<T>() {
U::try_match(&cast)
} else {
Err(MATCH_FAIL)
}
}
/// Attempt to downcast self to `T` and if succssful, try converting `&T` with `U` using
/// [`crate::dispatcher::DispatchRule`].
///
/// If unsuccessful, returns an error.
///
/// ```rust
/// use diskann_benchmark_runner::{
/// any::Any,
/// dispatcher::{self, MatchScore, FailureScore},
/// utils::datatype::{self, DataType, Type},
/// };
///
/// let value = Any::new(DataType::Float32, "datatype");
///
/// // A successful down cast and successful conversion.
/// let _: Type<f32> = value.convert::<DataType, _>().unwrap();
/// ```
pub fn convert<'a, T, U>(&'a self) -> anyhow::Result<U>
where
U: DispatchRule<&'a T>,
anyhow::Error: From<U::Error>,
T: 'static,
{
if let Some(cast) = self.downcast_ref::<T>() {
Ok(U::convert(cast)?)
} else {
Err(anyhow::Error::msg("invalid dispatch"))
}
}
/// A wrapper for [`DispatchRule::description`].
///
/// If `from` is `None` - document the expected tag for the input and return
/// `<U as DispatchRule<&T>>::description(f, None)`.
///
/// If `from` is `Some` - attempt to downcast to `T`. If successful, return the dispatch
/// rule description for `U` on the doncast reference. Otherwise, return the expected tag.
///
/// ```rust
/// use diskann_benchmark_runner::{
/// any::Any,
/// utils::datatype::{self, DataType, Type},
/// };
///
/// use std::io::Write;
///
/// struct Display(Option<Any>);
///
/// impl std::fmt::Display for Display {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// match &self.0 {
/// Some(v) => Any::description::<DataType, Type<f32>>(f, Some(&&v), "my-tag"),
/// None => Any::description::<DataType, Type<f32>>(f, None, "my-tag"),
/// }
/// }
/// }
///
/// // No contained value - document the expected conversion.
/// assert_eq!(
/// Display(None).to_string(),
/// "tag \"my-tag\"\nfloat32",
/// );
///
/// // Matching contained value.
/// assert_eq!(
/// Display(Some(Any::new(DataType::Float32, "datatype"))).to_string(),
/// "successful match",
/// );
///
/// // Successful down cast - unsuccessful match.
/// assert_eq!(
/// Display(Some(Any::new(DataType::UInt64, "datatype"))).to_string(),
/// "expected \"float32\" but found \"uint64\"",
/// );
///
/// // Unsuccessful down cast.
/// assert_eq!(
/// Display(Some(Any::new(0usize, "another-tag"))).to_string(),
/// "expected tag \"my-tag\" - instead got \"another-tag\"",
/// );
/// ```
pub fn description<'a, T, U>(
f: &mut std::fmt::Formatter<'_>,
from: Option<&&'a Self>,
tag: impl std::fmt::Display,
) -> std::fmt::Result
where
U: DispatchRule<&'a T>,
T: 'static,
{
match from {
Some(this) => match this.downcast_ref::<T>() {
Some(a) => U::description(f, Some(&a)),
None => write!(
f,
"expected tag \"{}\" - instead got \"{}\"",
tag,
this.tag(),
),
},
None => {
writeln!(f, "tag \"{}\"", tag)?;
U::description(f, None::<&&T>)
}
}
}
/// Serialize the contained object to a [`serde_json::Value`].
pub fn serialize(&self) -> Result<serde_json::Value, serde_json::Error> {
self.any.dump()
}
}
trait SerializableAny: std::fmt::Debug {
fn as_any(&self) -> &dyn std::any::Any;
fn dump(&self) -> Result<serde_json::Value, serde_json::Error>;
}
impl<T> SerializableAny for T
where
T: std::any::Any + serde::Serialize + std::fmt::Debug,
{
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn dump(&self) -> Result<serde_json::Value, serde_json::Error> {
serde_json::to_value(self)
}
}
// A backend type that allows users to decouple the serialized representation from the
// actual type.
#[derive(Debug)]
struct Raw<T> {
value: T,
repr: serde_json::Value,
}
impl<T> Raw<T> {
fn new(value: T, repr: serde_json::Value) -> Self {
Self { value, repr }
}
}
impl<T> SerializableAny for Raw<T>
where
T: std::any::Any + std::fmt::Debug,
{
fn as_any(&self) -> &dyn std::any::Any {
&self.value
}
fn dump(&self) -> Result<serde_json::Value, serde_json::Error> {
Ok(self.repr.clone())
}
}
///////////
// Tests //
///////////
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::datatype::{self, DataType, Type};
#[test]
fn test_new() {
let x = Any::new(42usize, "my-tag");
assert_eq!(x.tag(), "my-tag");
assert_eq!(x.type_id(), std::any::TypeId::of::<usize>());
assert!(x.is::<usize>());
assert!(!x.is::<u32>());
assert_eq!(*x.downcast_ref::<usize>().unwrap(), 42);
assert!(x.downcast_ref::<u32>().is_none());
assert!(!x.is::<Raw<usize>>());
assert!(!x.is::<Raw<u32>>());
assert!(x.downcast_ref::<Raw<usize>>().is_none());
assert!(x.downcast_ref::<Raw<u32>>().is_none());
assert_eq!(
x.serialize().unwrap(),
serde_json::Value::Number(serde_json::value::Number::from(42usize))
);
}
#[test]
fn test_raw() {
let repr = serde_json::json!(1.5);
let x = Any::raw(42usize, repr, "my-tag");
assert_eq!(x.tag(), "my-tag");
assert_eq!(x.type_id(), std::any::TypeId::of::<usize>());
assert!(x.is::<usize>());
assert!(!x.is::<u32>());
assert_eq!(*x.downcast_ref::<usize>().unwrap(), 42);
assert!(x.downcast_ref::<u32>().is_none());
assert!(!x.is::<Raw<usize>>());
assert!(!x.is::<Raw<u32>>());
assert!(x.downcast_ref::<Raw<usize>>().is_none());
assert!(x.downcast_ref::<Raw<u32>>().is_none());
assert_eq!(
x.serialize().unwrap(),
serde_json::Value::Number(serde_json::value::Number::from_f64(1.5).unwrap())
);
}
#[test]
fn test_try_match() {
let value = Any::new(DataType::Float32, "random-tag");
// A successful down cast and successful match.
assert_eq!(
value.try_match::<DataType, Type<f32>>().unwrap(),
MatchScore(0),
);
// A successful down cast but unsuccessful match.
assert_eq!(
value.try_match::<DataType, Type<f64>>().unwrap_err(),
datatype::MATCH_FAIL,
);
// An unsuccessful down cast.
let value = Any::new(0usize, "");
assert_eq!(
value.try_match::<DataType, Type<f32>>().unwrap_err(),
MATCH_FAIL,
);
}
#[test]
fn test_convert() {
let value = Any::new(DataType::Float32, "random-tag");
// A successful down cast and successful conversion.
let _: Type<f32> = value.convert::<DataType, _>().unwrap();
// An invalid match should return an error.
let value = Any::new(0usize, "random-rag");
let err = value.convert::<DataType, Type<f32>>().unwrap_err();
let msg = err.to_string();
assert!(msg.contains("invalid dispatch"), "{}", msg);
}
#[test]
#[should_panic(expected = "invalid dispatch")]
fn test_convert_inner_error() {
let value = Any::new(DataType::Float32, "random-tag");
let _ = value.convert::<DataType, Type<u64>>();
}
#[test]
fn test_description() {
struct Display(Option<Any>);
impl std::fmt::Display for Display {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0 {
Some(v) => Any::description::<DataType, Type<f32>>(f, Some(&v), "my-tag"),
None => Any::description::<DataType, Type<f32>>(f, None, "my-tag"),
}
}
}
// No contained value - document the expected conversion.
assert_eq!(Display(None).to_string(), "tag \"my-tag\"\nfloat32",);
// Matching contained value.
assert_eq!(
Display(Some(Any::new(DataType::Float32, ""))).to_string(),
"successful match",
);
// Successful down cast - unsuccessful match.
assert_eq!(
Display(Some(Any::new(DataType::UInt64, ""))).to_string(),
"expected \"float32\" but found \"uint64\"",
);
// Unsuccessful down cast.
assert_eq!(
Display(Some(Any::new(0usize, ""))).to_string(),
"expected tag \"my-tag\" - instead got \"\"",
);
}
}