-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetting.rs
More file actions
36 lines (30 loc) · 1.14 KB
/
Copy pathsetting.rs
File metadata and controls
36 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
pub use macros::make_setting;
use crate::metric::Metricable;
pub struct Setting<T: Metricable> {
x: *mut T,
}
// Safety: No one besides us and the debug probe has the raw pointer, so we can safely transfer
// Setting to another thread / execution context if T can be safely transferred.
unsafe impl<T> Send for Setting<T> where T: Send + Metricable {}
// Safety: We only allow mutability through exclusive references so there is no risk
// in having multiple shared references to this value across threads/execution contexts
unsafe impl<T> Sync for Setting<T> where T: Sync + Metricable {}
/// Create using [make_setting]
///
/// ```
/// let mut setting_foo = macros::make_setting!(FOO: i32 = 3, 0..=10).unwrap();
/// let current value = setting_foo.get();
/// ```
///
/// Will create a setting which on will show as a slider on the host side with the range
/// 0..=10. The initial value will be 3.
impl<T: Metricable> Setting<T> {
/// # Safety
/// Internal use only by [make_setting]
pub const unsafe fn new(x: *mut T) -> Self {
Setting { x }
}
pub fn get(&mut self) -> T {
unsafe { self.x.read_volatile() }
}
}