Merge 'eliminate the need for another Once in Completion' from Pedro Muniz

I added the `Once` before so fix a bug, but it was a bit hackery. We can
`get_or_init` to achieve the same purpose, and the code becomes much
cleaner. `get_or_init` guarantees the init will happen only once as
well.

Reviewed-by: Preston Thorpe <preston@turso.tech>
Reviewed-by: bit-aloo (@Shourya742)

Closes #3578
This commit is contained in:
Preston Thorpe
2025-10-06 19:52:41 -04:00
committed by GitHub

View File

@@ -3,7 +3,6 @@ use crate::storage::sqlite3_ondisk::WAL_FRAME_HEADER_SIZE;
use crate::{BufferPool, CompletionError, Result};
use bitflags::bitflags;
use cfg_block::cfg_block;
use parking_lot::Once;
use std::cell::RefCell;
use std::fmt;
use std::ptr::NonNull;
@@ -143,8 +142,6 @@ struct CompletionInner {
// Thread safe with OnceLock
result: std::sync::OnceLock<Option<CompletionError>>,
needs_link: bool,
/// before calling callback we check if done is true
done: Once,
/// Optional parent group this completion belongs to
parent: OnceLock<Arc<GroupCompletionInner>>,
}
@@ -298,7 +295,6 @@ impl Completion {
completion_type,
result: OnceLock::new(),
needs_link: false,
done: Once::new(),
parent: OnceLock::new(),
}),
}
@@ -310,7 +306,6 @@ impl Completion {
completion_type,
result: OnceLock::new(),
needs_link: true,
done: Once::new(),
parent: OnceLock::new(),
}),
}
@@ -420,7 +415,7 @@ impl Completion {
}
fn callback(&self, result: Result<i32, CompletionError>) {
self.inner.done.call_once(|| {
self.inner.result.get_or_init(|| {
match &self.inner.completion_type {
CompletionType::Read(r) => r.callback(result),
CompletionType::Write(w) => w.callback(result),
@@ -428,10 +423,6 @@ impl Completion {
CompletionType::Truncate(t) => t.callback(result),
CompletionType::Group(g) => g.callback(result),
};
self.inner
.result
.set(result.err())
.expect("result must be set only once");
if let Some(group) = self.inner.parent.get() {
// Capture first error in group
@@ -447,6 +438,8 @@ impl Completion {
}
// TODO: remove self from parent group
}
result.err()
});
}
@@ -459,15 +452,6 @@ impl Completion {
}
}
/// only call this method if you are sure that the completion is
/// a WriteCompletion, panics otherwise
pub fn as_write(&self) -> &WriteCompletion {
match self.inner.completion_type {
CompletionType::Write(ref w) => w,
_ => unreachable!(),
}
}
/// Link this completion to a group completion (internal use only)
fn link_internal(&mut self, group: &Completion) {
let group_inner = match &group.inner.completion_type {