opendal_core/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;
34
35 fn copy_from(
41 &mut self,
42 _path: &str,
43 _args: OpRead,
44 _range: BytesRange,
45 ) -> impl Future<Output = Result<()>> + MaybeSend {
46 async {
47 Err(Error::new(
48 ErrorKind::Unsupported,
49 "writer doesn't support native copy",
50 ))
51 }
52 }
53
54 fn close(&mut self) -> impl Future<Output = Result<Metadata>> + MaybeSend;
56
57 fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend;
59}
60
61impl Write for () {
62 async fn write(&mut self, _: Buffer) -> Result<()> {
63 unimplemented!("write is required to be implemented for oio::Write")
64 }
65
66 async fn close(&mut self) -> Result<Metadata> {
67 Err(Error::new(
68 ErrorKind::Unsupported,
69 "output writer doesn't support close",
70 ))
71 }
72
73 async fn abort(&mut self) -> Result<()> {
74 Err(Error::new(
75 ErrorKind::Unsupported,
76 "output writer doesn't support abort",
77 ))
78 }
79}
80
81pub trait WriteDyn: Unpin + Send + Sync {
83 fn write_dyn(&mut self, bs: Buffer) -> BoxedFuture<'_, Result<()>>;
85
86 fn copy_from_dyn<'a>(
88 &'a mut self,
89 path: &'a str,
90 args: OpRead,
91 range: BytesRange,
92 ) -> BoxedFuture<'a, Result<()>>;
93
94 fn close_dyn(&mut self) -> BoxedFuture<'_, Result<Metadata>>;
96
97 fn abort_dyn(&mut self) -> BoxedFuture<'_, Result<()>>;
99}
100
101impl<T: Write + ?Sized> WriteDyn for T {
102 fn write_dyn(&mut self, bs: Buffer) -> BoxedFuture<'_, Result<()>> {
103 Box::pin(self.write(bs))
104 }
105
106 fn copy_from_dyn<'a>(
107 &'a mut self,
108 path: &'a str,
109 args: OpRead,
110 range: BytesRange,
111 ) -> BoxedFuture<'a, Result<()>> {
112 Box::pin(self.copy_from(path, args, range))
113 }
114
115 fn close_dyn(&mut self) -> BoxedFuture<'_, Result<Metadata>> {
116 Box::pin(self.close())
117 }
118
119 fn abort_dyn(&mut self) -> BoxedFuture<'_, Result<()>> {
120 Box::pin(self.abort())
121 }
122}
123
124impl<T: WriteDyn + ?Sized> Write for Box<T> {
125 async fn write(&mut self, bs: Buffer) -> Result<()> {
126 self.deref_mut().write_dyn(bs).await
127 }
128
129 async fn copy_from(&mut self, path: &str, args: OpRead, range: BytesRange) -> Result<()> {
130 self.deref_mut().copy_from_dyn(path, args, range).await
131 }
132
133 async fn close(&mut self) -> Result<Metadata> {
134 self.deref_mut().close_dyn().await
135 }
136
137 async fn abort(&mut self) -> Result<()> {
138 self.deref_mut().abort_dyn().await
139 }
140}