stm32f1_hal/common/
holder.rs1use super::{
2 atomic_cell::AtomicCellMember,
3 fugit::{Duration, Rate},
4};
5use core::cell::UnsafeCell;
6
7pub struct StaticHolder<T: Copy> {
11 inner: UnsafeCell<T>,
12}
13
14unsafe impl<T: Copy> Sync for StaticHolder<T> {}
15
16impl<T: Copy> StaticHolder<T> {
17 pub const fn new(v: T) -> Self {
18 Self {
19 inner: UnsafeCell::new(v),
20 }
21 }
22
23 #[inline]
24 pub fn set(&self, v: T) {
25 critical_section::with(|_| {
26 *self.get_inner() = v;
27 });
28 }
29
30 #[inline]
34 pub unsafe fn get(&self) -> &T {
35 self.get_inner()
36 }
37
38 #[inline]
39 #[allow(clippy::mut_from_ref)]
40 fn get_inner(&self) -> &mut T {
41 unsafe { &mut *self.inner.get() }
42 }
43}
44
45impl<const NOM: u32, const DENOM: u32> AtomicCellMember for Duration<u32, NOM, DENOM> {
46 #[inline(always)]
47 fn to_num(self) -> usize {
48 self.ticks() as usize
49 }
50
51 #[inline(always)]
52 unsafe fn from_num(value: usize) -> Self {
53 Self::from_ticks(value as u32)
54 }
55}
56
57impl<const NOM: u32, const DENOM: u32> AtomicCellMember for Rate<u32, NOM, DENOM> {
58 #[inline(always)]
59 fn to_num(self) -> usize {
60 self.raw() as usize
61 }
62
63 #[inline(always)]
64 unsafe fn from_num(value: usize) -> Self {
65 Self::from_raw(value as u32)
66 }
67}