use crate::values::layout::avalue;
use crate::values::layout::avalue::AValueImpl;
use crate::values::layout::avalue::AValueSimple;
use crate::values::layout::heap::repr::AValueRepr;
use crate::values::FrozenValue;
use crate::values::FrozenValueTyped;
use crate::values::StarlarkValue;
pub struct AllocStaticSimple<T: StarlarkValue<'static>>(
AValueRepr<AValueImpl<'static, AValueSimple<T>>>,
);
impl<T: StarlarkValue<'static>> AllocStaticSimple<T> {
pub const fn alloc(value: T) -> Self {
AllocStaticSimple(avalue::alloc_static::<AValueSimple<T>>(value))
}
pub fn unpack(&'static self) -> FrozenValueTyped<'static, T> {
FrozenValueTyped::new_repr(&self.0)
}
pub fn to_frozen_value(&'static self) -> FrozenValue {
self.unpack().to_frozen_value()
}
}
#[cfg(test)]
mod tests {
use allocative::Allocative;
use starlark_derive::starlark_value;
use starlark_derive::NoSerialize;
use starlark_derive::ProvidesStaticType;
use crate as starlark;
use crate::values::AllocStaticSimple;
use crate::values::StarlarkValue;
#[test]
fn test_alloc_static_simple() {
#[derive(
Debug,
derive_more::Display,
ProvidesStaticType,
NoSerialize,
Allocative
)]
#[display("MySimpleValue")]
struct MySimpleValue(u32);
#[starlark_value(type = "MySimpleValue")]
impl<'v> StarlarkValue<'v> for MySimpleValue {}
static VALUE: AllocStaticSimple<MySimpleValue> =
AllocStaticSimple::alloc(MySimpleValue(17));
assert_eq!(17, VALUE.unpack().as_ref().0);
}
}