Skip to main content

reify_reflect/
lib.rs

1#![deny(unsafe_code)]
2
3//! A unified, fully-safe ecosystem for moving values between Rust's type
4//! level and value level.
5//!
6//! This is the **facade crate**. It re-exports each of the focused crates
7//! in the workspace under a single namespace, so a single dependency on
8//! `reify-reflect` (with appropriate features) is enough to use everything.
9//! If you only need one piece, depending on the focused crate directly
10//! gives you a smaller build.
11//!
12//! # What is "reification" and "reflection"?
13//!
14//! Two complementary directions:
15//!
16//! - **Reflection** is type → value. A type like `S<S<S<Z>>>` encodes the
17//!   number 3 at compile time; [`core::Reflect::reflect`] hands you `3`
18//!   at runtime.
19//! - **Reification** is value → type. A `u64` known only at runtime can
20//!   be lifted into a callback in which it is genuinely a `const N: u64`.
21//!   See [`const_reify`](https://docs.rs/const-reify) (re-exported as
22//!   [`const_bridge`] when the `const-reify` feature is enabled).
23//!
24//! Everything here is `#![deny(unsafe_code)]`. There is no `unsafe`, no
25//! compiler-internal layout assumption, and no `unsafeCoerce`-style trick.
26//! The Rust borrow checker enforces what GHC's parametricity enforces in
27//! Haskell's `reflection` library.
28//!
29//! # Module map
30//!
31//! | Module | Crate | Use it for |
32//! |---|---|---|
33//! | [`core`] | [`reify_reflect_core`] | The [`Reflect`](core::Reflect) trait, the [`reify`](core::reify) scoping function, the [`Reified`](core::Reified) branded token, and the [`RuntimeValue`](core::RuntimeValue) enum. |
34//! | [`nat`] | [`reflect_nat`] | Peano naturals (`Z`/`S<N>`), type-level booleans, heterogeneous lists, with optional `frunk`/`typenum` bridges. |
35//! | [`graph`] | [`reify_graph`] | Convert `Rc<RefCell<T>>` or `Arc<Mutex<T>>` graphs to and from a flat node+edge form, preserving sharing and cycles. |
36//! | [`context`] | [`context_trait`] | Swap out `Ord`, `Hash`, `Display` (or any user-defined trait) for one block of code via `WithContext`. |
37//! | [`async_trace`] | [`async_reify`] | Wrap futures to record poll events and turn them into an inspectable async step graph. |
38//! | [`const_bridge`] (feature `const-reify`) | [`const_reify`](https://docs.rs/const-reify) | Dispatch a runtime `u64` in `0..=255` to the matching `const N: u64` monomorphization, safely. |
39//!
40//! For the `#[derive(Reflect)]`, `#[trace_async]`, and `#[reifiable]`
41//! proc macros, depend on `reflect-derive`, `async-reify-macros`, and
42//! `const-reify-derive` respectively (or enable the relevant features).
43//!
44//! # Quick start
45//!
46//! Reflect a type-level number to a runtime value:
47//!
48//! ```
49//! use reify_reflect::core::{Reflect, RuntimeValue};
50//! use reify_reflect::nat::{S, Z};
51//!
52//! type Three = S<S<S<Z>>>;
53//! assert_eq!(Three::reflect(), RuntimeValue::Nat(3));
54//! ```
55//!
56//! For deeper walkthroughs, see the [guides] and the [narrative blog post]
57//! in the source tree.
58//!
59//! [guides]: https://github.com/joshburgess/reify-reflect/tree/main/docs/guides
60//! [narrative blog post]: https://github.com/joshburgess/reify-reflect/blob/main/docs/blog-post.md
61//!
62//! # Feature flags
63//!
64//! | Feature | Default | Effect |
65//! |---|---|---|
66//! | `serde` | yes | Enables `Serialize`/`Deserialize` impls in [`graph`] and [`async_trace`]. |
67//! | `const-reify` | no | Enables the [`const_bridge`] module. Adds 256 monomorphizations to compile time. |
68//! | `typenum` | no | Bridge between [`nat`] and the `typenum` crate. |
69//! | `frunk` | no | Bridge between [`nat`] and `frunk`'s HList. |
70//! | `full` | no | All of the above. |
71
72/// Core traits and types: [`Reflect`](reify_reflect_core::Reflect),
73/// [`reify`](reify_reflect_core::reify), [`Reified`](reify_reflect_core::Reified),
74/// [`RuntimeValue`](reify_reflect_core::RuntimeValue).
75pub mod core {
76    pub use reify_reflect_core::*;
77}
78
79/// Type-level naturals ([`Z`](reflect_nat::Z), [`S`](reflect_nat::S)),
80/// booleans ([`True`](reflect_nat::True), [`False`](reflect_nat::False)),
81/// and heterogeneous lists ([`HNil`](reflect_nat::HNil), [`HCons`](reflect_nat::HCons)).
82pub mod nat {
83    pub use reflect_nat::*;
84}
85
86/// `Rc<RefCell<T>>` graph reification and reconstruction.
87pub mod graph {
88    pub use reify_graph::*;
89}
90
91/// Runtime-synthesized trait instances scoped to callbacks.
92pub mod context {
93    pub use context_trait::*;
94}
95
96/// Async computation tracing and step graph extraction.
97pub mod async_trace {
98    pub use async_reify::*;
99}
100
101/// Runtime-to-const-generic dispatch (requires `const-reify` feature).
102#[cfg(feature = "const-reify")]
103pub mod const_bridge {
104    pub use const_reify::*;
105}