use alloc::{boxed::Box, vec::Vec}; use crate::errors::PicoError; pub fn static_alloc(value: T) -> &'static mut T { Box::leak(Box::new(value)) } pub fn try_buffer(capacity: usize) -> Result, PicoError> { try_buffer_with(capacity, Default::default) } fn try_buffer_with(capacity: usize, f: impl FnMut() -> T) -> Result, PicoError> { let mut buffer = Vec::new(); buffer.try_reserve_exact(capacity)?; buffer.resize_with(capacity, f); Ok(buffer) } pub fn try_static_buffer_with( capacity: usize, f: impl FnMut() -> T, ) -> Result<&'static mut [T], PicoError> { Ok(try_buffer_with(capacity, f)?.leak()) } pub fn try_static_timestamped_block_vecs( block_num: usize, block_capacity: usize, ) -> Result<&'static mut [(i64, Vec)], PicoError> { let mut blocks = Vec::new(); blocks.try_reserve_exact(block_num)?; for _ in 0..block_num { let block = try_buffer(block_capacity)?; blocks.push((0, block)); } Ok(blocks.leak()) }