Git Product home page Git Product logo

deflect's Introduction

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.

deflect's People

Contributors

jswrenn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.