opendal/types/write/
futures_async_writer.rs1use std::io;
19use std::pin::Pin;
20use std::task::ready;
21use std::task::Context;
22use std::task::Poll;
23
24use futures::AsyncWrite;
25use futures::SinkExt;
26
27use crate::raw::*;
28use crate::*;
29
30pub struct FuturesAsyncWriter {
36 sink: BufferSink,
37 buf: oio::FlexBuf,
38}
39
40impl FuturesAsyncWriter {
41 #[inline]
43 pub(crate) fn new(w: WriteGenerator<oio::Writer>) -> Self {
44 FuturesAsyncWriter {
45 sink: BufferSink::new(w),
46 buf: oio::FlexBuf::new(256 * 1024),
47 }
48 }
49}
50
51impl AsyncWrite for FuturesAsyncWriter {
52 fn poll_write(
53 self: Pin<&mut Self>,
54 cx: &mut Context<'_>,
55 buf: &[u8],
56 ) -> Poll<io::Result<usize>> {
57 let this = self.get_mut();
58
59 loop {
60 let n = this.buf.put(buf);
61 if n > 0 {
62 return Poll::Ready(Ok(n));
63 }
64
65 ready!(this.sink.poll_ready_unpin(cx)).map_err(format_std_io_error)?;
66
67 let bs = this.buf.get().expect("frozen buffer must be valid");
68 this.sink
69 .start_send_unpin(Buffer::from(bs))
70 .map_err(format_std_io_error)?;
71 this.buf.clean();
72 }
73 }
74
75 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
76 let this = self.get_mut();
77
78 loop {
79 this.buf.freeze();
81 let Some(bs) = this.buf.get() else {
82 return Poll::Ready(Ok(()));
83 };
84
85 ready!(this.sink.poll_ready_unpin(cx)).map_err(format_std_io_error)?;
86 this.sink
87 .start_send_unpin(Buffer::from(bs))
88 .map_err(format_std_io_error)?;
89 this.buf.clean();
90 }
91 }
92
93 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
94 let this = self.get_mut();
95
96 loop {
97 this.buf.freeze();
99 let Some(bs) = this.buf.get() else {
100 return this.sink.poll_close_unpin(cx).map_err(format_std_io_error);
101 };
102
103 ready!(this.sink.poll_ready_unpin(cx)).map_err(format_std_io_error)?;
104 this.sink
105 .start_send_unpin(Buffer::from(bs))
106 .map_err(format_std_io_error)?;
107 this.buf.clean();
108 }
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use std::sync::Arc;
115
116 use super::*;
117 use crate::raw::MaybeSend;
118
119 #[tokio::test]
120 async fn test_trait() {
121 let op = Operator::via_iter(Scheme::Memory, []).unwrap();
122
123 let acc = op.into_inner();
124 let ctx = Arc::new(WriteContext::new(
125 acc,
126 "test".to_string(),
127 OpWrite::new(),
128 OpWriter::new().with_chunk(1),
129 ));
130 let write_gen = WriteGenerator::create(ctx).await.unwrap();
131
132 let v = FuturesAsyncWriter::new(write_gen);
133
134 let _: Box<dyn Unpin + MaybeSend + Sync + 'static> = Box::new(v);
135 }
136}