Skip to main content

KeyValue

Struct KeyValue 

Source
pub struct KeyValue<'a> { /* private fields */ }
Expand description

Display a dynamic list of key-value pairs in a YAML-like style.

Keys are left-aligned and single-line values are aligned into a common column just past the longest key. A value that renders to multiple lines (for example a nested KeyValue or any other multi-line block) is placed on the lines following its key, indented by two spaces. This keeps nested structures visibly subordinate to their key regardless of whether the value is itself a key-value list or an opaque block.

§Examples

use diskann_benchmark_runner::utils::fmt::KeyValue;

let mut kv = KeyValue::new();
kv.push("a", &1);
kv.push("hello", &"world");

let expected = "a:     1\nhello: world";

assert_eq!(kv.to_string(), expected);

Multi-line values are indented beneath their key:

use diskann_benchmark_runner::utils::fmt::KeyValue;

let mut inner = KeyValue::new();
inner.push("x", &1);
inner.push("yy", &2);
let inner = inner.to_string();

let mut kv = KeyValue::new();
kv.push("name", &"example");
kv.push("nested", &inner);

let expected = "name:   example\nnested:\n  x:  1\n  yy: 2";

assert_eq!(kv.to_string(), expected);

Implementations§

Source§

impl<'a> KeyValue<'a>

Source

pub fn new() -> Self

Create a new empty KeyValue formatter.

Source

pub fn push(&mut self, key: &'a str, value: &'a dyn Display)

Push the key-value pair to self for formatting.

Source

pub fn push_debug(&mut self, key: &'a str, value: &'a dyn Debug)

Push the key-value pair to self for formatting via std::fmt::Debug.

Source

pub fn push_eager<D>(&mut self, key: &'a str, value: D)
where D: Display,

Push the key-value pair to self for formatting - eagerly formatting value.

Source

pub fn push_eager_nested<D>(&mut self, key: &'a str, value: D)
where D: Display,

Push the key-value pair to self for formatting - eagerly formatting value.

Source

pub fn len(&self) -> usize

Return the number of pending key-value pairs.

Source

pub fn is_empty(&self) -> bool

Return true if there are no key-value pairs.

Trait Implementations§

Source§

impl<'a> Debug for KeyValue<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for KeyValue<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for KeyValue<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for KeyValue<'a>

§

impl<'a> !Send for KeyValue<'a>

§

impl<'a> !Sync for KeyValue<'a>

§

impl<'a> !UnwindSafe for KeyValue<'a>

§

impl<'a> Forget for KeyValue<'a>

§

impl<'a> Freeze for KeyValue<'a>

§

impl<'a> Move for KeyValue<'a>

§

impl<'a> Unpin for KeyValue<'a>

§

impl<'a> UnsafeUnpin for KeyValue<'a>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.