forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

macros.rs98 lines, 2.5 KiBHistoryRawDownload
1/// ```no_run
2/// use rustc_hash::FxHashMap;
3/// use loro_internal::fx_map;
4///
5/// let mut expected = FxHashMap::default();
6/// expected.insert("test".to_string(), "test".to_string());
7/// expected.insert("test2".to_string(), "test2".to_string());
8/// let actual = fx_map!("test".into() => "test".into(), "test2".into() => "test2".into());
9/// assert_eq!(expected, actual);
10/// ```
11#[macro_export]
12macro_rules! fx_map {
13 ($($key:expr => $value:expr),*) => {
14 {
15 let mut m = rustc_hash::FxHashMap::default();
16 $(
17 m.insert($key, $value);
18 )*
19 m
20 }
21 };
22}
23
24/// ```no_run
25/// use loro_internal::vv;
26///
27/// let v = vv!(1 => 2, 2 => 3);
28/// assert_eq!(v.get(&1), Some(&2));
29/// assert_eq!(v.get(&2), Some(&3));
30/// ```
31#[macro_export]
32macro_rules! vv {
33 ($($key:expr => $value:expr),*) => {
34 {
35 let mut m = $crate::version::VersionVector::default();
36 $(
37 m.insert($key, $value);
38 )*
39 m
40 }
41 };
42}
43
44#[macro_export]
45macro_rules! array_mut_ref {
46 ($arr:expr, [$a0:expr, $a1:expr]) => {{
47 #[inline]
48 fn borrow_mut_ref<T>(arr: &mut [T], a0: usize, a1: usize) -> (&mut T, &mut T) {
49 assert!(a0 != a1);
50 // SAFETY: this is safe because we know a0 != a1
51 unsafe {
52 (
53 &mut *(&mut arr[a0] as *mut _),
54 &mut *(&mut arr[a1] as *mut _),
55 )
56 }
57 }
58
59 borrow_mut_ref($arr, $a0, $a1)
60 }};
61 ($arr:expr, [$a0:expr, $a1:expr, $a2:expr]) => {{
62 #[inline]
63 fn borrow_mut_ref<T>(
64 arr: &mut [T],
65 a0: usize,
66 a1: usize,
67 a2: usize,
68 ) -> (&mut T, &mut T, &mut T) {
69 assert!(a0 != a1 && a1 != a2 && a0 != a2);
70 // SAFETY: this is safe because we know there are not multiple mutable references to the same element
71 unsafe {
72 (
73 &mut *(&mut arr[a0] as *mut _),
74 &mut *(&mut arr[a1] as *mut _),
75 &mut *(&mut arr[a2] as *mut _),
76 )
77 }
78 }
79
80 borrow_mut_ref($arr, $a0, $a1, $a2)
81 }};
82}
83
84#[cfg(test)]
85mod test {
86
87 #[test]
88 fn test_macro() {
89 let mut arr = vec![100, 101, 102, 103];
90 let (a, b, _c) = array_mut_ref!(&mut arr, [1, 2, 3]);
91 assert_eq!(*a, 101);
92 assert_eq!(*b, 102);
93 *a = 50;
94 *b = 51;
95 assert!(arr[1] == 50);
96 assert!(arr[2] == 51);
97 }
98}