Skip to main content

luaur_common/records/
item_interface_map.rs

1#[allow(non_snake_case)]
2#[derive(Debug, Clone, Copy)]
3pub struct ItemInterfaceMap;
4
5impl ItemInterfaceMap {
6    #[inline]
7    #[allow(non_snake_case)]
8    pub fn getKey<Key, Value>(item: &(Key, Value)) -> &Key {
9        &item.0
10    }
11
12    #[inline]
13    #[allow(non_snake_case)]
14    pub fn setKey<Key, Value>(item: &mut (Key, Value), key: Key) {
15        item.0 = key;
16    }
17
18    /// # Safety
19    /// `data` must be valid for `count` elements.
20    /// This function performs manual initialization of the memory.
21    pub unsafe fn fill<Key: Clone, Value: Default>(
22        data: *mut (Key, Value),
23        count: usize,
24        key: &Key,
25    ) {
26        for i in 0..count {
27            let item_ptr = data.add(i);
28            core::ptr::write(core::ptr::addr_of_mut!((*item_ptr).0), key.clone());
29            core::ptr::write(core::ptr::addr_of_mut!((*item_ptr).1), Value::default());
30        }
31    }
32
33    /// # Safety
34    /// `data` must be valid for `count` elements and must be initialized.
35    pub unsafe fn destroy<Key, Value>(data: *mut (Key, Value), count: usize) {
36        for i in 0..count {
37            core::ptr::drop_in_place(data.add(i));
38        }
39    }
40}