Skip to main content

Any

Struct Any 

Source
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

Source

pub fn new<T>(any: T, tag: &'static str) -> Self
where T: Serialize + Debug + 'static,

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.

Source

pub fn raw<T>(any: T, repr: Value, tag: &'static str) -> Self
where 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.

Source

pub fn tag(&self) -> &'static str

Return the benchmark tag associated with this benchmarks.

Source

pub fn type_id(&self) -> TypeId

Return the Rust std::any::TypeId for the contained object.

Source

pub fn is<T>(&self) -> bool
where 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>());
Source

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());
Source

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,
);
Source

pub fn convert<'a, T, U>(&'a self) -> Result<U>
where U: DispatchRule<&'a T>, Error: From<U::Error>, T: 'static,

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();
Source

pub fn description<'a, T, U>( f: &mut Formatter<'_>, from: Option<&&'a Self>, tag: impl Display, ) -> Result
where 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\"",
);
Source

pub fn serialize(&self) -> Result<Value, Error>

Serialize the contained object to a serde_json::Value.

Trait Implementations§

Source§

impl Debug for Any

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> DispatchRule<T> for T

Source§

type Error = Infallible

Errors that can occur during convert.
Source§

fn try_match(_from: &T) -> Result<MatchScore, FailureScore>

Attempt to match the value From to the type represented by Self. Read more
Source§

fn convert(from: T) -> Result<T, <T as DispatchRule<T>>::Error>

Perform the actual conversion. Read more
Source§

fn description(f: &mut Formatter<'_>, from: Option<&T>) -> Result<(), Error>

Write a description of the dispatch rule and outcome to the formatter. Read more
Source§

fn try_match_verbose<'a>( from: &'a From, ) -> Result<MatchScore, TaggedFailureScore<'a>>
where Self: 'a,

The equivalent of try_match but returns a reason for a failed score. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.