pub trait ExportedName: Debug + Display + Freeze<Frozen = FrozenExportedName> + Allocative + 'static {
    // Required methods
    fn borrow(&self) -> Option<BorrowedExportedName<'_>>;
    fn equal_to(&self, rhs: &str) -> bool;
    fn try_export_as(&self, name: &str);
}
Expand description

Type parameter for types which need to be exported with a name.

Typical use case is this:

use allocative::Allocative;
use starlark::eval::Evaluator;
use starlark::values::exported_name::ExportedName;
use starlark::values::exported_name::FrozenExportedName;
use starlark::values::StarlarkValue;
use starlark_derive::starlark_value;
use starlark_derive::NoSerialize;
use starlark_derive::ProvidesStaticType;

#[derive(
    Debug,
    NoSerialize,
    ProvidesStaticType,
    Allocative,
    derive_more::Display
)]
#[display(fmt = "{:?}", "self")]
struct MyStruct<T: ExportedName + 'static> {
    name: T,
}

#[starlark_value(type = "MyStruct")]
impl<'v, T: ExportedName> StarlarkValue<'v> for MyStruct<T> {
    type Canonical = MyStruct<FrozenExportedName>;

    fn export_as(
        &self,
        variable_name: &str,
        _eval: &mut Evaluator<'v, '_>,
    ) -> starlark::Result<()> {
        self.name.try_export_as(variable_name);
        Ok(())
    }
}

This is an utility implementing common pattern, but it is completely optional when implementing StarlarkValue::export_as.

Required Methods§

source

fn borrow(&self) -> Option<BorrowedExportedName<'_>>

Borrow the name.

source

fn equal_to(&self, rhs: &str) -> bool

Name is equal to the given string.

source

fn try_export_as(&self, name: &str)

Try update the name.

This operation is no-op, if

  • the name is already set (first export wins)
  • the name is frozen

Implementors§