Trait scale_value::At

source ·
pub trait At<Ctx>: Sealed {
    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 std::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§

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

Implementations on Foreign Types§

Implementors§