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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::ffi::CStr;
use std::os::raw::*;
#[repr(C)]
pub struct Feature {
uri: *const c_char,
data: *mut c_void,
}
pub type FeaturesList = [&'static Feature];
impl Feature {
pub fn uri(&self) -> Option<&CStr> {
if !self.uri.is_null() {
Some(unsafe { CStr::from_ptr(self.uri) })
} else {
None
}
}
pub unsafe fn data<T>(&mut self) -> Option<&mut T> {
(self.data as *mut T).as_mut()
}
pub fn get_feature_raw(features: &FeaturesList, uri: &CStr) -> Option<*mut c_void> {
Some(
(features
.iter()
.find(|feature| unsafe { CStr::from_ptr(feature.uri) } == uri)?)
.data,
)
}
pub unsafe fn get_feature<T>(features: &FeaturesList, uri: &CStr) -> Option<&'static mut T> {
let feature = Self::get_feature_raw(features, uri)?;
(feature as *mut T).as_mut()
}
}
#[cfg(test)]
#[test]
fn test_map_features() {
const FEATURE_0_URI: &[u8] = b"http://example.org/Feature0\0";
const FEATURE_0_DATA: f64 = 42.0;
const FEATURE_0: Feature = Feature {
uri: FEATURE_0_URI.as_ptr() as *const c_char,
data: &FEATURE_0_DATA as *const f64 as *mut f64 as *mut c_void,
};
const FEATURE_1_URI: &[u8] = b"http://example.org/Feature1\0";
const FEATURE_1_DATA: f64 = 17.0;
const FEATURE_1: Feature = Feature {
uri: FEATURE_1_URI.as_ptr() as *const c_char,
data: &FEATURE_1_DATA as *const f64 as *mut f64 as *mut c_void,
};
const FEATURES: [&Feature; 2] = [&FEATURE_0, &FEATURE_1];
unsafe {
let feature_0_data = Feature::get_feature::<f64>(
&FEATURES,
CStr::from_bytes_with_nul(FEATURE_0_URI).unwrap(),
)
.unwrap();
assert_eq!(42.0, *feature_0_data);
let feature_1_data = Feature::get_feature::<f64>(
&FEATURES,
CStr::from_bytes_with_nul(FEATURE_1_URI).unwrap(),
)
.unwrap();
assert_eq!(17.0, *feature_1_data);
}
}