opendal_core/raw/oio/copy/
api.rs1use std::future::Future;
19use std::ops::DerefMut;
20
21use crate::raw::*;
22use crate::*;
23
24pub type Copier = Box<dyn CopyDyn>;
26
27pub trait Copy: Unpin + Send + Sync {
29 fn next(&mut self) -> impl Future<Output = Result<Option<usize>>> + MaybeSend;
34
35 fn close(&mut self) -> impl Future<Output = Result<Metadata>> + MaybeSend;
37
38 fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend;
40}
41
42impl Copy for () {
43 async fn next(&mut self) -> Result<Option<usize>> {
44 Ok(None)
45 }
46
47 async fn close(&mut self) -> Result<Metadata> {
48 Ok(Metadata::default())
49 }
50
51 async fn abort(&mut self) -> Result<()> {
52 Ok(())
53 }
54}
55
56pub struct OneShotCopier {
58 fut: Option<BoxedStaticFuture<Result<Metadata>>>,
59 meta: Option<Metadata>,
60}
61
62unsafe impl Sync for OneShotCopier {}
66
67unsafe impl Send for OneShotCopier {}
71
72impl OneShotCopier {
73 pub fn new(fut: impl Future<Output = Result<Metadata>> + MaybeSend + 'static) -> Self {
75 Self {
76 fut: Some(Box::pin(fut)),
77 meta: None,
78 }
79 }
80
81 pub fn completed() -> Self {
83 Self {
84 fut: None,
85 meta: Some(Metadata::default()),
86 }
87 }
88}
89
90impl Copy for OneShotCopier {
91 async fn next(&mut self) -> Result<Option<usize>> {
92 if self.meta.is_none() {
93 self.close().await?;
94 }
95
96 Ok(None)
97 }
98
99 async fn close(&mut self) -> Result<Metadata> {
100 if let Some(fut) = self.fut.take() {
101 self.meta = Some(fut.await?);
102 }
103
104 Ok(self.meta.clone().unwrap_or_default())
105 }
106
107 async fn abort(&mut self) -> Result<()> {
108 self.fut = None;
109 self.meta = None;
110 Ok(())
111 }
112}
113
114pub trait CopyDyn: Unpin + Send + Sync {
116 fn next_dyn(&mut self) -> BoxedFuture<'_, Result<Option<usize>>>;
118
119 fn close_dyn(&mut self) -> BoxedFuture<'_, Result<Metadata>>;
121
122 fn abort_dyn(&mut self) -> BoxedFuture<'_, Result<()>>;
124}
125
126impl<T: Copy + ?Sized> CopyDyn for T {
127 fn next_dyn(&mut self) -> BoxedFuture<'_, Result<Option<usize>>> {
128 Box::pin(self.next())
129 }
130
131 fn close_dyn(&mut self) -> BoxedFuture<'_, Result<Metadata>> {
132 Box::pin(self.close())
133 }
134
135 fn abort_dyn(&mut self) -> BoxedFuture<'_, Result<()>> {
136 Box::pin(self.abort())
137 }
138}
139
140impl<T: CopyDyn + ?Sized> Copy for Box<T> {
141 async fn next(&mut self) -> Result<Option<usize>> {
142 self.deref_mut().next_dyn().await
143 }
144
145 async fn close(&mut self) -> Result<Metadata> {
146 self.deref_mut().close_dyn().await
147 }
148
149 async fn abort(&mut self) -> Result<()> {
150 self.deref_mut().abort_dyn().await
151 }
152}