• Stars
    star
    290
  • Rank 142,981 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reflection via DWARF.

deflect

[EXPERIMENTAL] Deflect brings reflection to Rust using DWARF debug info.

Deflect can be used to recover the concrete types of trait objects, inspect the internal state of async generators, and pretty-print arbitrary data.

Example

Use the [Reflect] trait to debug or recursively destructure any value.

pub struct Foo {
    a: u8
}

// initialize the debuginfo provider
let context = deflect::default_provider()?;

// create some type-erased data
let erased: Box<dyn Any> = Box::new(Foo { a: 42 });

// cast it to `&dyn Reflect`
let reflectable: &dyn deflect::Reflect = &erased;

// reflect it!
let value: deflect::Value = reflectable.reflect(&context)?;

// pretty-print the reflected value
assert_eq!(value.to_string(), "box Foo { a: 42 }");

// downcast into a `BoxedDyn` value
let value: deflect::value::BoxedDyn = value.try_into()?;

// dereference the boxed value
let value: deflect::Value = value.deref()?;

// downcast into a `Struct` value
let value: deflect::value::Struct = value.try_into()?;

// get the field `a` by name
let Some(field) = value.field("a")? else {
    panic!("no field named `a`!")
};

// get the value of the field
let value = field.value()?;

// downcast into a `u8`
let value: u8 = value.try_into()?;

// check that it's equal to `42`!
assert_eq!(value, 42);

See the examples directory of this crate's source for additional examples.

Limitations

The current implementation of [default_provider] only works when DWARF debuginfo is stored in the program's binary. It will not work if DWARF debug info is split into other files. Pull requests are welcome.

This crate is highly experimental. It is not suitable as a critical component of any system. The initial releases of this crate require significant polish. Pull requests are welcome. Its known soundness holes include ignorance of UnsafeCell. Don't reflect into types containing UnsafeCell.

Additionally, the particulars of how Rust encodes DWARF debug info my change over time. This crate will do its best to keep up with those changes. Again, pull requests are welcome.

License

This project is licensed under the Apache License, Version 2.0, or the MIT license, at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in deflect by you, shall be licensed as MIT and Apache 2.0, without any additional terms or conditions.

More Repositories

1

typic

Type-safe transmutations between layout-compatible types.
Rust
119
star
2

scoped-trace

Rust
22
star
3

typelayout

An experiment in embedding layout computations in the type system.
Rust
16
star
4

fn_name

Rust macros that produce the name of the function they're invoked within.
Rust
16
star
5

elain

Set the minimum alignments of types using const generics.
Rust
15
star
6

bam

Beep, as an ALSA MIDI device
Rust
12
star
7

analit

Versatile Analog Literals for Rust
RenderScript
11
star
8

beep

Beep the PC speaker
Rust
8
star
9

fireplace

A command line graphing utility written in Rust.
Rust
8
star
10

flake

Infinite Whiteboard
Rust
6
star
11

midiplex

Volume-aware splitting of a polyphonic MIDI stream into multiple, monophonic streams.
Rust
3
star
12

site

Personal Website
HTML
2
star
13

transmutation-foundation

2
star
14

stiki

Static Wiki
Racket
2
star
15

turnout-for-what

Shell utility for conditional redirection.
Rust
2
star
16

fromzeros

Rust
1
star
17

type-census

Track the number of extant instances of your types.
Rust
1
star
18

basicb

A basic beep utility, designed for simplicity and low-latency.
Rust
1
star
19

gsfs

C
1
star
20

final

Rust
1
star
21

gulog

A Prolog in Rust
Rust
1
star
22

safer-transmutation-redex

Racket
1
star
23

backstrip

Simple, static cataloging.
1
star
24

tracing-allocations

experiments with tracking-allocator + tracing
Rust
1
star
25

tracing-book

JavaScript
1
star
26

rustylog

Rust
1
star
27

fsize

Pointer-sized floating point type alias.
Rust
1
star
28

laserstorm

Rust
1
star
29

Advanced-MIC-1

A short guide I wrote on some advanced MIC-1 programming techniques.
1
star
30

tracing-causality

A tracing layer that tracks the causal relationships between spans, and can be concurrently queried elsewhere.
Rust
1
star