StackSafe
StackSafe enables safe execution of deeply recursive algorithms through intelligent stack management, eliminating the need for manual stack size tuning or complex refactoring to iterative approaches.
Quick Start
Add StackSafe to your Cargo.toml
:
[]
= "0.1"
Transform recursive functions with the #[stacksafe]
attribute:
use stacksafe;
// Safe for any input size
println!;
Recursive Data Structures
Use StackSafe<T>
to wrap recursive data structures:
use ;
How It Works
-
#[stacksafe]
attribute monitors remaining stack space at function entry points. When available space falls below a threshold (default: 128 KiB), it automatically allocates a new stack segment (default: 2 MiB) and continues execution. -
StackSafe<T>
is a wrapper type that transparently implement common traits likeClone
,Debug
, andPartialEq
with#[stacksafe]
support, allowing you to use it in recursive data structures without losing functionality.
Configuration
Customize stack management behavior:
use ;
// Trigger allocation when < 64 KiB remaining (default: 128 KiB).
set_minimum_stack_size;
// Allocate 4 MiB stacks for deep recursion (default: 2 MiB).
set_stack_allocation_size;
Feature Flags
StackSafe supports several optional features:
serde
: Enables stack-safe serialization and deserialization forStackSafe<T>
wrappers.derive-visitor
: Provides stack-safe visitor pattern implementations forStackSafe<T>
.
Platform Support
StackSafe works on all major platforms supported by the stacker
crate, including:
- Linux (x86_64, ARM64, others)
- macOS (Intel, Apple Silicon)
- Windows (MSVC, GNU)
- FreeBSD, NetBSD, OpenBSD
- And more...
License
This project is licensed under the Apache-2.0 license.
Acknowledgments
Inspired by the the excellent recursive
by Orson Peters.