Expand description
ยง๐ฎ Bronzite - Compile-time Type Reflection for Rust
Bronzite provides powerful compile-time access to type information, trait implementations, method bodies, and more. It enables proc-macros to introspect Rust code using a daemon that leverages rustc internals.
ยงโจ Quick Start
ยงHigh-Level Reflection API (Recommended)
The v0.2 API provides an ergonomic, type-safe interface for exploring types:
โ
use bronzite_client::Crate;
#[proc_macro]
pub fn my_macro(input: TokenStream) -> TokenStream {
// Connect to daemon and reflect on a crate
let krate = Crate::reflect("my_crate").unwrap();
// Get a struct and explore it
let user = krate.get_struct("User").unwrap();
// Navigate to fields
for field in user.fields().unwrap() {
println!("{}: {}", field.name.unwrap(), field.ty);
// Navigate to field's type definition
if let Some(field_type) = field.type_def().unwrap() {
println!(" -> defined in: {}", field_type.path());
}
}
// Check trait implementations
if user.implements("Debug").unwrap() {
println!("User implements Debug!");
}
// Get methods
for method in user.methods().unwrap() {
println!("Method: {}", method.name);
if let Some(body) = &method.body_source {
println!(" Body: {}", body);
}
}
// Generate code based on discoveries
quote! { /* ... */ }.into()
}ยงPattern Matching
Query types using intuitive glob patterns:
โ
use bronzite_client::Crate;
let krate = Crate::reflect("my_crate")?;
// Exact match
let user = krate.get_struct("User")?;
// Single-level glob: matches "foo::Bar" but not "foo::bar::Baz"
let items = krate.items("mymod::*")?;
// Recursive glob: matches all descendants
let all_items = krate.items("mymod::**")?;
// Get only specific types
let structs = krate.structs("*")?;
let enums = krate.enums("*")?;
let traits = krate.traits("*")?;ยงBuilt-in Proc-Macros
For simple use cases, use the built-in macros:
โ
use bronzite::bronzite_trait_names;
// Get trait implementations as a const array
const USER_TRAITS: &[&str] = bronzite_trait_names!("my_crate", "User");
// Expands to: &["Debug", "Clone", "Serialize", ...]ยง๐๏ธ Architecture
Bronzite consists of several components working together:
bronzite_client: High-level reflection API and low-level RPC clientbronzite-daemon: Background daemon that caches rustc compilation resultsbronzite_macros: Ready-to-use proc-macros for common reflection tasksbronzite-query: Rustc plugin that extracts type informationbronzite_types: Shared types for the IPC protocol
ยง๐ฆ Installation
Add to your Cargo.toml:
[dependencies]
bronzite = "0.2"
bronzite-client = "0.2" # For proc-macro developmentInstall the daemon:
cargo install bronzite
rustup toolchain install nightly-2025-08-20The daemon auto-starts when you use the reflection API.
ยง๐ API Overview
ยงNavigation Methods
The high-level API allows fluent navigation between related types:
- Struct โ Fields โ Field Types:
struct.fields()โfield.type_def() - Struct โ Methods โ Return Types:
struct.methods()โmethod.return_type_def() - Trait โ Implementors:
trait.implementors() - Type Alias โ Concrete Type:
alias.resolve()
ยงType Information
Access comprehensive type metadata:
- Struct fields with types, visibility, and layout
- Enum variants with discriminants
- Trait methods with signatures and default implementations
- Method bodies as source code
- Generic parameters and where clauses
- Documentation comments
ยง๐ Learn More
ยง๐ฏ Use Cases
- Derive macro helpers: Generate implementations based on field types
- Validation: Check trait bounds at compile time
- Code generation: Generate boilerplate based on type structure
- Static analysis: Analyze type relationships in proc-macros
- Documentation tools: Extract and process doc comments
Macrosยง
- bronzite_
crate_ traits - Get all trait names defined in a crate as a const slice.
- bronzite_
field_ names - Get field names of a struct as a const slice.
- bronzite_
implementors - Get types that implement a trait as a const slice of type path strings.
- bronzite_
implements - Check if a type implements a trait at compile time.
- bronzite_
method_ names - Get method names from a typeโs inherent impl as a const slice.
- bronzite_
resolve_ alias - Resolve a type alias and get the underlying type as a string literal.
- bronzite_
trait_ names - Get the names of all traits implemented by a type as a const slice.
Structsยง
- Assoc
Const Info - Associated constant information.
- Assoc
Type Info - Associated type information.
- Bronzite
Client - A client for communicating with the Bronzite daemon.
- Crate
- A reflected crate - the main entry point for type reflection.
- EnumDef
- A reflected enum definition.
- Enum
Variant Info - Information about an enum variant.
- Field
- A field of a struct, enum variant, or union.
- Field
Info - Information about a struct field.
- Function
Signature - Parsed function signature.
- Generic
Param - A generic parameter (lifetime, type, or const).
- Inherent
Impl Details - Detailed information about an inherent impl block.
- Item
Info - Basic information about an item in the crate.
- Layout
Info - Memory layout information for a type.
- Method
- A method (from an impl block).
- Method
Details - Detailed information about a method.
- Method
Summary - Summary of a method (for listings).
- Struct
Def - A reflected struct definition with navigation methods.
- Trait
Def - A reflected trait definition.
- Trait
Details - Detailed information about a trait.
- Trait
Impl - A trait implementation block.
- Trait
Impl Details - Detailed information about a trait implementation.
- Trait
Info - Summary information about a trait.
- Trait
Method - A method defined in a trait.
- Trait
Method Info - Information about a trait method.
- Type
Alias Def - A reflected type alias.
- Type
Alias Info - Information about a type alias.
- Type
Details - Detailed information about a type.
- Type
Summary - Summary information about a type (for listings).
- Union
Def - A reflected union definition.
Enumsยง
- Error
- Errors that can occur when using the Bronzite client.
- Generic
Param Kind - Item
- A unified representation of any Rust item (struct, enum, trait, etc).
- Item
Kind - The kind of an item.
- Type
Kind - The kind of a type.
- Visibility
- Visibility of an item.
Functionsยง
- connect
- Try to connect to an existing daemon, or return an error if not running.
- connect_
for_ workspace - Try to connect to an existing daemon for a specific workspace.
- connect_
or_ start - Connect to the daemon, ensuring itโs running first.
- ensure_
daemon_ running - Ensure the daemon is running, starting it if necessary.
- ensure_
daemon_ running_ with_ timeout - Ensure the daemon is running with a custom timeout.
- is_
daemon_ running - Check if the daemon is running and responding.
Type Aliasesยง
- Result
- Result type for Bronzite operations.