2022-07-20 16:19:35 +00:00
|
|
|
/// ```no_run
|
2022-07-18 09:58:24 +00:00
|
|
|
/// use fxhash::FxHashMap;
|
2023-01-16 10:44:19 +00:00
|
|
|
/// use loro_internal::fx_map;
|
2022-07-18 09:58:24 +00:00
|
|
|
///
|
2022-07-20 16:19:35 +00:00
|
|
|
/// let mut expected = FxHashMap::default();
|
2022-07-18 09:58:24 +00:00
|
|
|
/// expected.insert("test".to_string(), "test".to_string());
|
|
|
|
/// expected.insert("test2".to_string(), "test2".to_string());
|
|
|
|
/// let actual = fx_map!("test".into() => "test".into(), "test2".into() => "test2".into());
|
|
|
|
/// assert_eq!(expected, actual);
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! fx_map {
|
|
|
|
($($key:expr => $value:expr),*) => {
|
|
|
|
{
|
2022-12-07 13:51:18 +00:00
|
|
|
let mut m = fxhash::FxHashMap::default();
|
2022-07-18 09:58:24 +00:00
|
|
|
$(
|
|
|
|
m.insert($key, $value);
|
|
|
|
)*
|
|
|
|
m
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-07-26 18:24:25 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! array_mut_ref {
|
|
|
|
($arr:expr, [$a0:expr, $a1:expr]) => {{
|
|
|
|
#[inline]
|
|
|
|
fn borrow_mut_ref<T>(arr: &mut [T], a0: usize, a1: usize) -> (&mut T, &mut T) {
|
2022-09-16 14:15:58 +00:00
|
|
|
assert!(a0 != a1);
|
|
|
|
// SAFETY: this is safe because we know a0 != a1
|
2022-07-26 18:24:25 +00:00
|
|
|
unsafe {
|
|
|
|
(
|
|
|
|
&mut *(&mut arr[a0] as *mut _),
|
|
|
|
&mut *(&mut arr[a1] as *mut _),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
borrow_mut_ref($arr, $a0, $a1)
|
|
|
|
}};
|
|
|
|
($arr:expr, [$a0:expr, $a1:expr, $a2:expr]) => {{
|
|
|
|
#[inline]
|
|
|
|
fn borrow_mut_ref<T>(
|
|
|
|
arr: &mut [T],
|
|
|
|
a0: usize,
|
|
|
|
a1: usize,
|
|
|
|
a2: usize,
|
|
|
|
) -> (&mut T, &mut T, &mut T) {
|
2022-09-16 14:15:58 +00:00
|
|
|
assert!(a0 != a1 && a1 != a2 && a0 != a2);
|
|
|
|
// SAFETY: this is safe because we know there are not multiple mutable references to the same element
|
2022-07-26 18:24:25 +00:00
|
|
|
unsafe {
|
|
|
|
(
|
|
|
|
&mut *(&mut arr[a0] as *mut _),
|
|
|
|
&mut *(&mut arr[a1] as *mut _),
|
|
|
|
&mut *(&mut arr[a2] as *mut _),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
borrow_mut_ref($arr, $a0, $a1, $a2)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_macro() {
|
|
|
|
let mut arr = vec![100, 101, 102, 103];
|
2022-08-11 11:09:07 +00:00
|
|
|
let (a, b, _c) = array_mut_ref!(&mut arr, [1, 2, 3]);
|
2022-07-26 18:24:25 +00:00
|
|
|
assert_eq!(*a, 101);
|
|
|
|
assert_eq!(*b, 102);
|
|
|
|
*a = 50;
|
|
|
|
*b = 51;
|
|
|
|
assert!(arr[1] == 50);
|
|
|
|
assert!(arr[2] == 51);
|
|
|
|
}
|