#[cfg(not(feature = "panic_on_next_alloc"))]
use alloc_tracker::Allocator;
#[cfg(feature = "panic_on_next_alloc")]
use alloc_tracker::{Allocator, panic_on_next_alloc};
#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();
#[cfg(feature = "panic_on_next_alloc")]
fn main() {
println!("Demonstrating panic-on-next-allocation functionality...");
println!("Allocating some memory...");
#[expect(clippy::useless_vec, reason = "example needs to show allocation")]
let mut data = vec![1, 2, 3, 4, 5];
println!("Successfully allocated vector with {} elements", data.len());
println!("Enabling panic-on-next-allocation...");
panic_on_next_alloc(true);
println!("Performing operations that do not allocate...");
#[expect(
clippy::indexing_slicing,
reason = "we know the vector has at least 2 elements"
)]
{
data[0] = 10;
data[1] = 20;
}
let sum: i32 = data.iter().sum();
println!("Sum of modified data: {sum}");
println!("Disabling panic-on-next-allocation...");
panic_on_next_alloc(false);
println!("Allocating more memory after disabling panic-on-next-allocation...");
#[expect(clippy::useless_vec, reason = "example needs to show allocation")]
let more_data = vec![6, 7, 8, 9, 10];
println!(
"Successfully allocated another vector with {} elements",
more_data.len()
);
println!("Demonstrating one-shot behavior - enabling and then allocating twice...");
panic_on_next_alloc(true);
println!("Flag is enabled but we will not actually allocate to avoid panic");
panic_on_next_alloc(false);
println!("Example completed successfully!");
}
#[cfg(not(feature = "panic_on_next_alloc"))]
fn main() {
println!("This example requires the 'panic_on_next_alloc' feature to be enabled.");
println!("Run with: cargo run --example panic_on_alloc --features panic_on_next_alloc");
}