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 untagged<T>(any: T) -> Self
pub fn untagged<T>(any: T) -> Self
Return a new Any without a tag.
This method exists for examples and testing and generally should not be used.
Users should use crate::Checker::any to construct a properly tagged Any.
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::untagged(42usize);
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::untagged(42usize);
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::untagged(DataType::Float32);
// 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::untagged(0usize);
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::untagged(DataType::Float32);
// 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::untagged(DataType::Float32))).to_string(),
"successful match",
);
// Successful down cast - unsuccessful match.
assert_eq!(
Display(Some(Any::untagged(DataType::UInt64))).to_string(),
"expected \"float32\" but found \"uint64\"",
);
// Unsuccessful down cast.
assert_eq!(
Display(Some(Any::untagged(0usize))).to_string(),
"expected tag \"my-tag\" - instead got \"\"",
);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 !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