Macro starlark::starlark_simple_value
source · 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 allocative::Allocative;
use derive_more::Display;
use starlark::starlark_simple_value;
use starlark::values::Heap;
use starlark::values::NoSerialize;
use starlark::values::ProvidesStaticType;
use starlark::values::StarlarkValue;
use starlark::values::Value;
use starlark_derive::starlark_value;
#[derive(Debug, Display, ProvidesStaticType, NoSerialize, Allocative)]
struct MyObject(String);
starlark_simple_value!(MyObject);
#[starlark_value(type = "my_object")]
impl<'v> StarlarkValue<'v> for MyObject {
// We can choose to implement whichever methods we want.
// All other operations will result in runtime errors.
fn plus(&self, heap: &'v Heap) -> starlark::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> {
...
}
}