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 const fn is_tuple(&self) -> bool
pub const fn is_tuple(&self) -> bool
Returns true if this value is a tuple (anonymous (A, B, ...)).
Note: tuple structs (named struct Foo(A, B);) report false here; they match
is_struct.
Sourcepub const fn is_list(&self) -> bool
pub const fn is_list(&self) -> bool
Returns true if this value is a list (variable-length, homogeneous, e.g. Vec<T>).
Sourcepub const fn is_array(&self) -> bool
pub const fn is_array(&self) -> bool
Returns true if this value is a fixed-size array (e.g. [T; N]).
Sourcepub const fn is_list_like(&self) -> bool
pub const fn is_list_like(&self) -> bool
Returns true if this value is a list, array, or slice
(the set accepted by into_list_like).
Sourcepub const fn is_pointer(&self) -> bool
pub const fn is_pointer(&self) -> bool
Returns true if this value is a (smart) pointer.
Sourcepub const fn is_ndarray(&self) -> bool
pub const fn is_ndarray(&self) -> bool
Returns true if this value is an n-dimensional array.
Sourcepub const fn is_dynamic_value(&self) -> bool
pub const fn is_dynamic_value(&self) -> bool
Returns true if this value is a dynamic value
(e.g. facet_value::Value — runtime-kind-dispatched).
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 into_list_like(self) -> Result<PokeListLike<'mem, 'facet>, ReflectError>
pub fn into_list_like(self) -> Result<PokeListLike<'mem, 'facet>, ReflectError>
Converts this into a PokeListLike if the value is a list, array, or slice.
Sourcepub fn into_map(self) -> Result<PokeMap<'mem, 'facet>, ReflectError>
pub fn into_map(self) -> Result<PokeMap<'mem, 'facet>, ReflectError>
Converts this into a PokeMap if the value is a map.
Sourcepub fn into_set(self) -> Result<PokeSet<'mem, 'facet>, ReflectError>
pub fn into_set(self) -> Result<PokeSet<'mem, 'facet>, ReflectError>
Converts this into a PokeSet if the value is a set.
Sourcepub fn into_option(self) -> Result<PokeOption<'mem, 'facet>, ReflectError>
pub fn into_option(self) -> Result<PokeOption<'mem, 'facet>, ReflectError>
Converts this into a PokeOption if the value is an option.
Sourcepub fn into_result(self) -> Result<PokeResult<'mem, 'facet>, ReflectError>
pub fn into_result(self) -> Result<PokeResult<'mem, 'facet>, ReflectError>
Converts this into a PokeResult if the value is a result.
Sourcepub fn into_tuple(self) -> Result<PokeTuple<'mem, 'facet>, ReflectError>
pub fn into_tuple(self) -> Result<PokeTuple<'mem, 'facet>, ReflectError>
Converts this into a PokeTuple if the value is a tuple (or tuple struct).
Sourcepub fn into_pointer(self) -> Result<PokePointer<'mem, 'facet>, ReflectError>
pub fn into_pointer(self) -> Result<PokePointer<'mem, 'facet>, ReflectError>
Converts this into a PokePointer if the value is a pointer.
Sourcepub fn into_ndarray(self) -> Result<PokeNdArray<'mem, 'facet>, ReflectError>
pub fn into_ndarray(self) -> Result<PokeNdArray<'mem, 'facet>, ReflectError>
Converts this into a PokeNdArray if the value is an n-dimensional array.
Sourcepub fn into_dynamic_value(
self,
) -> Result<PokeDynamicValue<'mem, 'facet>, ReflectError>
pub fn into_dynamic_value( self, ) -> Result<PokeDynamicValue<'mem, 'facet>, ReflectError>
Converts this into a PokeDynamicValue if the value is a dynamic value.
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