Ygg01 / typeinfo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This is an experimental crate in adding compile time reflection in Rust via Macros don't use it for anything

Dev Diary

// Everything is non_exhaustive and public
// (not sure how to handle private fields...)

/// Result of `typeinfo!`
struct Type {
    name: &'static str, // result of `type_name`
    inner: TypeInner,
    layout: Layout,
    generics: &'static[Generic],
    lifetimes: &'static[Lifetime],
}

impl Type {
    /// Defer to `StructTy::field`, panic if not a struct
    const fn field(name: &'static str) -> &'static StructField;
    /// Defer to `EnumTy::variant`, panic if not an enum
    const fn variant(name: &'static str) -> &'static EnumVariant;
}

enum TypeInner { Struct(StructTy), Enum(EnumTy), ...}

struct StructTy { fields: &'static[Field] }
struct EnumTy { variants: &'static[EnumVariant] }

struct EnumVariant {
    fields: &'static[Field],
    discriminant: Discriminant,
    value: Option<isize> // Value of C-style enums
}

struct Field {
    type: Type,   // Field type
    name: Option<&'static str>,  // Field name if a named struct
    field_index: usize,  // Field count within the struct as defined
    offset: usize,  // offset in implementation
}

impl Field {
    // Helpers to get or set a field on the parent struct
    // Not sure what this would do for enums
    const fn getter(&self) -> (fn(&ParentTy) -> SelfTy);
    const fn setter(&self) -> fn(&mut ParentTy, SelfTy);
}

struct Generic {
    ty: Type,
    default: Option<Type>
}

impl StructTy { // similar for EnumTy with Variant
    /// Get a field by name at compile time, compile_error if it doesn't exist
    const fn field(name: &'static str) -> &'static Field;
}

// ...

Prior art:

About


Languages

Language:Rust 100.0%