This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 4973a8436b refactor(core): rename WriteGenerator to WriteState and
drop its unused generic (#8857)
4973a8436b is described below
commit 4973a8436b1a7355d8434838c472f36ffa66a7d7
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 18:33:19 2026 +0800
refactor(core): rename WriteGenerator to WriteState and drop its unused
generic (#8857)
`WriteGenerator<W>` hardcoded `W = oio::Writer` in both impl blocks, so the
parameter only added noise at every use site. The name also described the
wrong thing: the type neither generates nor reads, it owns the mutable state
of one logical write call.
Name it `WriteState` and store `oio::Writer` directly. Values now read
`WriteState::create(ctx)`, matching the immutable `WriteContext` it is built
from, and the constructor doc no longer calls it an "exact buf writer".
The type is crate-internal (`pub(crate) use context::*`), so this changes no
public API.
---
core/core/src/types/context/write.rs | 30 +++++++++++++----------
core/core/src/types/write/buffer_sink.rs | 12 ++++-----
core/core/src/types/write/futures_async_writer.rs | 10 ++++----
core/core/src/types/write/futures_bytes_sink.rs | 6 ++---
core/core/src/types/write/writer.rs | 4 +--
5 files changed, 33 insertions(+), 29 deletions(-)
diff --git a/core/core/src/types/context/write.rs
b/core/core/src/types/context/write.rs
index 4bc36db7f9..c40e659dfc 100644
--- a/core/core/src/types/context/write.rs
+++ b/core/core/src/types/context/write.rs
@@ -135,8 +135,14 @@ impl PendingCopy {
}
}
-pub struct WriteGenerator<W> {
- w: W,
+/// WriteState owns the mutable state of one logical write call.
+///
+/// It holds the raw [`oio::Writer`] opened on the composed service together
with
+/// the chunk buffer, the partially accepted write tracking, and the
`copy_from`
+/// continuation state. Every writer front-end hands its calls to this state so
+/// that chunking and abort rules stay identical across them.
+pub struct WriteState {
+ w: oio::Writer,
ctx: Option<Arc<WriteContext>>,
@@ -154,8 +160,8 @@ pub struct WriteGenerator<W> {
buffer: oio::QueueBuf,
}
-impl WriteGenerator<oio::Writer> {
- /// Create a new exact buf writer.
+impl WriteState {
+ /// Create the write state for the given write context.
pub fn create(ctx: Arc<WriteContext>) -> Result<Self> {
let (chunk_size, exact) = ctx.calculate_chunk_size();
let can_copy_from = ctx.service().capability().write_can_copy_from &&
!ctx.args().append();
@@ -187,9 +193,7 @@ impl WriteGenerator<oio::Writer> {
buffer: oio::QueueBuf::new(),
}
}
-}
-impl WriteGenerator<oio::Writer> {
/// Write as much of the buffer as the next chunk can accept.
pub async fn write(&mut self, bs: Buffer) -> Result<usize> {
self.check_writable()?;
@@ -1666,7 +1670,7 @@ mod tests {
rng.fill_bytes(&mut expected);
let buf = Arc::new(Mutex::new(vec![]));
- let mut w = WriteGenerator::new(
+ let mut w = WriteState::new(
Box::new(MockWriter {
buf: buf.clone(),
write_sizes: Arc::new(Mutex::new(vec![])),
@@ -1694,7 +1698,7 @@ mod tests {
setup();
let buf = Arc::new(Mutex::new(vec![]));
- let mut w = WriteGenerator::new(
+ let mut w = WriteState::new(
Box::new(MockWriter {
buf: buf.clone(),
write_sizes: Arc::new(Mutex::new(vec![])),
@@ -1725,7 +1729,7 @@ mod tests {
setup();
let buf = Arc::new(Mutex::new(vec![]));
- let mut w = WriteGenerator::new(
+ let mut w = WriteState::new(
Box::new(MockWriter {
buf: buf.clone(),
write_sizes: Arc::new(Mutex::new(vec![])),
@@ -1768,7 +1772,7 @@ mod tests {
setup();
let buf = Arc::new(Mutex::new(vec![]));
- let mut w = WriteGenerator::new(
+ let mut w = WriteState::new(
Box::new(MockWriter {
buf: buf.clone(),
write_sizes: Arc::new(Mutex::new(vec![])),
@@ -1817,7 +1821,7 @@ mod tests {
let buf = Arc::new(Mutex::new(vec![]));
let buffer_size = rng.random_range(1..10);
- let mut writer = WriteGenerator::new(
+ let mut writer = WriteState::new(
Box::new(MockWriter {
buf: buf.clone(),
write_sizes: Arc::new(Mutex::new(vec![])),
@@ -1851,7 +1855,7 @@ mod tests {
/// Test that when writing a large buffer in exact mode, it gets split
into chunks.
///
- /// This test verifies that WriteGenerator correctly handles large buffers
by
+ /// This test verifies that WriteState correctly handles large buffers by
/// splitting them into chunks of the configured chunk size, rather than
writing
/// everything at once.
#[tokio::test]
@@ -1863,7 +1867,7 @@ mod tests {
let buf = Arc::new(Mutex::new(vec![]));
let write_sizes = Arc::new(Mutex::new(vec![]));
- let mut writer = WriteGenerator::new(
+ let mut writer = WriteState::new(
Box::new(MockWriter {
buf: buf.clone(),
write_sizes: write_sizes.clone(),
diff --git a/core/core/src/types/write/buffer_sink.rs
b/core/core/src/types/write/buffer_sink.rs
index c39baf2c22..af3f404d60 100644
--- a/core/core/src/types/write/buffer_sink.rs
+++ b/core/core/src/types/write/buffer_sink.rs
@@ -35,9 +35,9 @@ pub struct BufferSink {
}
enum State {
- Idle(Option<WriteGenerator<oio::Writer>>),
- Writing(BoxedStaticFuture<(WriteGenerator<oio::Writer>, Result<usize>)>),
- Closing(BoxedStaticFuture<(WriteGenerator<oio::Writer>,
Result<Metadata>)>),
+ Idle(Option<WriteState>),
+ Writing(BoxedStaticFuture<(WriteState, Result<usize>)>),
+ Closing(BoxedStaticFuture<(WriteState, Result<Metadata>)>),
}
/// # Safety
@@ -48,7 +48,7 @@ unsafe impl Sync for State {}
impl BufferSink {
/// Create a new sink from a [`oio::Writer`].
#[inline]
- pub(crate) fn new(w: WriteGenerator<oio::Writer>) -> Self {
+ pub(crate) fn new(w: WriteState) -> Self {
BufferSink {
state: State::Idle(Some(w)),
buf: Buffer::new(),
@@ -242,9 +242,9 @@ mod tests {
OpWrite::new(),
OpWriter::new().with_chunk(1),
));
- let write_gen = WriteGenerator::create(ctx).unwrap();
+ let write_state = WriteState::create(ctx).unwrap();
- let v = BufferSink::new(write_gen);
+ let v = BufferSink::new(write_state);
let _: Box<dyn Unpin + MaybeSend + Sync + 'static> = Box::new(v);
}
diff --git a/core/core/src/types/write/futures_async_writer.rs
b/core/core/src/types/write/futures_async_writer.rs
index 33a8d3ad38..6c9588a9a2 100644
--- a/core/core/src/types/write/futures_async_writer.rs
+++ b/core/core/src/types/write/futures_async_writer.rs
@@ -54,7 +54,7 @@ impl fmt::Debug for FuturesAsyncWriter {
impl FuturesAsyncWriter {
/// NOTE: don't allow users to create directly.
#[inline]
- pub(crate) fn new(w: WriteGenerator<oio::Writer>) -> Self {
+ pub(crate) fn new(w: WriteState) -> Self {
FuturesAsyncWriter {
sink: BufferSink::new(w),
buf: oio::FlexBuf::new(256 * 1024),
@@ -170,8 +170,8 @@ mod tests {
}
fn new_async_writer(writer: RecordingWriter) -> FuturesAsyncWriter {
- let write_gen = WriteGenerator::new(Box::new(writer), None, false);
- FuturesAsyncWriter::new(write_gen)
+ let write_state = WriteState::new(Box::new(writer), None, false);
+ FuturesAsyncWriter::new(write_state)
}
#[tokio::test]
@@ -244,9 +244,9 @@ mod tests {
OpWrite::new(),
OpWriter::new().with_chunk(1),
));
- let write_gen = WriteGenerator::create(ctx).unwrap();
+ let write_state = WriteState::create(ctx).unwrap();
- let v = FuturesAsyncWriter::new(write_gen);
+ let v = FuturesAsyncWriter::new(write_state);
let _: Box<dyn Unpin + MaybeSend + Sync + 'static> = Box::new(v);
}
diff --git a/core/core/src/types/write/futures_bytes_sink.rs
b/core/core/src/types/write/futures_bytes_sink.rs
index b1ecd80535..f609e57700 100644
--- a/core/core/src/types/write/futures_bytes_sink.rs
+++ b/core/core/src/types/write/futures_bytes_sink.rs
@@ -38,7 +38,7 @@ pub struct FuturesBytesSink {
impl FuturesBytesSink {
/// Create a new sink from a [`oio::Writer`].
#[inline]
- pub(crate) fn new(w: WriteGenerator<oio::Writer>) -> Self {
+ pub(crate) fn new(w: WriteState) -> Self {
FuturesBytesSink {
sink: BufferSink::new(w),
}
@@ -96,9 +96,9 @@ mod tests {
OpWrite::new(),
OpWriter::new().with_chunk(1),
));
- let write_gen = WriteGenerator::create(ctx).unwrap();
+ let write_state = WriteState::create(ctx).unwrap();
- let v = FuturesBytesSink::new(write_gen);
+ let v = FuturesBytesSink::new(write_state);
let _: Box<dyn Unpin + MaybeSend + Sync + 'static> = Box::new(v);
}
diff --git a/core/core/src/types/write/writer.rs
b/core/core/src/types/write/writer.rs
index 014376fde3..f5f320cc53 100644
--- a/core/core/src/types/write/writer.rs
+++ b/core/core/src/types/write/writer.rs
@@ -101,7 +101,7 @@ use crate::*;
pub struct Writer {
/// Keep a reference to write context in writer.
ctx: Arc<WriteContext>,
- inner: WriteGenerator<oio::Writer>,
+ inner: WriteState,
}
impl fmt::Debug for Writer {
@@ -117,7 +117,7 @@ impl Writer {
/// Create a new writer from an `oio::Writer`.
pub(crate) fn new(ctx: WriteContext) -> Result<Self> {
let ctx = Arc::new(ctx);
- let inner = WriteGenerator::create(ctx.clone())?;
+ let inner = WriteState::create(ctx.clone())?;
Ok(Self { ctx, inner })
}