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