This project demonstrates two implementations of a stack in Rust: a safe implementation using Vec and an unsafe implementation using raw pointers for manual memory management. The project includes:
- Benchmarks: Comparing performance between safe and unsafe implementations using
criterion. - Fuzzing: Testing the robustness of the stack implementations using
cargo-fuzz.
- Safe Implementation:
- Uses
Vecfor memory safety. - Minimal risk of undefined behavior.
- Uses
- Unsafe Implementation:
- Manages memory manually using raw pointers.
- Optimized for performance-critical use cases.
- Tests allocation, deallocation, and pointer arithmetic.
cargofor building and managing dependencies
For benchmarks and fuzzing:
- Install
criterionfor benchmarks. - Install
cargo-fuzzfor fuzz testing (requires nightly toolchain).
git clone https://github.com/sunsided/fixedstack-rs.git
cd fixedstack-rsjust build
# or
cargo buildjust test
# or
cargo testBenchmarks are implemented using the Criterion crate. To run benchmarks:
just bench
# or
cargo benchThis compares the performance of push and pop operations for both safe and unsafe stack implementations.
Fuzz tests are implemented using cargo-fuzz. Fuzzing helps uncover edge cases and undefined behavior, especially in the unsafe implementation.
cargo install cargo-fuzzjust fuzz
# or
cargo fuzz run fuzz_stackWhen the fuzzer finds a failing input, the test case is saved in fuzz/artifacts/fuzz_stack/. To reproduce the failure:
cargo fuzz run fuzz_stack fuzz/artifacts/fuzz_stack/<failing-input>You can also minimize failing inputs for easier debugging:
cargo fuzz tmin fuzz_stack fuzz/artifacts/fuzz_stack/<failing-input>.
├── src
│ ├── lib.rs # Main library code for stack implementations
│ ├── stack_safe.rs # Safe stack implementation
│ └── stack_unsafe.rs # Unsafe stack implementation
│
├── benches
│ └── benchmarks.rs # Criterion benchmarks
│
└── fuzz
├── fuzz_targets # Directory for fuzzing targets
└── fuzz_stack.rs # Fuzz target for stack testing
use stack_experiments::stack_unsafe::FixedStack;
fn main() {
let mut stack = FixedStack::new(3);
stack.push(1);
stack.push(2);
println!("{:?}", stack.pop()); // Output: Some(2)
println!("{:?}", stack.pop()); // Output: Some(1)
}use stack_experiments::stack_unsafe::FixedStack;
fn main() {
let mut stack = FixedStack::new(3);
stack.push(1);
stack.push(2);
println!("{:?}", stack.pop()); // Output: Some(2)
println!("{:?}", stack.pop()); // Output: Some(1)
}Contributions are welcome! Feel free to fork the repository, create a feature branch, and submit a pull request.
This project is licensed under the EUPL-1.2 License. See LICENSE for details.