macro_rules! starlark_simple_value {
    ($x:ident) => { ... };
}
Expand description

A macro reducing boilerplace defining Starlark values which are simple - they aren’t mutable and can’t contain references to other Starlark values.

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

use starlark::values::{Heap, StarlarkValue, Value, ProvidesStaticType, NoSerialize};
use starlark::{starlark_simple_value, starlark_type};
use derive_more::Display;
use allocative::Allocative;

#[derive(Debug, Display, ProvidesStaticType, NoSerialize, Allocative)]
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 ProvidesStaticType, AllocValue, AllocFrozenValue and UnpackValue. It also defines a method:

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