Expand description
A small module giving you a simple container that allows easy and cheap replacement of parts of its content, with the ability to commit or rollback pending changes.
Create a new Data
struct with some initial set of code,
then record changes with Data::replace_range
,
which will validate that the changes do not conflict with one another.
At any time, you can “checkpoint” the current changes with Data::commit
or roll them back (perhaps due to a conflict) with Data::restore
.
When you’re done, use Data::to_vec
to merge the original data with the changes.
§Notes
The Data::to_vec
method includes uncommitted changes, if present.
The reason for including uncommitted changes is that typically, once you’re calling those,
you’re done with edits and will be dropping the Data
struct in a moment.
In this case, requiring an extra call to commit
would be unnecessary work.
Of course, there’s no harm in calling commit
—it’s just not strictly necessary.
Put another way, the main point of commit
is to checkpoint a set of known-good changes
before applying additional sets of as-of-yet unvalidated changes.
If no future changes are expected, you aren’t required to pay the cost of commit
.
If you want to discard uncommitted changes, simply call Data::restore
first.
Structs§
- A container that allows easily replacing chunks of its data.
- Span 🔒Data that should replace a particular range of the original.