mirror of
https://github.com/loro-dev/loro.git
synced 2024-12-01 04:28:18 +00:00
fix: when computing the len of the map, do not count elements that are None (#402)
This commit is contained in:
parent
cc5ca7d296
commit
ce88b326dd
2 changed files with 25 additions and 1 deletions
|
@ -3615,7 +3615,14 @@ impl MapHandler {
|
|||
pub fn len(&self) -> usize {
|
||||
match &self.inner {
|
||||
MaybeDetached::Detached(m) => m.try_lock().unwrap().value.len(),
|
||||
MaybeDetached::Attached(a) => a.with_state(|state| state.as_map_state().unwrap().len()),
|
||||
MaybeDetached::Attached(a) => a.with_state(|state| {
|
||||
state
|
||||
.as_map_state()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|&(_, v)| v.value.is_some())
|
||||
.count()
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -854,3 +854,20 @@ fn awareness() {
|
|||
);
|
||||
assert_eq!(b.get_all_states().get(&2).map(|x| x.state.clone()), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
// https://github.com/loro-dev/loro/issues/397
|
||||
fn len_and_is_empty_inconsistency() {
|
||||
let doc = LoroDoc::new();
|
||||
let map = doc.get_map("map");
|
||||
println!("{:#?}", map);
|
||||
assert!(map.is_empty());
|
||||
map.insert("leaf", 42i64).unwrap();
|
||||
println!("{:#?}", map.get("leaf"));
|
||||
|
||||
assert_eq!(map.len(), 1);
|
||||
map.delete("leaf").unwrap();
|
||||
println!("{:#?}", map.get("leaf"));
|
||||
assert_eq!(map.len(), 0);
|
||||
assert!(map.is_empty());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue