Struct starlark::values::OwnedFrozenValue
source · pub struct OwnedFrozenValue { /* private fields */ }
Expand description
A FrozenValue
along with a FrozenHeapRef
that ensures it is kept alive.
Obtained from FrozenModule::get
or
OwnedFrozenValue::alloc
.
While it is possible to obtain the underlying FrozenValue
with
unchecked_frozen_value
, that approach
is strongly discouraged. See the other methods which unpack the code, access it as a
Value
(which has a suitable lifetime) or add references to other heaps.
Implementations§
source§impl OwnedFrozenValue
impl OwnedFrozenValue
sourcepub unsafe fn new(owner: FrozenHeapRef, value: FrozenValue) -> Self
pub unsafe fn new(owner: FrozenHeapRef, value: FrozenValue) -> Self
Create an OwnedFrozenValue
- generally OwnedFrozenValue
s are obtained
from FrozenModule::get
.
Safe provided the value
(and any values it points at) are kept alive by the
owner
, typically because the value was created on the heap.
use starlark::values::FrozenHeap;
use starlark::values::OwnedFrozenValue;
let heap = FrozenHeap::new();
let value = heap.alloc("test");
unsafe { OwnedFrozenValue::new(heap.into_ref(), value) };
sourcepub fn alloc(x: impl AllocFrozenValue) -> Self
pub fn alloc(x: impl AllocFrozenValue) -> Self
Create an OwnedFrozenValue
in a new heap.
sourcepub fn unpack_bool(&self) -> Option<bool>
pub fn unpack_bool(&self) -> Option<bool>
Unpack the boolean contained in the underlying value, or None
if it is not a boolean.
sourcepub fn unpack_i32(&self) -> Option<i32>
pub fn unpack_i32(&self) -> Option<i32>
Obtain the underlying integer if it fits in an i32
.
Note floats are not considered integers, i. e. unpack_i32
for 1.0
will return None
.
sourcepub fn unpack_str(&self) -> Option<&str>
pub fn unpack_str(&self) -> Option<&str>
Unpack the string contained in the underlying value, or None
if it is not an string.
sourcepub fn downcast<T: StarlarkValue<'static>>(
self
) -> Result<OwnedFrozenValueTyped<T>, Self>
pub fn downcast<T: StarlarkValue<'static>>( self ) -> Result<OwnedFrozenValueTyped<T>, Self>
Check if self
references <T>
.
sourcepub fn downcast_anyhow<T: StarlarkValue<'static>>(
self
) -> Result<OwnedFrozenValueTyped<T>>
pub fn downcast_anyhow<T: StarlarkValue<'static>>( self ) -> Result<OwnedFrozenValueTyped<T>>
downcast
, but return an error for human instead of original value.
sourcepub fn owned_value<'v>(&self, heap: &'v FrozenHeap) -> Value<'v>
pub fn owned_value<'v>(&self, heap: &'v FrozenHeap) -> Value<'v>
Extract a Value
by passing the FrozenHeap
which will promise to keep it alive.
When using with a Module
,
see the frozen_heap
function.
If you don’t care about the resulting lifetime the value
method is easier.
sourcepub fn map(&self, f: impl FnOnce(FrozenValue) -> FrozenValue) -> Self
pub fn map(&self, f: impl FnOnce(FrozenValue) -> FrozenValue) -> Self
Operate on the FrozenValue
stored inside.
Safe provided you don’t store the argument FrozenValue
after the closure has returned.
Using this function is discouraged when possible.
sourcepub fn try_map<E>(
&self,
f: impl FnOnce(FrozenValue) -> Result<FrozenValue, E>
) -> Result<Self, E>
pub fn try_map<E>( &self, f: impl FnOnce(FrozenValue) -> Result<FrozenValue, E> ) -> Result<Self, E>
sourcepub fn owner(&self) -> &FrozenHeapRef
pub fn owner(&self) -> &FrozenHeapRef
Obtain a reference to the FrozenHeap that owns this value.
sourcepub unsafe fn unchecked_frozen_value(&self) -> FrozenValue
pub unsafe fn unchecked_frozen_value(&self) -> FrozenValue
Obtain direct access to the FrozenValue
that lives inside. If you drop all
references to the FrozenHeap
keeping it alive, any code using the FrozenValue
is likely to segfault. If possible use value
or
owned_frozen_value
.
sourcepub unsafe fn owned_frozen_value(&self, heap: &FrozenHeap) -> FrozenValue
pub unsafe fn owned_frozen_value(&self, heap: &FrozenHeap) -> FrozenValue
Extract a FrozenValue
by passing the FrozenHeap
which will keep it alive.
Provided the argument heap does indeed stay alive for the lifetime of the result,
all will be fine. Unsafe if you pass the wrong heap, or don’t keep the heap alive
long enough. Where possible, use value
or
owned_value
.
Trait Implementations§
source§impl AllocFrozenValue for OwnedFrozenValue
impl AllocFrozenValue for OwnedFrozenValue
source§fn alloc_frozen_value(self, heap: &FrozenHeap) -> FrozenValue
fn alloc_frozen_value(self, heap: &FrozenHeap) -> FrozenValue
source§impl Allocative for OwnedFrozenValue
impl Allocative for OwnedFrozenValue
source§impl Clone for OwnedFrozenValue
impl Clone for OwnedFrozenValue
source§fn clone(&self) -> OwnedFrozenValue
fn clone(&self) -> OwnedFrozenValue
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more