feat: impl yata insert_at

This commit is contained in:
Zixuan Chen 2022-10-03 18:00:56 +08:00
parent cd95e2276c
commit ec596792f6
3 changed files with 25 additions and 20 deletions

View file

@ -1,6 +1,4 @@
use std::ptr::NonNull;
use rle::{rle_tree::node::LeafNode, HasLength};
use rle::HasLength;
use crate::{
id::{Counter, ID},
@ -11,7 +9,7 @@ use crate::{
use self::{
content_map::ContentMap,
cursor_map::CursorMap,
cursor_map::{make_notify, CursorMap},
y_span::{Status, YSpan},
};
@ -58,20 +56,7 @@ impl Tracker {
status: Status::new(),
len,
},
&mut |yspan, leaf| {
id_to_cursor.set(
yspan.id.into(),
cursor_map::Marker::Insert {
// SAFETY: marker can only live while the bumpalo is alive. so we are safe to change lifetime here
ptr: unsafe {
NonNull::new_unchecked(
leaf as usize as *mut LeafNode<'static, _, _>,
)
},
len: yspan.len(),
},
)
},
&mut make_notify(&mut id_to_cursor),
);
});

View file

@ -107,3 +107,20 @@ impl Mergable for Marker {
}
pub(super) type CursorMap = RangeMap<u128, Marker>;
pub(super) fn make_notify(
map: &mut CursorMap,
) -> impl for<'a> FnMut(&YSpan, *mut LeafNode<'a, YSpan, YSpanTreeTrait>) + '_ {
|span, leaf| {
map.set(
span.id.into(),
Marker::Insert {
// SAFETY: marker can only live while the bumpalo is alive. so we are safe to change lifetime here
ptr: unsafe {
NonNull::new_unchecked(leaf as usize as *mut LeafNode<'static, _, _>)
},
len: span.len(),
},
)
}
}

View file

@ -6,7 +6,7 @@ use crate::id::ID;
use super::{
content_map::ContentMap,
y_span::{YSpan, YSpanTreeTrait},
Tracker,
Tracker, cursor_map::make_notify,
};
#[derive(Default, Debug)]
@ -69,7 +69,10 @@ impl ListCrdt for YataImpl {
}
fn insert_at(container: &mut Self::Container, op: Self::OpUnit, pos: usize) {
todo!()
let mut notify = make_notify(&mut container.id_to_cursor);
container.content.with_tree_mut(|tree| {
tree.insert_notify(pos, op, &mut notify);
})
}
fn id(op: &Self::OpUnit) -> Self::OpId {