An open API service indexing awesome lists of open source software.

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

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>",
);
```