Trait starlark::values::SimpleValue[][src]

pub trait SimpleValue: StarlarkValue<'static> + Send + Sync { }
Expand description

A trait representing Starlark values which are simple - they aren’t mutable and can’t contain other Starlark values.

Let’s define a simple object, where +x makes the string uppercase:

use starlark::values::{Heap, StarlarkValue, Value};
use starlark::{starlark_simple_value, starlark_type};

#[derive(Debug)]
struct MyObject(String);
starlark_simple_value!(MyObject);
impl<'v> StarlarkValue<'v> for MyObject {
    starlark_type!("my_object");

    // We can choose to implement whichever methods we want.
    // All other operations will result in runtime errors.
    fn plus(&self, heap: &'v Heap) -> anyhow::Result<Value<'v>> {
        Ok(heap.alloc(MyObject(self.0.to_uppercase())))
    }
}

The starlark_simple_value! macro defines instances of AnyLifetime, AllocValue, AllocFrozenValue, SimpleValue and FromValue. It also defines a method:

impl MyObject {
    pub fn from_value<'v>(x: Value<'v>) -> Option<ARef<'v, MyObject>> {
        ...
    }
}

All users defining SimpleValue should use this macro.

Implementors