Something went wrong. Try again.
A RPi Pico powered Lightning Detector
Something went wrong. Try again.
1.5 kB · 56 lines
Rust
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657use core::pin::Pin;
use alloc::{alloc::AllocError, boxed::Box, vec::Vec};
use crate::errors::PicoError;
/// Initialise a future in-place to avoid placing the future on the stack temporarilypub fn try_box_future_within<'a, T, F, Fut>(fut: F) -> Result<Pin<Box<Fut>>, AllocError>where Fut: Future<Output = T> + 'a, F: FnOnce() -> Fut,{ let mut boxed = Box::try_new_uninit()?;
boxed.write(fut());
// SAFETY: We have initialised the Box correctly by writing to // the memory in the line above. Ok(unsafe { Box::into_pin(boxed.assume_init()) })}
pub fn static_alloc<T>(value: T) -> &'static mut T { Box::leak(Box::new(value))}
pub fn try_buffer<T: Default>(capacity: usize) -> Result<Vec<T>, PicoError> { try_buffer_with(capacity, Default::default)}
fn try_buffer_with<T>(capacity: usize, f: impl FnMut() -> T) -> Result<Vec<T>, PicoError> { let mut buffer = Vec::new();
buffer.try_reserve_exact(capacity)?;
buffer.resize_with(capacity, f);
Ok(buffer)}
pub fn try_static_buffer_with<T>( 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<const CAP: usize, T: Default + Copy>( block_num: usize,) -> Result<&'static mut [(i64, [T; CAP])], PicoError> { let mut blocks = Vec::new();
blocks.try_reserve_exact(block_num)?; blocks.resize_with(block_num, || (0, [T::default(); CAP]));
Ok(blocks.leak())}