loro/crates/loro-internal/src/macros.rs
Zixuan Chen d942e3d7a2
Feat: Peritext-like rich text support (#123)
* feat: richtext wip

* feat: add insert to style range map wip

* feat: richtext state

* fix: fix style state inserting and style map

* fix: tiny vec merge err

* fix: comment err

* refactor: use new generic-btree & refine impl

* feat: fugue tracker

* feat: tracker

* feat: tracker

* fix: fix a few err in impl

* feat: init richtext content state

* feat: refactor arena

* feat: extract anchor_type info out of style flag

* refactor: state apply op more efficiently
we can now reuse the repr in state and op

* fix: new clippy errors

* refactor: use state chunk as delta item

* refactor: use two op to insert style start and style end

* feat: diff calc

* feat: handler

* fix: tracker checkout err

* fix: pass basic richtext handler tests

* fix: pass handler basic marking tests

* fix: pass all peritext criteria

* feat: snapshot encoding for richtext init

* refactor: replace Text with Richtext

* refacotr: rm text code

* fix: richtext checkout err

* refactor: diff of text and map

* refactor: del span

* refactor: event

* fix: fuzz err

* fix: pass all tests

* fix: fuzz err

* fix: list child cache err

* chore: rm debug code

* fix: encode enhanced err

* fix: encode enchanced

* fix: fix several richtext issue

* fix: richtext anchor err

* chore: rm debug code

* fix: richtext fuzz err

* feat: speedup text snapshot decode

* perf: optimize snapshot encoding

* perf: speed up decode & insert

* fix: fugue span merge err

* perf: speedup delete & id cursor map

* fix: fugue merge err

* chore: update utils

* perf: speedup text insert / del

* fix: cursor cache

* perf: reduce conversion by introducing InsertText

* perf: speed up by refined cursor cache

* chore: update gbtree dep

* refactor(wasm): use quill delta format

* chore: fix warnings
2023-10-29 14:02:13 +08:00

94 lines
2.5 KiB
Rust

/// ```no_run
/// use fxhash::FxHashMap;
/// use loro_internal::fx_map;
///
/// let mut expected = FxHashMap::default();
/// 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),*) => {
{
let mut m = fxhash::FxHashMap::default();
$(
m.insert($key, $value);
)*
m
}
};
}
/// ```no_run
/// use loro_internal::vv;
///
/// let v = vv!(1 => 2, 2 => 3);
/// assert_eq!(v.get(&1), Some(&2));
/// assert_eq!(v.get(&2), Some(&3));
/// ```
#[macro_export]
macro_rules! vv {
($($key:expr => $value:expr),*) => {
{
let mut m = $crate::version::VersionVector::default();
$(
m.insert($key, $value);
)*
m
}
};
}
#[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) {
assert!(a0 != a1);
// SAFETY: this is safe because we know a0 != a1
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) {
assert!(a0 != a1 && a1 != a2 && a0 != a2);
// SAFETY: this is safe because we know there are not multiple mutable references to the same element
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];
let (a, b, _c) = array_mut_ref!(&mut arr, [1, 2, 3]);
assert_eq!(*a, 101);
assert_eq!(*b, 102);
*a = 50;
*b = 51;
assert!(arr[1] == 50);
assert!(arr[2] == 51);
}