Poke

Struct Poke 

Source
pub struct Poke<'mem, 'facet> { /* private fields */ }
Expand description

A mutable view into a value with runtime type information.

Poke provides reflection capabilities for mutating values at runtime. It is the mutable counterpart to Peek.

§Wholesale Replacement vs Field Mutation

Poke can be created for any type. Replacing a value wholesale with Poke::set is always safe - it just drops the old value and writes the new one.

However, mutating individual struct fields via PokeStruct::set_field requires the struct to be marked as POD (#[facet(pod)]). This is because field mutation could violate struct-level invariants.

§Lifetime Parameters

  • 'mem: The memory lifetime - how long the underlying data is valid
  • 'facet: The type’s lifetime parameter (for types like &'a str)

§Example

// Wholesale replacement works on any type
let mut s = String::from("hello");
let mut poke = Poke::new(&mut s);
poke.set(String::from("world")).unwrap();

// Field mutation requires #[facet(pod)]
#[derive(Facet)]
#[facet(pod)]
struct Point { x: i32, y: i32 }

let mut point = Point { x: 1, y: 2 };
let mut poke = Poke::new(&mut point);
poke.into_struct().unwrap().set_field_by_name("x", 10i32).unwrap();
assert_eq!(point.x, 10);

Implementations§

Source§

impl<'mem, 'facet> Poke<'mem, 'facet>

Source

pub fn new<T: Facet<'facet>>(t: &'mem mut T) -> Self

Creates a mutable view over a T value.

This always succeeds - wholesale replacement via Poke::set is safe for any type. The POD check happens when you try to mutate individual struct fields.

Source

pub unsafe fn from_raw_parts(data: PtrMut, shape: &'static Shape) -> Self

Creates a mutable view from raw parts without any validation.

§Safety
  • data must point to a valid, initialized value of the type described by shape
  • data must be valid for the lifetime 'mem
Source

pub fn shape(&self) -> &'static Shape

Returns the shape of the value.

Source

pub fn data(&self) -> PtrConst

Returns a const pointer to the underlying data.

Source

pub fn data_mut(&mut self) -> PtrMut

Returns a mutable pointer to the underlying data.

Source

pub fn is_struct(&self) -> bool

Returns true if this value is a struct.

Source

pub fn is_enum(&self) -> bool

Returns true if this value is an enum.

Source

pub fn is_scalar(&self) -> bool

Returns true if this value is a scalar (primitive type).

Source

pub fn into_struct(self) -> Result<PokeStruct<'mem, 'facet>, ReflectError>

Converts this into a PokeStruct if the value is a struct.

Source

pub fn into_enum(self) -> Result<PokeEnum<'mem, 'facet>, ReflectError>

Converts this into a PokeEnum if the value is an enum.

Source

pub fn get<T: Facet<'facet>>(&self) -> Result<&T, ReflectError>

Gets a reference to the underlying value.

Returns an error if the shape doesn’t match T.

Source

pub fn get_mut<T: Facet<'facet>>(&mut self) -> Result<&mut T, ReflectError>

Gets a mutable reference to the underlying value.

Returns an error if the shape doesn’t match T.

Source

pub fn set<T: Facet<'facet>>(&mut self, value: T) -> Result<(), ReflectError>

Sets the value to a new value.

This replaces the entire value. The new value must have the same shape.

Source

pub fn as_peek(&self) -> Peek<'_, 'facet>

Converts this Poke into a read-only Peek.

Trait Implementations§

Source§

impl Debug for Poke<'_, '_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'mem, 'facet> Freeze for Poke<'mem, 'facet>

§

impl<'mem, 'facet> RefUnwindSafe for Poke<'mem, 'facet>

§

impl<'mem, 'facet> !Send for Poke<'mem, 'facet>

§

impl<'mem, 'facet> !Sync for Poke<'mem, 'facet>

§

impl<'mem, 'facet> Unpin for Poke<'mem, 'facet>

§

impl<'mem, 'facet> !UnwindSafe for Poke<'mem, 'facet>

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.