pub struct Any { /* private fields */ }Expand description
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.
Implementations§
Source§impl Any
impl Any
Sourcepub fn new<T>(any: T, tag: &'static str) -> Self
pub fn new<T>(any: T, tag: &'static str) -> Self
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.
Sourcepub fn raw<T>(any: T, repr: Value, tag: &'static str) -> Selfwhere
T: Debug + 'static,
pub fn raw<T>(any: T, repr: Value, tag: &'static str) -> Selfwhere
T: Debug + 'static,
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.
Sourcepub fn type_id(&self) -> TypeId
pub fn type_id(&self) -> TypeId
Return the Rust std::any::TypeId for the contained object.
Sourcepub fn is<T>(&self) -> boolwhere
T: Any,
pub fn is<T>(&self) -> boolwhere
T: Any,
Return true if the runtime value is T. Otherwise, return false.
use diskann_benchmark_runner::any::Any;
let value = Any::new(42usize, "usize");
assert!(value.is::<usize>());
assert!(!value.is::<u32>());Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: Any,
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: Any,
Return a reference to the contained object if it’s runtime type is T.
Otherwise return None.
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());Sourcepub fn try_match<'a, T, U>(&'a self) -> Result<MatchScore, FailureScore>where
U: DispatchRule<&'a T>,
T: 'static,
pub fn try_match<'a, T, U>(&'a self) -> Result<MatchScore, FailureScore>where
U: DispatchRule<&'a T>,
T: 'static,
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).
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,
);Sourcepub fn convert<'a, T, U>(&'a self) -> Result<U>
pub fn convert<'a, T, U>(&'a self) -> Result<U>
Attempt to downcast self to T and if succssful, try converting &T with U using
crate::dispatcher::DispatchRule.
If unsuccessful, returns an error.
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();Sourcepub fn description<'a, T, U>(
f: &mut Formatter<'_>,
from: Option<&&'a Self>,
tag: impl Display,
) -> Resultwhere
U: DispatchRule<&'a T>,
T: 'static,
pub fn description<'a, T, U>(
f: &mut Formatter<'_>,
from: Option<&&'a Self>,
tag: impl Display,
) -> Resultwhere
U: DispatchRule<&'a T>,
T: 'static,
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.
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\"",
);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Any
impl !RefUnwindSafe for Any
impl !Send for Any
impl !Sync for Any
impl Unpin for Any
impl UnsafeUnpin for Any
impl !UnwindSafe for Any
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> DispatchRule<T> for T
impl<T> DispatchRule<T> for T
Source§type Error = Infallible
type Error = Infallible
convert.Source§fn try_match(_from: &T) -> Result<MatchScore, FailureScore>
fn try_match(_from: &T) -> Result<MatchScore, FailureScore>
Source§fn convert(from: T) -> Result<T, <T as DispatchRule<T>>::Error>
fn convert(from: T) -> Result<T, <T as DispatchRule<T>>::Error>
Source§fn description(f: &mut Formatter<'_>, from: Option<&T>) -> Result<(), Error>
fn description(f: &mut Formatter<'_>, from: Option<&T>) -> Result<(), Error>
Source§fn try_match_verbose<'a>(
from: &'a From,
) -> Result<MatchScore, TaggedFailureScore<'a>>where
Self: 'a,
fn try_match_verbose<'a>(
from: &'a From,
) -> Result<MatchScore, TaggedFailureScore<'a>>where
Self: 'a,
try_match but returns a reason for a failed score. Read more