Idiomatic Rust bindings for Swift symbol demangling.
This crate wraps a vendored copy of the Swift runtime's demangling code, providing safe, ergonomic Rust APIs for:
- Demangling Swift symbols to human-readable strings
- Traversing the full parse tree with proper lifetime management
- Extracting function metadata (async, throws, parameters, return type, etc.)
Add to your project:
cargo add swift-demangleruse swift_demangler::demangle;
let demangled = demangle("$s4main5helloSSyYaKF").unwrap();
assert_eq!(demangled, "main.hello() async throws -> Swift.String");use swift_demangler::{Context, HasFunctionSignature, HasModule, Symbol};
let ctx = Context::new();
if let Some(symbol) = Symbol::parse(&ctx, "$s4main5helloSSyYaKF") {
if let Some(func) = symbol.as_function() {
println!("Function: {}", func.name().unwrap_or("?"));
println!("Module: {}", func.module().unwrap_or("?"));
println!("Async: {}", func.is_async());
println!("Throws: {}", func.is_throwing());
}
}For detailed analysis, the raw module provides direct access to the parse tree:
use swift_demangler::Context;
use swift_demangler::raw::{Node, NodeKind};
let ctx = Context::new();
if let Some(root) = Node::parse(&ctx, "$s4main5helloSSyYaKF") {
for node in root.descendants() {
println!("{:?}: {:?}", node.kind(), node.text());
}
}By default, the crate builds the C++ library from source using CMake:
cargo buildThis requires:
- CMake 3.16+
- C++20 compiler (Clang or GCC)
To link against a pre-built library instead of building from source:
# Build the C library separately
cmake -S swift-demangling -B build -G Ninja
cmake --build build
# Link the Rust crate to the pre-built library
SWIFT_DEMANGLE_DIR=build/lib cargo build --no-default-featuresbundled(default) - Build the C++ library from source using CMakecli- Build theswift-demanglercommand-line tool
cargo build --features cli --release
./target/release/swift-demangler '$s4main5helloSSyYaKF'
./target/release/swift-demangler --tree '$s4main5helloSSyYaKF'
./target/release/swift-demangler --function '$s4main5helloSSyYaKF'This crate is licensed under the Apache License 2.0.
It includes vendored code from the Swift and LLVM projects:
- Swift: Apache License 2.0 - see
swift-demangling/vendor/LICENSE-Swift.txt - LLVM: Apache License 2.0 with LLVM Exceptions - see
swift-demangling/vendor/LICENSE-LLVM.txt