Skip to content

Ygg01/typeinfo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages