https://github.com/ickk/inspect
inspect descriptions of types at run-time
https://github.com/ickk/inspect
Last synced: about 1 year ago
JSON representation
inspect descriptions of types at run-time
- Host: GitHub
- URL: https://github.com/ickk/inspect
- Owner: ickk
- Created: 2025-02-02T05:44:08.000Z (over 1 year ago)
- Default Branch: dev
- Last Pushed: 2025-02-03T05:50:34.000Z (over 1 year ago)
- Last Synced: 2025-02-03T06:30:49.053Z (over 1 year ago)
- Language: Rust
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
> *Do you think God stays in heaven because he too lives in fear of what he's
> created?* ~ Spy Kids 2
`inspect`
---------
An experiment collecting information about types to be queried at run-time.
This can work for types containing references, and even those with non-static
lifetime parameters; In the latter case the `TypeId` collected corresponds to
the `'static` version of the given type.
For now it is implemented for common primitive types, including numerics,
pointers, references, slices, tuples (up to *8-tuples*) and some types from
`std` like `Vec`, `PhantomData`, `Option`, `Result`, &c.
`TypeInfo` contains the type_name, size, align, and `TypeId` of a types. For
structs the names, offsets, and types of fields are available. For types like
`Vec`, `Option`, `[T]` the type info of the generic item is available.
```rust
use ::inspect::TypeInfo;
#[derive(TypeInfo)]
struct MyStruct(Option>);
#[derive(TypeInfo)]
struct Child(usize, Option>, Box<[T]>);
let type_info = TypeInfo::of::>();
assert_eq!(
format!("size: {:?}, align: {:?}", type_info.size(), type_info.align()),
"size: Some(40), align: Some(8)"
);
assert_eq!(
format!("{type_info:#}"),
"MyStruct(
Option(
usize,
Option>,
Box<[u32]>,
)>,
)",
);
let type_info = TypeInfo::of::<[&'static Vec]>();
assert_eq!(
format!("size: {:?}, align: {:?}", type_info.size(), type_info.align()),
"size: None, align: None",
);
assert_eq!(
format!("{type_info:#}"),
"[&Vec]",
);
use ::core::marker::PhantomData;
let type_info = TypeInfo::of::>>();
assert_eq!(
format!("size: {:?}, align: {:?}", type_info.size(), type_info.align()),
"size: Some(0), align: Some(1)",
);
assert_eq!(
format!("{type_info:#}"),
"PhantomData>",
);
```