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>
impl<'mem, 'facet> Poke<'mem, 'facet>
Sourcepub fn new<T: Facet<'facet>>(t: &'mem mut T) -> Self
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.
Sourcepub unsafe fn from_raw_parts(data: PtrMut, shape: &'static Shape) -> Self
pub unsafe fn from_raw_parts(data: PtrMut, shape: &'static Shape) -> Self
Creates a mutable view from raw parts without any validation.
§Safety
datamust point to a valid, initialized value of the type described byshapedatamust be valid for the lifetime'mem
Sourcepub fn try_reborrow<'shorter>(&mut self) -> Option<Poke<'_, 'shorter>>where
'facet: 'shorter,
pub fn try_reborrow<'shorter>(&mut self) -> Option<Poke<'_, 'shorter>>where
'facet: 'shorter,
Attempts to reborrow this mutable view as an owned Poke.
This is useful when only &mut Poke is available (e.g. through DerefMut)
but an API requires ownership of Poke.
Returns Some if the underlying type can shrink the 'facet lifetime
(covariant or bivariant), or None otherwise.
Sourcepub fn into_struct(self) -> Result<PokeStruct<'mem, 'facet>, ReflectError>
pub fn into_struct(self) -> Result<PokeStruct<'mem, 'facet>, ReflectError>
Converts this into a PokeStruct if the value is a struct.
Sourcepub fn into_enum(self) -> Result<PokeEnum<'mem, 'facet>, ReflectError>
pub fn into_enum(self) -> Result<PokeEnum<'mem, 'facet>, ReflectError>
Converts this into a PokeEnum if the value is an enum.
Sourcepub fn into_list(self) -> Result<PokeList<'mem, 'facet>, ReflectError>
pub fn into_list(self) -> Result<PokeList<'mem, 'facet>, ReflectError>
Converts this into a PokeList if the value is a list.
Sourcepub fn get<T: Facet<'facet>>(&self) -> Result<&T, ReflectError>
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.
Sourcepub fn get_mut<T: Facet<'facet>>(&mut self) -> Result<&mut T, ReflectError>
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.
Sourcepub fn set<T: Facet<'facet>>(&mut self, value: T) -> Result<(), ReflectError>
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.
Sourcepub fn into_peek(self) -> Peek<'mem, 'facet>
pub fn into_peek(self) -> Peek<'mem, 'facet>
Consumes this Poke, returning a read-only Peek with the same 'mem lifetime.
Sourcepub fn at_path_mut(
self,
path: &Path,
) -> Result<Poke<'mem, 'facet>, PathAccessError>
pub fn at_path_mut( self, path: &Path, ) -> Result<Poke<'mem, 'facet>, PathAccessError>
Navigate to a nested value by following a Path, returning a mutable view.
Each PathStep in the path is applied in order, descending into
structs, enums, and lists. If any step cannot be applied, a
PathAccessError is returned with the step index and context.
§Supported steps
Field— struct fields and enum variant fields (afterVariant)Variant— verify enum variant matches, then allowFieldaccessIndex— list/array element accessOptionSome— navigate intoSome(T)or returnOptionIsNone
MapKey, MapValue, Deref, Inner, and Proxy are currently not
supported for mutable access and return
PathAccessError::MissingTarget.
§Errors
Returns PathAccessError if:
- The path’s root shape doesn’t match this value’s shape
- A step kind doesn’t apply to the current shape
- A field/list index is out of bounds
- An enum variant doesn’t match the runtime variant