#![cfg_attr(feature = "nightly", feature(allocator_api))]
use allocator_api2::vec::Vec;
use talc::{DefaultBinning, TalcCell, min_first_heap_size, source::Manual};
fn main() {
let mut memory = [0u8; min_first_heap_size::<DefaultBinning>() + 10000];
let talc = TalcCell::new(Manual);
println!("Claiming memory...");
let heap_end = unsafe { talc.claim(memory.as_mut_ptr(), memory.len()) }.unwrap();
println!("Done!");
println!("Allocating a Vec, extending it, shrinking it...");
let mut vec = Vec::with_capacity_in(100, &talc);
vec.extend(0..300usize);
vec.truncate(100);
vec.shrink_to_fit();
println!("Done!");
println!("Resizing the heap...");
let alloc_end = unsafe { talc.reserved(heap_end) }.up_to;
let memory_top = memory.as_mut_ptr_range().end;
let new_end = alloc_end.as_ptr().wrapping_add(200).min(memory_top);
unsafe {
talc.resize(heap_end, new_end).unwrap();
}
println!("Done!");
println!("Shrinking the allocation again...");
vec.truncate(50);
vec.shrink_to_fit();
println!("Done!");
println!("Freeing resources...");
drop(vec);
}