Trait At

Source
pub trait At<Ctx>: Sealed {
    // Required method
    fn at<L: AsLocation>(&self, loc: L) -> Option<&Value<Ctx>>;
}
Expand description

This trait allows indexing into Values (and options of Values) using the At::at() function. It’s a little like Rust’s core::ops::Index trait, but adapted so that we can return and work with optionals.

Indexing into a Value never panics; instead it will return None if a value at the given location cannot be found.

§Example

use scale_value::{ Value, At };

let val = Value::named_composite([
    ("hello", Value::unnamed_composite([
        Value::u128(1),
        Value::bool(true),
        Value::named_composite([
            ("wibble", Value::bool(false)),
            ("foo", Value::named_composite([
                ("bar", Value::u128(123))
            ]))
        ])
    ]))
]);

// Use `at` to access nested values:
assert_eq!(val.at("hello").at(0), Some(&Value::u128(1)));
assert_eq!(val.at("hello").at(1), Some(&Value::bool(true)));
assert_eq!(val.at("hello").at(2).at("wibble"), Some(&Value::bool(false)));
assert_eq!(val.at("hello").at(2).at("foo").at("bar"), Some(&Value::u128(123)));

// If the value doesn't exist, None will be returned:
assert_eq!(val.at("wibble").at(3), None);
assert_eq!(val.at("wibble").at("wobble").at("nope"), None);

Required Methods§

Source

fn at<L: AsLocation>(&self, loc: L) -> Option<&Value<Ctx>>

Index into a value, returning a reference to the value if one exists, or None if not.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<Ctx, T: At<Ctx>> At<Ctx> for Option<&T>

Source§

fn at<L: AsLocation>(&self, loc: L) -> Option<&Value<Ctx>>

Implementors§

Source§

impl<Ctx> At<Ctx> for Composite<Ctx>

Source§

impl<Ctx> At<Ctx> for Value<Ctx>

Source§

impl<Ctx> At<Ctx> for Variant<Ctx>