}
/// Trait that makes an object appendable.
-pub trait Append {
+pub trait Append: Default {
/// Append another object of the same type onto `self`.
fn append(&mut self, other: Self);
/// Returns whether the structure is considered empty.
fn is_empty(&self) -> bool;
+
+ /// Take the value, replacing it with the default value.
+ fn take(&mut self) -> Option<Self> {
+ if self.is_empty() {
+ None
+ } else {
+ Some(core::mem::take(self))
+ }
+ }
}
impl<K: Ord, V> Append for BTreeMap<K, V> {
/// Take the staged [`ChangeSet`] to be persisted now (if any).
pub fn take_staged(&mut self) -> Option<ChangeSet> {
- if self.stage.is_empty() {
- None
- } else {
- Some(core::mem::take(&mut self.stage))
- }
+ self.stage.take()
}
/// Get a reference to the inner [`TxGraph`].
if last_db_commit.elapsed() >= DB_COMMIT_DELAY {
let db = &mut *db.lock().unwrap();
last_db_commit = Instant::now();
- if !db_stage.is_empty() {
- db.append_changeset(&core::mem::take(&mut db_stage))?;
+ if let Some(changeset) = db_stage.take() {
+ db.append_changeset(&changeset)?;
}
println!(
"[{:>10}s] committed to db (took {}s)",
{
let db = &mut *db.lock().unwrap();
db_stage.append((local_chain::ChangeSet::default(), graph_changeset));
- if !db_stage.is_empty() {
- db.append_changeset(&core::mem::take(&mut db_stage))?;
+ if let Some(changeset) = db_stage.take() {
+ db.append_changeset(&changeset)?;
}
}
}
if last_db_commit.elapsed() >= DB_COMMIT_DELAY {
let db = &mut *db.lock().unwrap();
last_db_commit = Instant::now();
- if !db_stage.is_empty() {
- db.append_changeset(&core::mem::take(&mut db_stage))?;
+ if let Some(changeset) = db_stage.take() {
+ db.append_changeset(&changeset)?;
}
println!(
"[{:>10}s] committed to db (took {}s)",