opendal/raw/oio/write/
api.rs1use std::future::Future;
19use std::ops::DerefMut;
20
21use crate::raw::*;
22use crate::*;
23
24pub type Writer = Box<dyn WriteDyn>;
26
27pub trait Write: Unpin + Send + Sync {
29 fn write(&mut self, bs: Buffer) -> impl Future<Output = Result<()>> + MaybeSend;
36
37 fn close(&mut self) -> impl Future<Output = Result<Metadata>> + MaybeSend;
39
40 fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend;
42}
43
44impl Write for () {
45 async fn write(&mut self, _: Buffer) -> Result<()> {
46 unimplemented!("write is required to be implemented for oio::Write")
47 }
48
49 async fn close(&mut self) -> Result<Metadata> {
50 Err(Error::new(
51 ErrorKind::Unsupported,
52 "output writer doesn't support close",
53 ))
54 }
55
56 async fn abort(&mut self) -> Result<()> {
57 Err(Error::new(
58 ErrorKind::Unsupported,
59 "output writer doesn't support abort",
60 ))
61 }
62}
63
64pub trait WriteDyn: Unpin + Send + Sync {
65 fn write_dyn(&mut self, bs: Buffer) -> BoxedFuture<Result<()>>;
66
67 fn close_dyn(&mut self) -> BoxedFuture<Result<Metadata>>;
68
69 fn abort_dyn(&mut self) -> BoxedFuture<Result<()>>;
70}
71
72impl<T: Write + ?Sized> WriteDyn for T {
73 fn write_dyn(&mut self, bs: Buffer) -> BoxedFuture<Result<()>> {
74 Box::pin(self.write(bs))
75 }
76
77 fn close_dyn(&mut self) -> BoxedFuture<Result<Metadata>> {
78 Box::pin(self.close())
79 }
80
81 fn abort_dyn(&mut self) -> BoxedFuture<Result<()>> {
82 Box::pin(self.abort())
83 }
84}
85
86impl<T: WriteDyn + ?Sized> Write for Box<T> {
87 async fn write(&mut self, bs: Buffer) -> Result<()> {
88 self.deref_mut().write_dyn(bs).await
89 }
90
91 async fn close(&mut self) -> Result<Metadata> {
92 self.deref_mut().close_dyn().await
93 }
94
95 async fn abort(&mut self) -> Result<()> {
96 self.deref_mut().abort_dyn().await
97 }
98}
99
100pub type BlockingWriter = Box<dyn BlockingWrite>;
102
103pub trait BlockingWrite: Send + Sync + 'static {
105 fn write(&mut self, bs: Buffer) -> Result<()>;
115
116 fn close(&mut self) -> Result<Metadata>;
118}
119
120impl BlockingWrite for () {
121 fn write(&mut self, bs: Buffer) -> Result<()> {
122 let _ = bs;
123
124 unimplemented!("write is required to be implemented for oio::BlockingWrite")
125 }
126
127 fn close(&mut self) -> Result<Metadata> {
128 Err(Error::new(
129 ErrorKind::Unsupported,
130 "output writer doesn't support close",
131 ))
132 }
133}
134
135impl<T: BlockingWrite + ?Sized> BlockingWrite for Box<T> {
139 fn write(&mut self, bs: Buffer) -> Result<()> {
140 (**self).write(bs)
141 }
142
143 fn close(&mut self) -> Result<Metadata> {
144 (**self).close()
145 }
146}