Struct Partial

Source
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<'_>

Source

pub fn alloc(shape: ShapeDesc) -> Self

Allocates a partial on the heap for the given shape descriptor.

Source

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().

Source

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)

Source

pub fn hashmap_slot(&mut self, size_hint: Option<usize>) -> Option<HashMapSlot>

Returns a slot for a HashMap field in the shape.

Source

pub fn hashmap_iter(&self) -> Option<HashMapIter>

Returns an iterator over the key-value pairs in a HashMap

Source

pub fn scalar_slot(&mut self) -> Option<Slot<'_>>

Returns a slot for assigning this whole shape as a scalar

Source

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.

Source

pub fn slot_by_index(&mut self, index: usize) -> Result<Slot<'_>, FieldError>

Returns a slot for initializing a field in the shape by index.

Source

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.
Source

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.
Source

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.

Source

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.

Source

pub fn shape(&self) -> ShapeDesc

Returns the shape we’re currently building.

Source

pub fn addr(&self) -> NonNull<u8>

Returns the address of the value we’re building in memory.

Source

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.
Source

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.
Source

pub fn selected_variant_index(&self) -> Option<usize>

Returns the currently selected variant index, if any.

Source

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).

Trait Implementations§

Source§

impl Drop for Partial<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'s> Freeze for Partial<'s>

§

impl<'s> RefUnwindSafe for Partial<'s>

§

impl<'s> !Send for Partial<'s>

§

impl<'s> !Sync for Partial<'s>

§

impl<'s> Unpin for Partial<'s>

§

impl<'s> !UnwindSafe for Partial<'s>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.