in starlark/src/stdlib/mod.rs [138:206]
fn test_value_attributes() {
#[derive(Copy, Clone, Debug, Dupe, PartialEq, Display, AnyLifetime, NoSerialize)]
#[display(fmt = "{}", _0)]
struct Bool2(bool);
starlark_simple_value!(Bool2);
impl<'v> StarlarkValue<'v> for Bool2 {
starlark_type!("bool2");
fn get_methods(&self) -> Option<&'static Methods> {
static RES: MethodsStatic = MethodsStatic::new();
RES.methods(methods)
}
fn equals(&self, other: Value<'v>) -> anyhow::Result<bool> {
match other.downcast_ref::<Bool2>() {
None => Ok(false),
Some(v) => Ok(*v == *self),
}
}
}
impl<'v> UnpackValue<'v> for Bool2 {
fn expected() -> String {
Bool2::get_type_value_static().as_str().to_owned()
}
fn unpack_value(value: Value<'v>) -> Option<Self> {
Some(*value.downcast_ref::<Bool2>().unwrap())
}
}
#[starlark_module]
fn globals(builder: &mut GlobalsBuilder) {
const True2: Bool2 = Bool2(true);
const False2: Bool2 = Bool2(false);
}
#[starlark_module]
fn methods(builder: &mut MethodsBuilder) {
#[starlark(attribute)]
fn invert1(this: Bool2) -> anyhow::Result<Bool2> {
Ok(Bool2(!this.0))
}
fn invert2(this: Bool2) -> anyhow::Result<Bool2> {
Ok(Bool2(!this.0))
}
}
let mut a = Assert::new();
a.globals_add(globals);
a.all_true(
r#"
True2 == True2
True2 != False2
True2.invert1 == False2
False2.invert1 == True2
False2.invert2() == True2
hasattr(True2, "invert1") == True
hasattr(True2, "invert2") == True
hasattr(True2, "invert3") == False
dir(False2) == ["invert1","invert2"]
getattr(False2, "invert1") == True2
getattr(True2, "invert1") == False2
getattr(True2, "invert2")() == False2
"#,
);
}