fix: cursor get_sliced should have len > 0

This commit is contained in:
Zixuan Chen 2023-01-30 11:58:31 +08:00 committed by Leonzhao
parent 5bcaf4c58a
commit 9d31605bde
2 changed files with 21 additions and 1 deletions

View file

@ -173,7 +173,7 @@ impl ListContainer {
pub fn get(&self, pos: usize) -> Option<LoroValue> {
self.state
.get(pos)
.map(|range| self.raw_data.slice(&range.get_sliced().0))
.map(|range| self.raw_data.slice(&range.get_sliced_with_len(1).0))
.and_then(|slice| slice.first().cloned())
}
@ -685,3 +685,18 @@ impl ContainerWrapper for List {
self.client_id
}
}
#[cfg(test)]
mod test {
use crate::LoroCore;
#[test]
fn test_list_get() {
let mut loro = LoroCore::default();
let mut list = loro.get_list("id");
list.insert(&loro, 0, 123).unwrap();
list.insert(&loro, 1, 123).unwrap();
assert_eq!(list.get(0), Some(123.into()));
assert_eq!(list.get(1), Some(123.into()));
}
}

View file

@ -377,9 +377,14 @@ impl<'tree, T: Rle, A: RleTreeTrait<T>, M> RawSafeCursor<'tree, T, A, M> {
}
pub fn get_sliced(&self) -> T {
assert_ne!(self.0.len, 0);
self.as_ref()
.slice(self.0.offset, self.0.offset + self.0.len)
}
pub fn get_sliced_with_len(&self, len: usize) -> T {
self.as_ref().slice(self.0.offset, self.0.offset + len)
}
}
impl<'tree, T: Rle, A: RleTreeTrait<T>, M> GetOp for RawSafeCursor<'tree, T, A, M> {