pickledb-rs
pickledb-rs copied to clipboard
Add ability to iterate over all Lists in the PickleDB
This is definitely a breaking change, and would require a major version bump, since it changes a few of the existing types and methods. But I think this might be a more intuitive naming scheme for the list iterators.
The updated types are:
PickleDbListIterator
: Iterates over all lists in the DB
PickleDbListItemIterator
: Iterates over items in a specific list
PickleDbListItem
: Item yielded from a list
Updates in this PR to make these changes:
- The old
PickleDb::liter()
is now namedPickleDb::liter_get()
to obtain an iterator over a specific list. - The old
PickleDbListIterator
is now namedPickleDbListItemIterator
since it iterates over items in a list. - Changes
PickleDb::liter()
to produce an iterator over all lists in the db (the newPickleDbListIterator
) - Introduces a new
PickleDbListIterator
that yields an iterator per listPickleDbListItemIterator
let mut db = pickledb::PickleDb::new_bin("1.db", pickledb::PickleDbDumpPolicy::AutoDump);
// Create a new list
db.lcreate("list1").unwrap()
.lextend(&vec![1,2,3,4]);
// Create another one
db.lcreate("list2").unwrap()
.lextend(&vec![5,6,7,8]);
// Iterate over all lists
for list in db.liter() {
println!("Iterating over list: {:?}", list.name());
for item in list {
println!("Current item is: {}", item.get_item::<i32>().unwrap());
}
}
@landhb thank you for working on this!
I'm thinking if there's a way to make it backward compatible - maybe preserve the old list iterator (and mark it as deprecated) and introduce new iterators?
This way users can enjoy this new feature and at the same time it won't break anything for existing usage. Let me know what you think
Hey @seladb sorry for the late response. I'll see if I can feature gate it and add deprecation warnings.
@seladb Finally got some time to come back to this. I've added the deprecation warning, and by default all old code using liter()
will continue to compile and work as before. Just with the added warning:
$ cargo run --example lists
Compiling pickledb v0.5.2 (/mnt/archive/Documents/Projects/pickledb-rs)
warning: use of deprecated associated function `pickledb::PickleDb::liter`: Please switch to `pickledb::PickleDb::liter_get`
--> examples/lists/src/main.rs:110:25
|
110 | for item_iter in db.liter("list2") {
| ^^^^^
|
= note: `#[warn(deprecated)]` on by default
warning: `pickledb` (example "lists") generated 1 warning
To use the newer version of liter
users will need to opt-in to the list-iterator
feature.
Thank you @landhb ! I'm a little bit busy right now but I'll take a look soon