Skip to main content

design/place/subplace/
traits.rs

1use crate::ptr::Metadata;
2
3/// Represents a *subplace* in `Self::Source`.
4///
5/// A subplace is directly contained in its parent (i.e. there is no pointer
6/// indirection).
7pub unsafe trait Subplace: Sized {
8    type Source: ?Sized;
9    type Target: ?Sized;
10
11    fn offset(
12        self,
13        metadata: Metadata<Self::Source>,
14    ) -> (usize, Metadata<Self::Target>);
15}
16
17/// Represents a field of a struct or a tuple of
18/// <code>Self::[Source](Subplace::Source)</code>.
19pub unsafe trait Field: Subplace + Default {
20    /// The name of the field.
21    ///
22    /// If this represents a field of a tuple or a tuple struct, then this will
23    /// be the stringified index.
24    const NAME: &'static str;
25}
26
27/// A type whose values can be `match`ed.
28///
29/// For every value `VARIANT` in `Self::VARIANTS`, `Self` implements
30/// `HasVariant<VARIANT>`. Casting a pointer to `Self` to `<Self as
31/// HasVariant<VARIANTS>>::VariantType` is sound when `variant_at` returned
32/// `VARIANT`.
33pub unsafe trait Matchable {
34    const VARIANTS: &'static [&'static str];
35
36    unsafe fn variant_at(ptr: *const Self) -> &'static str;
37}
38
39pub unsafe trait HasVariant<const VARIANT: &'static str>:
40    Matchable
41{
42    type VariantType;
43}
44
45pub type VariantType<E, const VARIANT: &'static str> =
46    <E as HasVariant<VARIANT>>::VariantType;