Crate bronzite

Crate bronzite 

Source
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

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 client
  • bronzite-daemon: Background daemon that caches rustc compilation results
  • bronzite_macros: Ready-to-use proc-macros for common reflection tasks
  • bronzite-query: Rustc plugin that extracts type information
  • bronzite_types: Shared types for the IPC protocol

ยง๐Ÿ“ฆ Installation

Add to your Cargo.toml:

[dependencies]
bronzite = "0.2"
bronzite-client = "0.2"  # For proc-macro development

Install the daemon:

cargo install bronzite
rustup toolchain install nightly-2025-08-20

The daemon auto-starts when you use the reflection API.

ยง๐Ÿ” API Overview

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ยง

AssocConstInfo
Associated constant information.
AssocTypeInfo
Associated type information.
BronziteClient
A client for communicating with the Bronzite daemon.
Crate
A reflected crate - the main entry point for type reflection.
EnumDef
A reflected enum definition.
EnumVariantInfo
Information about an enum variant.
Field
A field of a struct, enum variant, or union.
FieldInfo
Information about a struct field.
FunctionSignature
Parsed function signature.
GenericParam
A generic parameter (lifetime, type, or const).
InherentImplDetails
Detailed information about an inherent impl block.
ItemInfo
Basic information about an item in the crate.
LayoutInfo
Memory layout information for a type.
Method
A method (from an impl block).
MethodDetails
Detailed information about a method.
MethodSummary
Summary of a method (for listings).
StructDef
A reflected struct definition with navigation methods.
TraitDef
A reflected trait definition.
TraitDetails
Detailed information about a trait.
TraitImpl
A trait implementation block.
TraitImplDetails
Detailed information about a trait implementation.
TraitInfo
Summary information about a trait.
TraitMethod
A method defined in a trait.
TraitMethodInfo
Information about a trait method.
TypeAliasDef
A reflected type alias.
TypeAliasInfo
Information about a type alias.
TypeDetails
Detailed information about a type.
TypeSummary
Summary information about a type (for listings).
UnionDef
A reflected union definition.

Enumsยง

Error
Errors that can occur when using the Bronzite client.
GenericParamKind
Item
A unified representation of any Rust item (struct, enum, trait, etc).
ItemKind
The kind of an item.
TypeKind
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.