opendal_core/raw/oio/copy/
api.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::future::Future;
19use std::ops::DerefMut;
20
21use crate::raw::*;
22use crate::*;
23
24/// Copier is the type-erased [`Copy`].
25pub type Copier = Box<dyn CopyDyn>;
26
27/// Copy is the trait that OpenDAL returns for stateful copy operations.
28pub trait Copy: Unpin + Send + Sync {
29    /// Drive the copy operation forward.
30    ///
31    /// `Ok(Some(n))` means the copy operation made progress by `n` bytes.
32    /// `Ok(None)` means the copy operation has completed.
33    fn next(&mut self) -> impl Future<Output = Result<Option<usize>>> + MaybeSend;
34
35    /// Abort the pending copy operation.
36    fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend;
37}
38
39impl Copy for () {
40    async fn next(&mut self) -> Result<Option<usize>> {
41        Ok(None)
42    }
43
44    async fn abort(&mut self) -> Result<()> {
45        Ok(())
46    }
47}
48
49/// OneShotCopier drives a single asynchronous copy step.
50pub struct OneShotCopier {
51    fut: Option<BoxedStaticFuture<Result<Option<usize>>>>,
52}
53
54/// # Safety
55///
56/// OneShotCopier is only accessed by `&mut self`.
57unsafe impl Sync for OneShotCopier {}
58
59/// # Safety
60///
61/// On wasm targets, futures are local but still only polled through `&mut self`.
62unsafe impl Send for OneShotCopier {}
63
64impl OneShotCopier {
65    /// Create a new one-shot copier.
66    pub fn new(fut: impl Future<Output = Result<Option<usize>>> + MaybeSend + 'static) -> Self {
67        Self {
68            fut: Some(Box::pin(fut)),
69        }
70    }
71
72    /// Create a one-shot copier that has already completed.
73    pub fn completed() -> Self {
74        Self { fut: None }
75    }
76}
77
78impl Copy for OneShotCopier {
79    async fn next(&mut self) -> Result<Option<usize>> {
80        match self.fut.take() {
81            Some(fut) => fut.await,
82            None => Ok(None),
83        }
84    }
85
86    async fn abort(&mut self) -> Result<()> {
87        self.fut = None;
88        Ok(())
89    }
90}
91
92/// CopyDyn is the dyn version of [`Copy`].
93pub trait CopyDyn: Unpin + Send + Sync {
94    /// The dyn version of [`Copy::next`].
95    fn next_dyn(&mut self) -> BoxedFuture<'_, Result<Option<usize>>>;
96
97    /// The dyn version of [`Copy::abort`].
98    fn abort_dyn(&mut self) -> BoxedFuture<'_, Result<()>>;
99}
100
101impl<T: Copy + ?Sized> CopyDyn for T {
102    fn next_dyn(&mut self) -> BoxedFuture<'_, Result<Option<usize>>> {
103        Box::pin(self.next())
104    }
105
106    fn abort_dyn(&mut self) -> BoxedFuture<'_, Result<()>> {
107        Box::pin(self.abort())
108    }
109}
110
111impl<T: CopyDyn + ?Sized> Copy for Box<T> {
112    async fn next(&mut self) -> Result<Option<usize>> {
113        self.deref_mut().next_dyn().await
114    }
115
116    async fn abort(&mut self) -> Result<()> {
117        self.deref_mut().abort_dyn().await
118    }
119}