Crates.io | transactional_iterator |
lib.rs | transactional_iterator |
version | 0.4.0 |
source | src |
created_at | 2024-05-22 09:40:45.488012 |
updated_at | 2024-05-23 13:16:27.638306 |
description | Iterator that allows to commit or abort progress. |
homepage | |
repository | https://git.pipapo.org/cehteh/transactional_iterator.git |
max_upload_size | |
id | 1247755 |
size | 24,324 |
A Transaction
can be used to make changes to an iterator and only apply the changes if the
transaction is committed. When the Transaction
is rolled back, the changes are
discarded. This is useful for implementing backtracking searches, parsers, undo functionality,
unlimited peeking and more.
The original iterator must implement 'Clone' to be useable with the transactional iterator.
Transactions can be created with 3 different policies:
Panic
:
Will panic on drop if not committed or rolled back.
Rollback
:
Will rollback changes on drop or panic.
AutoCommit
:
Will commit changes on drop or panic.
use transactional_iterator::{Transaction, Panic};
let mut iter = vec![1, 2, 3].into_iter();
let mut transaction = Transaction::new(&mut iter, Panic);
// iterate within the transaction
assert_eq!(transaction.next(), Some(1));
assert_eq!(transaction.next(), Some(2));
// Commit the transaction
transaction.commit();
// The changes are now applied
assert_eq!(iter.next(), Some(3));