facet_reflect/peek/
value.rs

1use core::{cmp::Ordering, marker::PhantomData, ptr::NonNull};
2#[cfg(feature = "alloc")]
3use facet_core::Field;
4use facet_core::{
5    Def, Facet, PointerType, PtrConst, Shape, StructKind, Type, TypeNameOpts, UserType,
6    VTableErased, Variance,
7};
8
9use crate::{PeekNdArray, PeekSet, ReflectError, ScalarType};
10
11use super::{
12    ListLikeDef, PeekDynamicValue, PeekEnum, PeekList, PeekListLike, PeekMap, PeekOption,
13    PeekPointer, PeekResult, PeekStruct, PeekTuple, tuple::TupleType,
14};
15
16#[cfg(feature = "alloc")]
17use super::OwnedPeek;
18
19/// A unique identifier for a peek value
20#[derive(Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash)]
21pub struct ValueId {
22    pub(crate) shape: &'static Shape,
23    pub(crate) ptr: *const u8,
24}
25
26impl ValueId {
27    #[inline]
28    pub(crate) fn new(shape: &'static Shape, ptr: *const u8) -> Self {
29        Self { shape, ptr }
30    }
31}
32
33impl core::fmt::Display for ValueId {
34    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
35        write!(f, "{}@{:p}", self.shape, self.ptr)
36    }
37}
38
39impl core::fmt::Debug for ValueId {
40    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
41        core::fmt::Display::fmt(self, f)
42    }
43}
44
45/// A read-only view into a value with runtime type information.
46///
47/// `Peek` provides reflection capabilities for reading values at runtime.
48/// If the value is a struct, you can read its fields; if it's an enum,
49/// you can determine which variant is selected; if it's a scalar, you can
50/// extract a concrete value.
51///
52/// # Lifetime Parameters
53///
54/// - `'mem`: The memory lifetime - how long the underlying data is valid
55/// - `'facet`: The type's lifetime parameter (for types like `&'a str`)
56///
57/// # Variance and Soundness
58///
59/// `Peek` is **invariant** over `'facet`. This is required for soundness:
60/// if `Peek` were covariant, it would be possible to launder lifetimes
61/// through reflection, leading to use-after-free bugs with types like
62/// `fn(&'a str)`. See [issue #1168](https://github.com/facet-rs/facet/issues/1168).
63///
64/// The underlying type's variance is tracked in [`Shape::variance`], which
65/// can be used for future variance-aware APIs.
66#[allow(clippy::type_complexity)]
67#[derive(Clone, Copy)]
68pub struct Peek<'mem, 'facet> {
69    /// Underlying data
70    pub(crate) data: PtrConst,
71
72    /// Shape of the value
73    pub(crate) shape: &'static Shape,
74
75    // Invariant over 'facet: Peek<'mem, 'a> cannot be cast to Peek<'mem, 'b> even if 'a: 'b.
76    //
77    // This is REQUIRED for soundness! If Peek were covariant over 'facet, we could:
78    // 1. Create Peek<'mem, 'static> from FnWrapper<'static> (contains fn(&'static str))
79    // 2. Use covariance to cast it to Peek<'mem, 'short>
80    // 3. Call get::<FnWrapper<'short>>() to get &FnWrapper<'short>
81    // 4. This would allow calling the function with a &'short str that goes out of scope
82    //    while the original function pointer still holds it as 'static
83    //
84    // The fn(&'a ()) -> &'a () pattern makes this type invariant over 'facet.
85    // The &'mem () makes this type covariant over 'mem (safe because we only read through it).
86    // See: https://github.com/facet-rs/facet/issues/1168
87    _invariant: PhantomData<(&'mem (), fn(&'facet ()) -> &'facet ())>,
88}
89
90impl<'mem, 'facet> Peek<'mem, 'facet> {
91    /// Returns a read-only view over a `T` value.
92    pub fn new<T: Facet<'facet> + ?Sized>(t: &'mem T) -> Self {
93        Self {
94            data: PtrConst::new(NonNull::from(t).as_ptr()),
95            shape: T::SHAPE,
96            _invariant: PhantomData,
97        }
98    }
99
100    /// Returns a read-only view over a value (given its shape), trusting you
101    /// that those two match.
102    ///
103    /// # Safety
104    ///
105    /// This function is unsafe because it doesn't check if the provided data
106    /// and shape are compatible. The caller must ensure that the data is valid
107    /// for the given shape.
108    pub unsafe fn unchecked_new(data: PtrConst, shape: &'static Shape) -> Self {
109        Self {
110            data,
111            shape,
112            _invariant: PhantomData,
113        }
114    }
115
116    // =============================================================================
117    // Variance-aware lifetime transformation methods
118    // =============================================================================
119
120    /// Returns the computed variance of the underlying type.
121    ///
122    /// This walks the type's fields to determine if the type is covariant,
123    /// contravariant, or invariant over its lifetime parameter.
124    #[inline]
125    pub fn variance(&self) -> Variance {
126        self.shape.computed_variance()
127    }
128
129    /// Shrinks the `'facet` lifetime parameter.
130    ///
131    /// This is safe for covariant types: if data is valid for `'static`,
132    /// it's also valid for any shorter lifetime `'shorter`.
133    ///
134    /// # Panics
135    ///
136    /// Panics if the type is not covariant (i.e., if shrinking would be unsound).
137    #[inline]
138    pub fn shrink_lifetime<'shorter>(self) -> Peek<'mem, 'shorter>
139    where
140        'facet: 'shorter,
141    {
142        self.try_shrink_lifetime()
143            .expect("shrink_lifetime requires a covariant type")
144    }
145
146    /// Tries to shrink the `'facet` lifetime parameter.
147    ///
148    /// Returns `Some` if the type is covariant (shrinking is safe),
149    /// or `None` if the type is invariant or contravariant.
150    #[inline]
151    pub fn try_shrink_lifetime<'shorter>(self) -> Option<Peek<'mem, 'shorter>>
152    where
153        'facet: 'shorter,
154    {
155        if self.variance() == Variance::Covariant {
156            Some(Peek {
157                data: self.data,
158                shape: self.shape,
159                _invariant: PhantomData,
160            })
161        } else {
162            None
163        }
164    }
165
166    /// Grows the `'facet` lifetime parameter.
167    ///
168    /// This is safe for contravariant types: if a function accepts `'short`,
169    /// it can also accept `'longer` (a longer lifetime is more restrictive).
170    ///
171    /// # Panics
172    ///
173    /// Panics if the type is not contravariant (i.e., if growing would be unsound).
174    #[inline]
175    pub fn grow_lifetime<'longer>(self) -> Peek<'mem, 'longer>
176    where
177        'longer: 'facet,
178    {
179        self.try_grow_lifetime()
180            .expect("grow_lifetime requires a contravariant type")
181    }
182
183    /// Tries to grow the `'facet` lifetime parameter.
184    ///
185    /// Returns `Some` if the type is contravariant (growing is safe),
186    /// or `None` if the type is invariant or covariant.
187    #[inline]
188    pub fn try_grow_lifetime<'longer>(self) -> Option<Peek<'mem, 'longer>>
189    where
190        'longer: 'facet,
191    {
192        if self.variance() == Variance::Contravariant {
193            Some(Peek {
194                data: self.data,
195                shape: self.shape,
196                _invariant: PhantomData,
197            })
198        } else {
199            None
200        }
201    }
202
203    /// Returns the vtable
204    #[inline(always)]
205    pub fn vtable(&self) -> VTableErased {
206        self.shape.vtable
207    }
208
209    /// Returns a unique identifier for this value, usable for cycle detection
210    #[inline]
211    pub fn id(&self) -> ValueId {
212        ValueId::new(self.shape, self.data.raw_ptr())
213    }
214
215    /// Returns true if the two values are pointer-equal
216    #[inline]
217    pub fn ptr_eq(&self, other: &Peek<'_, '_>) -> bool {
218        self.data.raw_ptr() == other.data.raw_ptr()
219    }
220
221    /// Returns true if this scalar is equal to the other scalar
222    ///
223    /// # Returns
224    ///
225    /// `false` if equality comparison is not supported for this scalar type
226    #[inline]
227    pub fn partial_eq(&self, other: &Peek<'_, '_>) -> Result<bool, ReflectError> {
228        if self.shape != other.shape {
229            return Err(ReflectError::WrongShape {
230                expected: self.shape,
231                actual: other.shape,
232            });
233        }
234
235        if let Some(result) = unsafe { self.shape.call_partial_eq(self.data, other.data) } {
236            return Ok(result);
237        }
238
239        Err(ReflectError::OperationFailed {
240            shape: self.shape(),
241            operation: "partial_eq",
242        })
243    }
244
245    /// Compares this scalar with another and returns their ordering
246    ///
247    /// # Returns
248    ///
249    /// `None` if comparison is not supported for this scalar type
250    #[inline]
251    pub fn partial_cmp(&self, other: &Peek<'_, '_>) -> Result<Option<Ordering>, ReflectError> {
252        if self.shape != other.shape {
253            return Err(ReflectError::WrongShape {
254                expected: self.shape,
255                actual: other.shape,
256            });
257        }
258
259        if let Some(result) = unsafe { self.shape.call_partial_cmp(self.data, other.data) } {
260            return Ok(result);
261        }
262
263        Err(ReflectError::OperationFailed {
264            shape: self.shape(),
265            operation: "partial_cmp",
266        })
267    }
268
269    /// Hashes this scalar using the vtable hash function.
270    ///
271    /// # Returns
272    ///
273    /// `Err` if hashing is not supported for this scalar type, `Ok` otherwise
274    #[inline(always)]
275    pub fn hash(&self, hasher: &mut dyn core::hash::Hasher) -> Result<(), ReflectError> {
276        let mut proxy = facet_core::HashProxy::new(hasher);
277        if unsafe { self.shape.call_hash(self.data, &mut proxy) }.is_some() {
278            return Ok(());
279        }
280
281        Err(ReflectError::OperationFailed {
282            shape: self.shape(),
283            operation: "hash",
284        })
285    }
286
287    /// Computes a structural hash of this value.
288    ///
289    /// Unlike [`hash`](Self::hash), this method recursively traverses the structure
290    /// and hashes each component, making it work for types that don't implement `Hash`.
291    ///
292    /// For scalars with a vtable hash function, it uses that. For compound types
293    /// (structs, enums, lists, etc.), it recursively hashes the structure.
294    ///
295    /// This is useful for Merkle-tree style hashing where you want to compare
296    /// subtrees for equality based on their structural content.
297    pub fn structural_hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
298        use core::hash::Hash;
299
300        // First, hash the shape's type identifier for type discrimination
301        self.shape.id.hash(hasher);
302
303        // Try vtable hash first for scalars
304        let mut proxy = facet_core::HashProxy::new(hasher);
305        if unsafe { self.shape.call_hash(self.data, &mut proxy) }.is_some() {
306            return;
307        }
308
309        // Otherwise, traverse the structure recursively
310        match self.shape.ty {
311            Type::User(UserType::Struct(struct_type)) => {
312                // Hash struct kind
313                (struct_type.kind as u8).hash(hasher);
314
315                // Hash each field, skipping metadata fields
316                for field in struct_type.fields {
317                    // Skip metadata fields - they don't affect structural identity
318                    if field.is_metadata() {
319                        continue;
320                    }
321
322                    // Hash field name
323                    field.name.hash(hasher);
324
325                    // Get field value and hash it recursively
326                    let field_offset = field.offset;
327                    let field_shape = field.shape();
328                    let field_ptr = unsafe { self.data.field(field_offset) };
329                    let field_peek = unsafe { Peek::unchecked_new(field_ptr, field_shape) };
330                    field_peek.structural_hash(hasher);
331                }
332            }
333
334            Type::User(UserType::Enum(_enum_type)) => {
335                // Get the discriminant and variant
336                if let Ok(peek_enum) = self.into_enum()
337                    && let Ok(variant) = peek_enum.active_variant()
338                {
339                    // Hash variant name
340                    variant.name.hash(hasher);
341
342                    // Hash variant payload based on kind
343                    match variant.data.kind {
344                        StructKind::Unit => {
345                            // No payload to hash
346                        }
347                        StructKind::TupleStruct | StructKind::Tuple => {
348                            // Hash tuple fields (no names)
349                            use super::HasFields;
350                            for (_field, peek) in peek_enum.fields() {
351                                peek.structural_hash(hasher);
352                            }
353                        }
354                        StructKind::Struct => {
355                            // Hash named fields
356                            use super::HasFields;
357                            for (field, peek) in peek_enum.fields() {
358                                field.name.hash(hasher);
359                                peek.structural_hash(hasher);
360                            }
361                        }
362                    }
363                }
364            }
365
366            _ => {
367                // Handle Def-based types
368                match self.shape.def {
369                    Def::List(_) | Def::Array(_) | Def::Slice(_) => {
370                        if let Ok(list_like) = self.into_list_like() {
371                            // Hash length
372                            list_like.len().hash(hasher);
373
374                            // Hash each element
375                            for elem in list_like.iter() {
376                                elem.structural_hash(hasher);
377                            }
378                        }
379                    }
380
381                    Def::Map(_) => {
382                        if let Ok(map) = self.into_map() {
383                            // Hash length
384                            map.len().hash(hasher);
385
386                            // Hash each key-value pair
387                            for (key, value) in map.iter() {
388                                key.structural_hash(hasher);
389                                value.structural_hash(hasher);
390                            }
391                        }
392                    }
393
394                    Def::Set(_) => {
395                        if let Ok(set) = self.into_set() {
396                            // Hash length
397                            set.len().hash(hasher);
398
399                            // Hash each element
400                            for elem in set.iter() {
401                                elem.structural_hash(hasher);
402                            }
403                        }
404                    }
405
406                    Def::Option(_) => {
407                        if let Ok(opt) = self.into_option() {
408                            if let Some(inner) = opt.value() {
409                                true.hash(hasher);
410                                inner.structural_hash(hasher);
411                            } else {
412                                false.hash(hasher);
413                            }
414                        }
415                    }
416
417                    Def::Result(_) => {
418                        if let Ok(result) = self.into_result() {
419                            if result.is_ok() {
420                                0u8.hash(hasher);
421                                if let Some(ok_val) = result.ok() {
422                                    ok_val.structural_hash(hasher);
423                                }
424                            } else {
425                                1u8.hash(hasher);
426                                if let Some(err_val) = result.err() {
427                                    err_val.structural_hash(hasher);
428                                }
429                            }
430                        }
431                    }
432
433                    Def::Pointer(_) => {
434                        if let Ok(ptr) = self.into_pointer()
435                            && let Some(inner) = ptr.borrow_inner()
436                        {
437                            inner.structural_hash(hasher);
438                        }
439                    }
440
441                    Def::DynamicValue(_) => {
442                        if let Ok(dyn_val) = self.into_dynamic_value() {
443                            // Hash based on dynamic value kind
444                            dyn_val.structural_hash_inner(hasher);
445                        }
446                    }
447
448                    Def::NdArray(_) => {
449                        // For ndarray, hash the dimensions and data
450                        if let Ok(arr) = self.into_ndarray() {
451                            let n_dim = arr.n_dim();
452                            n_dim.hash(hasher);
453                            for i in 0..n_dim {
454                                if let Some(dim) = arr.dim(i) {
455                                    dim.hash(hasher);
456                                }
457                            }
458                            // Hash each element
459                            let count = arr.count();
460                            for i in 0..count {
461                                if let Some(elem) = arr.get(i) {
462                                    elem.structural_hash(hasher);
463                                }
464                            }
465                        }
466                    }
467
468                    Def::Scalar | Def::Undefined | _ => {
469                        panic!(
470                            "structural_hash: type {} has no Hash impl and cannot be structurally hashed",
471                            self.shape
472                        );
473                    }
474                }
475            }
476        }
477    }
478
479    /// Returns the type name of this scalar
480    ///
481    /// # Arguments
482    ///
483    /// * `f` - A mutable reference to a `core::fmt::Formatter`
484    /// * `opts` - The `TypeNameOpts` to use for formatting
485    ///
486    /// # Returns
487    ///
488    /// The result of the type name formatting
489    #[inline(always)]
490    pub fn type_name(
491        &self,
492        f: &mut core::fmt::Formatter<'_>,
493        opts: TypeNameOpts,
494    ) -> core::fmt::Result {
495        if let Some(type_name_fn) = self.shape.type_name {
496            type_name_fn(self.shape, f, opts)
497        } else {
498            write!(f, "{}", self.shape.type_identifier)
499        }
500    }
501
502    /// Returns the shape
503    #[inline(always)]
504    pub const fn shape(&self) -> &'static Shape {
505        self.shape
506    }
507
508    /// Returns the data
509    #[inline(always)]
510    pub const fn data(&self) -> PtrConst {
511        self.data
512    }
513
514    /// Get the scalar type if set.
515    #[inline]
516    pub fn scalar_type(&self) -> Option<ScalarType> {
517        ScalarType::try_from_shape(self.shape)
518    }
519
520    /// Read the value from memory into a Rust value.
521    ///
522    /// # Panics
523    ///
524    /// Panics if the shape doesn't match the type `T`.
525    #[inline]
526    pub fn get<T: Facet<'facet> + ?Sized>(&self) -> Result<&'mem T, ReflectError> {
527        if self.shape != T::SHAPE {
528            Err(ReflectError::WrongShape {
529                expected: self.shape,
530                actual: T::SHAPE,
531            })
532        } else {
533            Ok(unsafe { self.data.get::<T>() })
534        }
535    }
536
537    /// Try to get the value as a string if it's a string type
538    /// Returns None if the value is not a string or couldn't be extracted
539    pub fn as_str(&self) -> Option<&'mem str> {
540        let peek = self.innermost_peek();
541        // ScalarType::Str matches both bare `str` and `&str`.
542        // For bare `str` (not a pointer), data points to str bytes directly.
543        // For `&str`, let it fall through to the pointer handler below.
544        if let Some(ScalarType::Str) = peek.scalar_type()
545            && !matches!(peek.shape.ty, Type::Pointer(_))
546        {
547            // Bare `str`: data is a wide pointer to str bytes.
548            // get::<str>() creates a &str reference to that data.
549            return unsafe { Some(peek.data.get::<str>()) };
550        }
551        #[cfg(feature = "alloc")]
552        if let Some(ScalarType::String) = peek.scalar_type() {
553            return unsafe { Some(peek.data.get::<alloc::string::String>().as_str()) };
554        }
555
556        // Handle references, including nested references like &&str
557        if let Type::Pointer(PointerType::Reference(vpt)) = peek.shape.ty {
558            let target_shape = vpt.target;
559
560            // Check if this is a nested reference (&&str) first
561            if let Type::Pointer(PointerType::Reference(inner_vpt)) = target_shape.ty {
562                let inner_target_shape = inner_vpt.target;
563                if let Some(ScalarType::Str) = ScalarType::try_from_shape(inner_target_shape) {
564                    // For &&str, we need to dereference twice.
565                    // Read the outer reference (8 bytes) as a pointer to &str, then dereference
566                    let outer_ptr: *const *const &str =
567                        unsafe { peek.data.as_ptr::<*const &str>() };
568                    let inner_ref: &str = unsafe { **outer_ptr };
569                    return Some(inner_ref);
570                }
571            } else if let Some(ScalarType::Str) = ScalarType::try_from_shape(target_shape)
572                && !matches!(target_shape.ty, Type::Pointer(_))
573            {
574                // Simple case: &str (but only if target is not a pointer itself)
575                return unsafe { Some(peek.data.get::<&str>()) };
576            }
577        }
578        None
579    }
580
581    /// Try to get the value as a byte slice if it's a &[u8] type
582    /// Returns None if the value is not a byte slice or couldn't be extracted
583    #[inline]
584    pub fn as_bytes(&self) -> Option<&'mem [u8]> {
585        // Check if it's a direct &[u8]
586        if let Type::Pointer(PointerType::Reference(vpt)) = self.shape.ty {
587            let target_shape = vpt.target;
588            if let Def::Slice(sd) = target_shape.def
589                && sd.t().is_type::<u8>()
590            {
591                unsafe { return Some(self.data.get::<&[u8]>()) }
592            }
593        }
594        None
595    }
596
597    /// Tries to identify this value as a struct
598    #[inline]
599    pub fn into_struct(self) -> Result<PeekStruct<'mem, 'facet>, ReflectError> {
600        if let Type::User(UserType::Struct(ty)) = self.shape.ty {
601            Ok(PeekStruct { value: self, ty })
602        } else {
603            Err(ReflectError::WasNotA {
604                expected: "struct",
605                actual: self.shape,
606            })
607        }
608    }
609
610    /// Tries to identify this value as an enum
611    #[inline]
612    pub fn into_enum(self) -> Result<PeekEnum<'mem, 'facet>, ReflectError> {
613        if let Type::User(UserType::Enum(ty)) = self.shape.ty {
614            Ok(PeekEnum { value: self, ty })
615        } else {
616            Err(ReflectError::WasNotA {
617                expected: "enum",
618                actual: self.shape,
619            })
620        }
621    }
622
623    /// Tries to identify this value as a map
624    #[inline]
625    pub fn into_map(self) -> Result<PeekMap<'mem, 'facet>, ReflectError> {
626        if let Def::Map(def) = self.shape.def {
627            Ok(PeekMap { value: self, def })
628        } else {
629            Err(ReflectError::WasNotA {
630                expected: "map",
631                actual: self.shape,
632            })
633        }
634    }
635
636    /// Tries to identify this value as a set
637    #[inline]
638    pub fn into_set(self) -> Result<PeekSet<'mem, 'facet>, ReflectError> {
639        if let Def::Set(def) = self.shape.def {
640            Ok(PeekSet { value: self, def })
641        } else {
642            Err(ReflectError::WasNotA {
643                expected: "set",
644                actual: self.shape,
645            })
646        }
647    }
648
649    /// Tries to identify this value as a list
650    #[inline]
651    pub fn into_list(self) -> Result<PeekList<'mem, 'facet>, ReflectError> {
652        if let Def::List(def) = self.shape.def {
653            return Ok(PeekList { value: self, def });
654        }
655
656        Err(ReflectError::WasNotA {
657            expected: "list",
658            actual: self.shape,
659        })
660    }
661
662    /// Tries to identify this value as a ndarray
663    #[inline]
664    pub fn into_ndarray(self) -> Result<PeekNdArray<'mem, 'facet>, ReflectError> {
665        if let Def::NdArray(def) = self.shape.def {
666            return Ok(PeekNdArray { value: self, def });
667        }
668
669        Err(ReflectError::WasNotA {
670            expected: "ndarray",
671            actual: self.shape,
672        })
673    }
674
675    /// Tries to identify this value as a list, array or slice
676    #[inline]
677    pub fn into_list_like(self) -> Result<PeekListLike<'mem, 'facet>, ReflectError> {
678        match self.shape.def {
679            Def::List(def) => Ok(PeekListLike::new(self, ListLikeDef::List(def))),
680            Def::Array(def) => Ok(PeekListLike::new(self, ListLikeDef::Array(def))),
681            Def::Slice(def) => {
682                // When we have a bare slice shape with a wide pointer,
683                // it means we have a reference to a slice (e.g., from Arc<[T]>::borrow_inner)
684                Ok(PeekListLike::new(self, ListLikeDef::Slice(def)))
685            }
686            _ => {
687                // &[i32] is actually a _pointer_ to a slice.
688                match self.shape.ty {
689                    Type::Pointer(ptr) => match ptr {
690                        PointerType::Reference(vpt) | PointerType::Raw(vpt) => {
691                            let target = vpt.target;
692                            match target.def {
693                                Def::Slice(def) => {
694                                    let ptr = unsafe { self.data.as_ptr::<*const [()]>() };
695                                    let ptr = PtrConst::new(unsafe {
696                                        NonNull::new_unchecked((*ptr) as *mut [()]).as_ptr()
697                                    });
698                                    let peek = unsafe { Peek::unchecked_new(ptr, def.t) };
699
700                                    return Ok(PeekListLike::new(peek, ListLikeDef::Slice(def)));
701                                }
702                                _ => {
703                                    // well it's not list-like then
704                                }
705                            }
706                        }
707                        PointerType::Function(_) => {
708                            // well that's not a list-like
709                        }
710                    },
711                    _ => {
712                        // well that's not a list-like either
713                    }
714                }
715
716                Err(ReflectError::WasNotA {
717                    expected: "list, array or slice",
718                    actual: self.shape,
719                })
720            }
721        }
722    }
723
724    /// Tries to identify this value as a pointer
725    #[inline]
726    pub fn into_pointer(self) -> Result<PeekPointer<'mem, 'facet>, ReflectError> {
727        if let Def::Pointer(def) = self.shape.def {
728            Ok(PeekPointer { value: self, def })
729        } else {
730            Err(ReflectError::WasNotA {
731                expected: "smart pointer",
732                actual: self.shape,
733            })
734        }
735    }
736
737    /// Tries to identify this value as an option
738    #[inline]
739    pub fn into_option(self) -> Result<PeekOption<'mem, 'facet>, ReflectError> {
740        if let Def::Option(def) = self.shape.def {
741            Ok(PeekOption { value: self, def })
742        } else {
743            Err(ReflectError::WasNotA {
744                expected: "option",
745                actual: self.shape,
746            })
747        }
748    }
749
750    /// Tries to identify this value as a result
751    #[inline]
752    pub fn into_result(self) -> Result<PeekResult<'mem, 'facet>, ReflectError> {
753        if let Def::Result(def) = self.shape.def {
754            Ok(PeekResult { value: self, def })
755        } else {
756            Err(ReflectError::WasNotA {
757                expected: "result",
758                actual: self.shape,
759            })
760        }
761    }
762
763    /// Tries to identify this value as a tuple
764    #[inline]
765    pub fn into_tuple(self) -> Result<PeekTuple<'mem, 'facet>, ReflectError> {
766        if let Type::User(UserType::Struct(struct_type)) = self.shape.ty {
767            if struct_type.kind == StructKind::Tuple {
768                Ok(PeekTuple {
769                    value: self,
770                    ty: TupleType {
771                        fields: struct_type.fields,
772                    },
773                })
774            } else {
775                Err(ReflectError::WasNotA {
776                    expected: "tuple",
777                    actual: self.shape,
778                })
779            }
780        } else {
781            Err(ReflectError::WasNotA {
782                expected: "tuple",
783                actual: self.shape,
784            })
785        }
786    }
787
788    /// Tries to identify this value as a dynamic value (like `facet_value::Value`)
789    #[inline]
790    pub fn into_dynamic_value(self) -> Result<PeekDynamicValue<'mem, 'facet>, ReflectError> {
791        if let Def::DynamicValue(def) = self.shape.def {
792            Ok(PeekDynamicValue { value: self, def })
793        } else {
794            Err(ReflectError::WasNotA {
795                expected: "dynamic value",
796                actual: self.shape,
797            })
798        }
799    }
800
801    /// Tries to return the innermost value — useful for serialization. For example, we serialize a `NonZero<u8>` the same
802    /// as a `u8`. Similarly, we serialize a `Utf8PathBuf` the same as a `String.
803    ///
804    /// Returns a `Peek` to the innermost value, unwrapping transparent wrappers recursively.
805    /// For example, this will peel through newtype wrappers or smart pointers that have an `inner`.
806    pub fn innermost_peek(self) -> Self {
807        let mut current_peek = self;
808        while let Some(inner_shape) = current_peek.shape.inner {
809            // Try to borrow the inner value
810            let result = unsafe { current_peek.shape.call_try_borrow_inner(current_peek.data) };
811            match result {
812                Some(Ok(inner_data)) => {
813                    current_peek = Peek {
814                        data: inner_data.as_const(),
815                        shape: inner_shape,
816                        _invariant: PhantomData,
817                    };
818                }
819                Some(Err(e)) => {
820                    panic!(
821                        "innermost_peek: try_borrow_inner returned an error! was trying to go from {} to {}. error: {e}",
822                        current_peek.shape, inner_shape
823                    );
824                }
825                None => {
826                    // No try_borrow_inner function, stop here
827                    break;
828                }
829            }
830        }
831        current_peek
832    }
833
834    /// Performs custom serialization of the current peek using the provided field's metadata.
835    ///
836    /// Returns an `OwnedPeek` that points to the final type that should be serialized in place
837    /// of the current peek.
838    #[cfg(feature = "alloc")]
839    pub fn custom_serialization(&self, field: Field) -> Result<OwnedPeek<'mem>, ReflectError> {
840        let Some(proxy_def) = field.proxy() else {
841            return Err(ReflectError::OperationFailed {
842                shape: self.shape,
843                operation: "field does not have a proxy definition",
844            });
845        };
846
847        let target_shape = proxy_def.shape;
848        let tptr = target_shape.allocate().map_err(|_| ReflectError::Unsized {
849            shape: target_shape,
850            operation: "Not a Sized type",
851        })?;
852        let ser_res = unsafe { (proxy_def.convert_out)(self.data(), tptr) };
853        let err = match ser_res {
854            Ok(rptr) => {
855                if rptr.as_uninit() != tptr {
856                    ReflectError::CustomSerializationError {
857                        message: "convert_out did not return the expected pointer".into(),
858                        src_shape: self.shape,
859                        dst_shape: target_shape,
860                    }
861                } else {
862                    return Ok(OwnedPeek {
863                        shape: target_shape,
864                        data: rptr,
865                        _phantom: PhantomData,
866                    });
867                }
868            }
869            Err(message) => ReflectError::CustomSerializationError {
870                message,
871                src_shape: self.shape,
872                dst_shape: target_shape,
873            },
874        };
875        // if we reach here we have an error and we need to deallocate the target allocation
876        unsafe {
877            // SAFETY: unwrap should be ok since the allocation was ok
878            target_shape.deallocate_uninit(tptr).unwrap()
879        };
880        Err(err)
881    }
882
883    /// Returns an `OwnedPeek` using the shape's container-level proxy for serialization.
884    ///
885    /// This is used when a type has `#[facet(proxy = ProxyType)]` at the container level.
886    /// Unlike field-level proxies which are checked via `custom_serialization(field)`,
887    /// this method checks the Shape itself for a proxy definition.
888    ///
889    /// Returns `None` if the shape has no container-level proxy.
890    #[cfg(feature = "alloc")]
891    pub fn custom_serialization_from_shape(&self) -> Result<Option<OwnedPeek<'mem>>, ReflectError> {
892        let Some(proxy_def) = self.shape.proxy else {
893            return Ok(None);
894        };
895
896        let target_shape = proxy_def.shape;
897        let tptr = target_shape.allocate().map_err(|_| ReflectError::Unsized {
898            shape: target_shape,
899            operation: "Not a Sized type",
900        })?;
901
902        let ser_res = unsafe { (proxy_def.convert_out)(self.data(), tptr) };
903        let err = match ser_res {
904            Ok(rptr) => {
905                if rptr.as_uninit() != tptr {
906                    ReflectError::CustomSerializationError {
907                        message: "proxy convert_out did not return the expected pointer".into(),
908                        src_shape: self.shape,
909                        dst_shape: target_shape,
910                    }
911                } else {
912                    return Ok(Some(OwnedPeek {
913                        shape: target_shape,
914                        data: rptr,
915                        _phantom: PhantomData,
916                    }));
917                }
918            }
919            Err(message) => ReflectError::CustomSerializationError {
920                message,
921                src_shape: self.shape,
922                dst_shape: target_shape,
923            },
924        };
925
926        // if we reach here we have an error and we need to deallocate the target allocation
927        unsafe {
928            // SAFETY: unwrap should be ok since the allocation was ok
929            target_shape.deallocate_uninit(tptr).unwrap()
930        };
931        Err(err)
932    }
933}
934
935impl<'mem, 'facet> core::fmt::Display for Peek<'mem, 'facet> {
936    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
937        if let Some(result) = unsafe { self.shape.call_display(self.data, f) } {
938            return result;
939        }
940        write!(f, "⟨{}⟩", self.shape)
941    }
942}
943
944impl<'mem, 'facet> core::fmt::Debug for Peek<'mem, 'facet> {
945    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
946        if let Some(result) = unsafe { self.shape.call_debug(self.data, f) } {
947            return result;
948        }
949
950        write!(f, "⟨{}⟩", self.shape)
951    }
952}
953
954impl<'mem, 'facet> core::cmp::PartialEq for Peek<'mem, 'facet> {
955    #[inline]
956    fn eq(&self, other: &Self) -> bool {
957        self.partial_eq(other).unwrap_or(false)
958    }
959}
960
961impl<'mem, 'facet> core::cmp::PartialOrd for Peek<'mem, 'facet> {
962    #[inline]
963    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
964        self.partial_cmp(other).unwrap_or(None)
965    }
966}
967
968impl<'mem, 'facet> core::hash::Hash for Peek<'mem, 'facet> {
969    fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
970        self.hash(hasher)
971            .expect("Hashing is not supported for this shape");
972    }
973}
974
975/// A covariant wrapper around [`Peek`] for types that are covariant over their lifetime parameter.
976///
977/// Unlike [`Peek`], which is invariant over `'facet` for soundness reasons,
978/// `CovariantPeek` is **covariant** over `'facet`. This means a `CovariantPeek<'mem, 'static>`
979/// can be used where a `CovariantPeek<'mem, 'a>` is expected.
980///
981/// # When to Use
982///
983/// Use `CovariantPeek` when you need to:
984/// - Store multiple `Peek` values with different lifetimes in a single collection
985/// - Pass `Peek` values to functions expecting shorter lifetimes
986/// - Build data structures that wrap `Peek` without forcing invariance on the wrapper
987///
988/// # Safety
989///
990/// `CovariantPeek` can only be constructed from types that are actually covariant.
991/// The constructor verifies this at runtime by checking [`Shape::computed_variance`].
992/// This ensures that lifetime shrinking is always safe.
993///
994/// # Example
995///
996/// ```
997/// use facet::Facet;
998/// use facet_reflect::{Peek, CovariantPeek};
999///
1000/// #[derive(Facet)]
1001/// struct Data<'a> {
1002///     value: &'a str,
1003/// }
1004///
1005/// // Data<'a> is covariant in 'a because &'a str is covariant
1006/// let data = Data { value: "hello" };
1007/// let peek: Peek<'_, 'static> = Peek::new(&data);
1008///
1009/// // Convert to CovariantPeek - this verifies covariance
1010/// let covariant = CovariantPeek::new(peek).expect("Data is covariant");
1011///
1012/// // Now we can use it where shorter lifetimes are expected
1013/// fn use_shorter<'a>(p: CovariantPeek<'_, 'a>) {
1014///     let _ = p;
1015/// }
1016/// use_shorter(covariant);
1017/// ```
1018#[derive(Clone, Copy)]
1019pub struct CovariantPeek<'mem, 'facet> {
1020    /// Underlying data
1021    data: PtrConst,
1022
1023    /// Shape of the value
1024    shape: &'static Shape,
1025
1026    // Covariant over both 'mem and 'facet: CovariantPeek<'mem, 'static> can be used where
1027    // CovariantPeek<'mem, 'a> is expected.
1028    //
1029    // This is safe ONLY because we verify at construction time that the underlying
1030    // type is covariant. For covariant types, shrinking lifetimes is always safe.
1031    _covariant: PhantomData<(&'mem (), &'facet ())>,
1032}
1033
1034impl<'mem, 'facet> CovariantPeek<'mem, 'facet> {
1035    /// Creates a new `CovariantPeek` from a `Peek`, verifying that the underlying type is covariant.
1036    ///
1037    /// Returns `None` if the type is not covariant (i.e., it's contravariant or invariant).
1038    ///
1039    /// # Example
1040    ///
1041    /// ```
1042    /// use facet::Facet;
1043    /// use facet_reflect::{Peek, CovariantPeek};
1044    ///
1045    /// // i32 has no lifetime parameters, so it's covariant
1046    /// let value = 42i32;
1047    /// let peek = Peek::new(&value);
1048    /// let covariant = CovariantPeek::new(peek);
1049    /// assert!(covariant.is_some());
1050    /// ```
1051    #[inline]
1052    pub fn new(peek: Peek<'mem, 'facet>) -> Option<Self> {
1053        if peek.variance() == Variance::Covariant {
1054            Some(Self {
1055                data: peek.data,
1056                shape: peek.shape,
1057                _covariant: PhantomData,
1058            })
1059        } else {
1060            None
1061        }
1062    }
1063
1064    /// Creates a new `CovariantPeek` from a `Peek`, panicking if the type is not covariant.
1065    ///
1066    /// # Panics
1067    ///
1068    /// Panics if the underlying type is not covariant.
1069    ///
1070    /// # Example
1071    ///
1072    /// ```
1073    /// use facet::Facet;
1074    /// use facet_reflect::{Peek, CovariantPeek};
1075    ///
1076    /// let value = "hello";
1077    /// let peek = Peek::new(&value);
1078    /// let covariant = CovariantPeek::new_unchecked(peek); // Will succeed
1079    /// ```
1080    #[inline]
1081    pub fn new_unchecked(peek: Peek<'mem, 'facet>) -> Self {
1082        Self::new(peek).unwrap_or_else(|| {
1083            panic!(
1084                "CovariantPeek::new_unchecked called on non-covariant type {} (variance: {:?})",
1085                peek.shape,
1086                peek.variance()
1087            )
1088        })
1089    }
1090
1091    /// Creates a `CovariantPeek` directly from a covariant `Facet` type.
1092    ///
1093    /// Returns `None` if the type is not covariant.
1094    ///
1095    /// # Example
1096    ///
1097    /// ```
1098    /// use facet::Facet;
1099    /// use facet_reflect::CovariantPeek;
1100    ///
1101    /// let value = 42i32;
1102    /// let covariant = CovariantPeek::from_ref(&value);
1103    /// assert!(covariant.is_some());
1104    /// ```
1105    #[inline]
1106    pub fn from_ref<T: Facet<'facet> + ?Sized>(t: &'mem T) -> Option<Self> {
1107        Self::new(Peek::new(t))
1108    }
1109
1110    /// Returns the underlying `Peek`.
1111    ///
1112    /// Note that the returned `Peek` is invariant, so you cannot use it to
1113    /// shrink lifetimes directly. Use `CovariantPeek` for lifetime flexibility.
1114    #[inline]
1115    pub fn into_peek(self) -> Peek<'mem, 'facet> {
1116        Peek {
1117            data: self.data,
1118            shape: self.shape,
1119            _invariant: PhantomData,
1120        }
1121    }
1122
1123    /// Returns the shape of the underlying value.
1124    #[inline]
1125    pub const fn shape(&self) -> &'static Shape {
1126        self.shape
1127    }
1128
1129    /// Returns the data pointer.
1130    #[inline]
1131    pub const fn data(&self) -> PtrConst {
1132        self.data
1133    }
1134}
1135
1136impl<'mem, 'facet> core::ops::Deref for CovariantPeek<'mem, 'facet> {
1137    type Target = Peek<'mem, 'facet>;
1138
1139    #[inline]
1140    fn deref(&self) -> &Self::Target {
1141        // SAFETY: CovariantPeek and Peek have the same memory layout for the
1142        // data and shape fields. The PhantomData fields don't affect layout.
1143        // We're creating a reference to a Peek that views the same data.
1144        //
1145        // This is safe because:
1146        // 1. We only construct CovariantPeek from covariant types
1147        // 2. The Peek reference we return has the same lifetime bounds
1148        // 3. We're not allowing mutation through this reference
1149        unsafe { &*(self as *const CovariantPeek<'mem, 'facet> as *const Peek<'mem, 'facet>) }
1150    }
1151}
1152
1153impl<'mem, 'facet> core::fmt::Debug for CovariantPeek<'mem, 'facet> {
1154    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1155        f.debug_struct("CovariantPeek")
1156            .field("shape", &self.shape)
1157            .field("data", &self.data)
1158            .finish()
1159    }
1160}
1161
1162impl<'mem, 'facet> core::fmt::Display for CovariantPeek<'mem, 'facet> {
1163    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1164        core::fmt::Display::fmt(&**self, f)
1165    }
1166}
1167
1168#[cfg(test)]
1169mod tests {
1170    use super::*;
1171
1172    /// Regression test for issue #1082: UB in `Peek("").as_str()`
1173    /// Previously, `as_str()` used `get::<&str>()` which tried to read a fat pointer
1174    /// from the str data, causing UB for empty strings (reading 16 bytes from 0-byte allocation).
1175    #[test]
1176    fn test_peek_as_str_empty_string() {
1177        let peek = Peek::new("");
1178        assert_eq!(peek.as_str(), Some(""));
1179    }
1180
1181    #[test]
1182    fn test_peek_as_str_non_empty_string() {
1183        let peek = Peek::new("hello");
1184        assert_eq!(peek.as_str(), Some("hello"));
1185    }
1186
1187    #[test]
1188    #[cfg(feature = "alloc")]
1189    fn test_peek_as_str_owned_string() {
1190        let s = alloc::string::String::from("owned string");
1191        let peek = Peek::new(&s);
1192        assert_eq!(peek.as_str(), Some("owned string"));
1193    }
1194
1195    /// Regression test for issue #794: Peek::as_str() with double reference
1196    /// Previously, this would cause UB when trying to read &&str as &str
1197    #[test]
1198    fn test_peek_as_str_double_reference() {
1199        let value = &"hello";
1200        let peek = Peek::new(&value);
1201        assert_eq!(peek.as_str(), Some("hello"));
1202    }
1203
1204    #[test]
1205    fn test_covariant_peek_from_covariant_type() {
1206        // i32 has no lifetime parameters, so it's covariant
1207        let value = 42i32;
1208        let peek = Peek::new(&value);
1209        let covariant = CovariantPeek::new(peek);
1210        assert!(covariant.is_some());
1211
1212        // Verify we can access Peek methods through Deref
1213        let covariant = covariant.unwrap();
1214        assert_eq!(covariant.shape(), peek.shape());
1215    }
1216
1217    #[test]
1218    fn test_covariant_peek_from_ref() {
1219        let value = 42i32;
1220        let covariant = CovariantPeek::from_ref(&value);
1221        assert!(covariant.is_some());
1222    }
1223
1224    #[test]
1225    fn test_covariant_peek_deref_to_peek() {
1226        let value = "hello";
1227        let peek = Peek::new(&value);
1228        let covariant = CovariantPeek::new(peek).unwrap();
1229
1230        // Test that Deref works - we can call Peek methods directly
1231        assert_eq!(covariant.as_str(), Some("hello"));
1232        assert_eq!(covariant.shape(), peek.shape());
1233    }
1234
1235    #[test]
1236    fn test_covariant_peek_into_peek() {
1237        let value = 42i32;
1238        let original_peek = Peek::new(&value);
1239        let covariant = CovariantPeek::new(original_peek).unwrap();
1240        let recovered_peek = covariant.into_peek();
1241
1242        assert_eq!(recovered_peek.shape(), original_peek.shape());
1243    }
1244
1245    #[test]
1246    fn test_covariant_peek_lifetime_covariance() {
1247        // This test verifies that CovariantPeek is actually covariant over 'facet
1248        // by passing a CovariantPeek<'_, 'static> to a function expecting CovariantPeek<'_, 'a>
1249        fn use_shorter<'a>(_p: CovariantPeek<'_, 'a>) {}
1250
1251        let value = 42i32;
1252        let covariant: CovariantPeek<'_, 'static> = CovariantPeek::from_ref(&value).unwrap();
1253
1254        // This compiles because CovariantPeek is covariant over 'facet
1255        use_shorter(covariant);
1256    }
1257
1258    #[test]
1259    #[cfg(feature = "alloc")]
1260    fn test_covariant_peek_vec_type() {
1261        // Vec<T> is covariant in T
1262        let vec = alloc::vec![1i32, 2, 3];
1263        let peek = Peek::new(&vec);
1264        let covariant = CovariantPeek::new(peek);
1265        assert!(covariant.is_some());
1266    }
1267
1268    #[test]
1269    #[cfg(feature = "alloc")]
1270    fn test_covariant_peek_option_type() {
1271        // Option<T> is covariant in T
1272        let opt = Some(42i32);
1273        let peek = Peek::new(&opt);
1274        let covariant = CovariantPeek::new(peek);
1275        assert!(covariant.is_some());
1276    }
1277
1278    #[test]
1279    fn test_spanned_structural_hash_ignores_span() {
1280        use crate::{Span, Spanned};
1281        use core::hash::Hasher;
1282        use std::hash::DefaultHasher;
1283
1284        // Two Spanned values with same inner value but different spans
1285        let a = Spanned::new(42i32, Span::new(0, 10));
1286        let b = Spanned::new(42i32, Span::new(100, 20));
1287
1288        // They should have the same structural hash
1289        let mut hasher_a = DefaultHasher::new();
1290        Peek::new(&a).structural_hash(&mut hasher_a);
1291        let hash_a = hasher_a.finish();
1292
1293        let mut hasher_b = DefaultHasher::new();
1294        Peek::new(&b).structural_hash(&mut hasher_b);
1295        let hash_b = hasher_b.finish();
1296
1297        assert_eq!(
1298            hash_a, hash_b,
1299            "Spanned values with same inner value should have same structural hash"
1300        );
1301    }
1302
1303    #[test]
1304    fn test_spanned_structural_hash_differs_for_different_values() {
1305        use crate::{Span, Spanned};
1306        use core::hash::Hasher;
1307        use std::hash::DefaultHasher;
1308
1309        // Two Spanned values with different inner values
1310        let a = Spanned::new(42i32, Span::new(0, 10));
1311        let b = Spanned::new(99i32, Span::new(0, 10));
1312
1313        // They should have different structural hashes
1314        let mut hasher_a = DefaultHasher::new();
1315        Peek::new(&a).structural_hash(&mut hasher_a);
1316        let hash_a = hasher_a.finish();
1317
1318        let mut hasher_b = DefaultHasher::new();
1319        Peek::new(&b).structural_hash(&mut hasher_b);
1320        let hash_b = hasher_b.finish();
1321
1322        assert_ne!(
1323            hash_a, hash_b,
1324            "Spanned values with different inner values should have different structural hashes"
1325        );
1326    }
1327
1328    #[test]
1329    fn test_spanned_field_metadata() {
1330        use crate::Spanned;
1331        use facet_core::{Type, UserType};
1332
1333        // Get the shape for Spanned<i32>
1334        let shape = <Spanned<i32> as facet_core::Facet>::SHAPE;
1335
1336        // Extract the struct type
1337        let struct_type = match shape.ty {
1338            Type::User(UserType::Struct(st)) => st,
1339            _ => panic!("Expected struct type"),
1340        };
1341
1342        // Find the span field and verify it has metadata = "span"
1343        let span_field = struct_type
1344            .fields
1345            .iter()
1346            .find(|f| f.name == "span")
1347            .expect("Should have span field");
1348
1349        assert!(
1350            span_field.is_metadata(),
1351            "span field should be marked as metadata"
1352        );
1353        assert_eq!(
1354            span_field.metadata_kind(),
1355            Some("span"),
1356            "span field should have metadata kind 'span'"
1357        );
1358
1359        // Verify the value field is NOT metadata
1360        let value_field = struct_type
1361            .fields
1362            .iter()
1363            .find(|f| f.name == "value")
1364            .expect("Should have value field");
1365
1366        assert!(
1367            !value_field.is_metadata(),
1368            "value field should not be marked as metadata"
1369        );
1370    }
1371}