pub struct Partial<'s> { /* private fields */ }
Expand description
A partially-initialized shape.
This type keeps track of the initialized state of every field and only allows getting out the concrete type or the boxed concrete type or moving out of this partial into a pointer if all the fields have been initialized.
Implementations§
Source§impl Partial<'_>
impl Partial<'_>
Sourcepub fn alloc(shape: ShapeDesc) -> Self
pub fn alloc(shape: ShapeDesc) -> Self
Allocates a partial on the heap for the given shape descriptor.
Sourcepub fn borrow<T: Shapely>(uninit: &mut MaybeUninit<T>) -> Self
pub fn borrow<T: Shapely>(uninit: &mut MaybeUninit<T>) -> Self
Borrows a MaybeUninit<Self>
and returns a Partial
.
Before calling assume_init, make sure to call Partial.build_in_place().
Sourcepub fn array_slot(&mut self, size_hint: Option<usize>) -> Option<ArraySlot>
pub fn array_slot(&mut self, size_hint: Option<usize>) -> Option<ArraySlot>
Returns a slot for treating this partial as an array (onto which you can push new items)
Sourcepub fn hashmap_slot(&mut self, size_hint: Option<usize>) -> Option<HashMapSlot>
pub fn hashmap_slot(&mut self, size_hint: Option<usize>) -> Option<HashMapSlot>
Returns a slot for a HashMap field in the shape.
Sourcepub fn hashmap_iter(&self) -> Option<HashMapIter>
pub fn hashmap_iter(&self) -> Option<HashMapIter>
Returns an iterator over the key-value pairs in a HashMap
Sourcepub fn scalar_slot(&mut self) -> Option<Slot<'_>>
pub fn scalar_slot(&mut self) -> Option<Slot<'_>>
Returns a slot for assigning this whole shape as a scalar
Sourcepub fn slot_by_name<'s>(
&'s mut self,
name: &str,
) -> Result<Slot<'s>, FieldError>
pub fn slot_by_name<'s>( &'s mut self, name: &str, ) -> Result<Slot<'s>, FieldError>
Returns a slot for initializing a field in the shape.
Sourcepub fn slot_by_index(&mut self, index: usize) -> Result<Slot<'_>, FieldError>
pub fn slot_by_index(&mut self, index: usize) -> Result<Slot<'_>, FieldError>
Returns a slot for initializing a field in the shape by index.
Sourcepub fn build_in_place(self)
pub fn build_in_place(self)
Asserts that every field has been initialized and forgets the Partial.
This method is only used when the origin is borrowed. If this method is not called, all fields will be freed when the Partial is dropped.
§Panics
This function will panic if:
- The origin is not borrowed (i.e., it’s heap allocated).
- Any field is not initialized.
Sourcepub fn build<T: Shapely>(self) -> T
pub fn build<T: Shapely>(self) -> T
Builds a value of type T
from the partial representation.
§Panics
This function will panic if:
- Not all the fields have been initialized.
- The generic type parameter T does not match the shape that this partial is building.
Sourcepub fn build_boxed<T: Shapely>(self) -> Box<T>
pub fn build_boxed<T: Shapely>(self) -> Box<T>
Build that partial into a boxed completed shape.
§Panics
This function will panic if:
- Not all the fields have been initialized.
- The generic type parameter T does not match the shape that this partial is building.
§Safety
This function uses unsafe code to create a Box from a raw pointer.
It’s safe because we’ve verified the initialization and shape matching,
and we forget self
to prevent double-freeing.
Sourcepub unsafe fn move_into(self, target: NonNull<u8>)
pub unsafe fn move_into(self, target: NonNull<u8>)
Moves the contents of this Partial
into a target memory location.
This function is useful when you need to place the fully initialized value into a specific memory address, such as when working with FFI or custom allocators.
§Safety
The target pointer must be valid and properly aligned, and must be large enough to hold the value. The caller is responsible for ensuring that the target memory is properly deallocated when it’s no longer needed.
Sourcepub fn set_variant_by_name(
&mut self,
variant_name: &str,
) -> Result<(), FieldError>
pub fn set_variant_by_name( &mut self, variant_name: &str, ) -> Result<(), FieldError>
Sets the variant of an enum by name.
§Errors
Returns an error if:
- The shape doesn’t represent an enum.
- No variant with the given name exists.
Sourcepub fn set_variant_by_index(
&mut self,
variant_index: usize,
) -> Result<(), FieldError>
pub fn set_variant_by_index( &mut self, variant_index: usize, ) -> Result<(), FieldError>
Sets the variant of an enum by index.
§Errors
Returns an error if:
- The shape doesn’t represent an enum.
- The index is out of bounds.
Sourcepub fn selected_variant_index(&self) -> Option<usize>
pub fn selected_variant_index(&self) -> Option<usize>
Returns the currently selected variant index, if any.
Sourcepub fn variant_field_by_name<'s>(
&'s mut self,
name: &str,
) -> Result<Slot<'s>, FieldError>
pub fn variant_field_by_name<'s>( &'s mut self, name: &str, ) -> Result<Slot<'s>, FieldError>
Get a slot for a field in the currently selected variant.
§Errors
Returns an error if:
- The shape doesn’t represent an enum.
- No variant has been selected yet.
- The field name doesn’t exist in the selected variant.
- The selected variant is a unit variant (which has no fields).