pub struct CovariantPeek<'mem, 'facet> { /* private fields */ }reflect only.Expand description
A covariant wrapper around Peek for types that are covariant over their lifetime parameter.
Unlike Peek, which is invariant over 'facet for soundness reasons,
CovariantPeek is covariant over 'facet. This means a CovariantPeek<'mem, 'static>
can be used where a CovariantPeek<'mem, 'a> is expected.
§When to Use
Use CovariantPeek when you need to:
- Store multiple
Peekvalues with different lifetimes in a single collection - Pass
Peekvalues to functions expecting shorter lifetimes - Build data structures that wrap
Peekwithout forcing invariance on the wrapper
§Safety
CovariantPeek can only be constructed from types that are actually covariant.
The constructor verifies this at runtime by checking Shape::computed_variance.
This ensures that lifetime shrinking is always safe.
§Example
use facet::Facet;
use facet_reflect::{Peek, CovariantPeek};
#[derive(Facet)]
struct Data<'a> {
value: &'a str,
}
// Data<'a> is covariant in 'a because &'a str is covariant
let data = Data { value: "hello" };
let peek: Peek<'_, 'static> = Peek::new(&data);
// Convert to CovariantPeek - this verifies covariance
let covariant = CovariantPeek::new(peek).expect("Data is covariant");
// Now we can use it where shorter lifetimes are expected
fn use_shorter<'a>(p: CovariantPeek<'_, 'a>) {
let _ = p;
}
use_shorter(covariant);Implementations§
Source§impl<'mem, 'facet> CovariantPeek<'mem, 'facet>
impl<'mem, 'facet> CovariantPeek<'mem, 'facet>
Sourcepub fn new(peek: Peek<'mem, 'facet>) -> Option<CovariantPeek<'mem, 'facet>>
pub fn new(peek: Peek<'mem, 'facet>) -> Option<CovariantPeek<'mem, 'facet>>
Creates a new CovariantPeek from a Peek, verifying that the underlying type is covariant.
Returns None if the type is not covariant (i.e., it’s contravariant or invariant).
§Example
use facet::Facet;
use facet_reflect::{Peek, CovariantPeek};
// i32 has no lifetime parameters, so it's covariant
let value = 42i32;
let peek = Peek::new(&value);
let covariant = CovariantPeek::new(peek);
assert!(covariant.is_some());Sourcepub fn new_unchecked(peek: Peek<'mem, 'facet>) -> CovariantPeek<'mem, 'facet>
pub fn new_unchecked(peek: Peek<'mem, 'facet>) -> CovariantPeek<'mem, 'facet>
Creates a new CovariantPeek from a Peek, panicking if the type is not covariant.
§Panics
Panics if the underlying type is not covariant.
§Example
use facet::Facet;
use facet_reflect::{Peek, CovariantPeek};
let value = "hello";
let peek = Peek::new(&value);
let covariant = CovariantPeek::new_unchecked(peek); // Will succeedSourcepub fn from_ref<T>(t: &'mem T) -> Option<CovariantPeek<'mem, 'facet>>
pub fn from_ref<T>(t: &'mem T) -> Option<CovariantPeek<'mem, 'facet>>
Creates a CovariantPeek directly from a covariant Facet type.
Returns None if the type is not covariant.
§Example
use facet::Facet;
use facet_reflect::CovariantPeek;
let value = 42i32;
let covariant = CovariantPeek::from_ref(&value);
assert!(covariant.is_some());Methods from Deref<Target = Peek<'mem, 'facet>>§
Sourcepub fn variance(&self) -> Variance
pub fn variance(&self) -> Variance
Returns the computed variance of the underlying type.
This walks the type’s fields to determine if the type is covariant, contravariant, or invariant over its lifetime parameter.
Sourcepub fn vtable(&self) -> VTableErased
pub fn vtable(&self) -> VTableErased
Returns the vtable
Sourcepub fn id(&self) -> ValueId
pub fn id(&self) -> ValueId
Returns a unique identifier for this value, usable for cycle detection
Sourcepub fn ptr_eq(&self, other: &Peek<'_, '_>) -> bool
pub fn ptr_eq(&self, other: &Peek<'_, '_>) -> bool
Returns true if the two values are pointer-equal
Sourcepub fn partial_eq(&self, other: &Peek<'_, '_>) -> Result<bool, ReflectError>
pub fn partial_eq(&self, other: &Peek<'_, '_>) -> Result<bool, ReflectError>
Returns true if this scalar is equal to the other scalar
§Returns
false if equality comparison is not supported for this scalar type
Sourcepub fn partial_cmp(
&self,
other: &Peek<'_, '_>,
) -> Result<Option<Ordering>, ReflectError>
pub fn partial_cmp( &self, other: &Peek<'_, '_>, ) -> Result<Option<Ordering>, ReflectError>
Compares this scalar with another and returns their ordering
§Returns
None if comparison is not supported for this scalar type
Sourcepub fn hash(&self, hasher: &mut dyn Hasher) -> Result<(), ReflectError>
pub fn hash(&self, hasher: &mut dyn Hasher) -> Result<(), ReflectError>
Hashes this scalar using the vtable hash function.
§Returns
Err if hashing is not supported for this scalar type, Ok otherwise
Sourcepub fn structural_hash<H>(&self, hasher: &mut H)where
H: Hasher,
pub fn structural_hash<H>(&self, hasher: &mut H)where
H: Hasher,
Computes a structural hash of this value.
Unlike hash, this method recursively traverses the structure
and hashes each component, making it work for types that don’t implement Hash.
For scalars with a vtable hash function, it uses that. For compound types (structs, enums, lists, etc.), it recursively hashes the structure.
This is useful for Merkle-tree style hashing where you want to compare subtrees for equality based on their structural content.
Sourcepub fn scalar_type(&self) -> Option<ScalarType>
pub fn scalar_type(&self) -> Option<ScalarType>
Get the scalar type if set.
Sourcepub fn get<T>(&self) -> Result<&'mem T, ReflectError>
pub fn get<T>(&self) -> Result<&'mem T, ReflectError>
Sourcepub fn as_str(&self) -> Option<&'mem str>
pub fn as_str(&self) -> Option<&'mem str>
Try to get the value as a string if it’s a string type Returns None if the value is not a string or couldn’t be extracted
Sourcepub fn as_bytes(&self) -> Option<&'mem [u8]>
pub fn as_bytes(&self) -> Option<&'mem [u8]>
Try to get the value as a byte slice if it’s a &u8 type Returns None if the value is not a byte slice or couldn’t be extracted
Sourcepub fn custom_serialization(
&self,
field: Field,
) -> Result<OwnedPeek<'mem>, ReflectError>
Available on crate feature alloc only.
pub fn custom_serialization( &self, field: Field, ) -> Result<OwnedPeek<'mem>, ReflectError>
alloc only.Performs custom serialization of the current peek using the provided field’s metadata.
Returns an OwnedPeek that points to the final type that should be serialized in place
of the current peek.
Sourcepub fn custom_serialization_from_shape(
&self,
) -> Result<Option<OwnedPeek<'mem>>, ReflectError>
Available on crate feature alloc only.
pub fn custom_serialization_from_shape( &self, ) -> Result<Option<OwnedPeek<'mem>>, ReflectError>
alloc only.Returns an OwnedPeek using the shape’s container-level proxy for serialization.
This is used when a type has #[facet(proxy = ProxyType)] at the container level.
Unlike field-level proxies which are checked via custom_serialization(field),
this method checks the Shape itself for a proxy definition.
Returns None if the shape has no container-level proxy.
Trait Implementations§
Source§impl<'mem, 'facet> Clone for CovariantPeek<'mem, 'facet>
impl<'mem, 'facet> Clone for CovariantPeek<'mem, 'facet>
Source§fn clone(&self) -> CovariantPeek<'mem, 'facet>
fn clone(&self) -> CovariantPeek<'mem, 'facet>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more