A RPi Pico powered Lightning Detector
1use alloc::{boxed::Box, vec::Vec};
2
3use crate::errors::PicoError;
4
5pub fn static_alloc<T>(value: T) -> &'static mut T {
6 Box::leak(Box::new(value))
7}
8
9pub fn try_buffer<T: Default>(capacity: usize) -> Result<Vec<T>, PicoError> {
10 try_buffer_with(capacity, Default::default)
11}
12
13fn try_buffer_with<T>(capacity: usize, f: impl FnMut() -> T) -> Result<Vec<T>, PicoError> {
14 let mut buffer = Vec::new();
15
16 buffer.try_reserve_exact(capacity)?;
17
18 buffer.resize_with(capacity, f);
19
20 Ok(buffer)
21}
22
23pub fn try_static_buffer_with<T>(
24 capacity: usize,
25 f: impl FnMut() -> T,
26) -> Result<&'static mut [T], PicoError> {
27 Ok(try_buffer_with(capacity, f)?.leak())
28}
29
30pub fn try_static_timestamped_block_vecs<T: Default>(
31 block_num: usize,
32 block_capacity: usize,
33) -> Result<&'static mut [(i64, Vec<T>)], PicoError> {
34 let mut blocks = Vec::new();
35
36 blocks.try_reserve_exact(block_num)?;
37
38 for _ in 0..block_num {
39 let block = try_buffer(block_capacity)?;
40
41 blocks.push((0, block));
42 }
43
44 Ok(blocks.leak())
45}