Skip to main content

opendal_core/raw/oio/delete/
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::BoxedFuture;
22use crate::raw::MaybeSend;
23use crate::raw::OpDelete;
24use crate::*;
25
26/// Deleter is a type erased [`Delete`].
27pub type Deleter = Box<dyn DeleteDyn>;
28
29/// The Delete trait defines interfaces for performing deletion operations.
30///
31/// Implementations may execute each request immediately or buffer requests for
32/// batch deletion. Callers must always call [`Delete::close`] to flush pending
33/// work before dropping the deleter.
34pub trait Delete: Unpin + Send + Sync {
35    /// Request deletion of a resource at the specified path with optional arguments.
36    ///
37    /// A successful return means the deleter has accepted the request. It does
38    /// not guarantee that all backend work has completed.
39    fn delete<'a>(
40        &'a mut self,
41        path: &'a str,
42        args: OpDelete,
43    ) -> impl Future<Output = Result<()>> + MaybeSend + 'a;
44
45    /// Flush pending requests and wait until all accepted deletions are complete.
46    fn close(&mut self) -> impl Future<Output = Result<()>> + MaybeSend;
47}
48
49impl Delete for () {
50    async fn delete(&mut self, _: &str, _: OpDelete) -> Result<()> {
51        Err(Error::new(
52            ErrorKind::Unsupported,
53            "output deleter doesn't support delete",
54        ))
55    }
56
57    async fn close(&mut self) -> Result<()> {
58        Err(Error::new(
59            ErrorKind::Unsupported,
60            "output deleter doesn't support close",
61        ))
62    }
63}
64
65/// The dyn version of [`Delete`].
66pub trait DeleteDyn: Unpin + Send + Sync {
67    /// The dyn version of [`Delete::delete`].
68    fn delete_dyn<'a>(&'a mut self, path: &'a str, args: OpDelete) -> BoxedFuture<'a, Result<()>>;
69
70    /// The dyn version of [`Delete::close`].
71    fn close_dyn(&mut self) -> BoxedFuture<'_, Result<()>>;
72}
73
74impl<T: Delete + ?Sized> DeleteDyn for T {
75    fn delete_dyn<'a>(&'a mut self, path: &'a str, args: OpDelete) -> BoxedFuture<'a, Result<()>> {
76        Box::pin(Delete::delete(self, path, args))
77    }
78
79    fn close_dyn(&mut self) -> BoxedFuture<'_, Result<()>> {
80        Box::pin(self.close())
81    }
82}
83
84impl<T: DeleteDyn + ?Sized> Delete for Box<T> {
85    fn delete<'a>(
86        &'a mut self,
87        path: &'a str,
88        args: OpDelete,
89    ) -> impl Future<Output = Result<()>> + MaybeSend + 'a {
90        self.deref_mut().delete_dyn(path, args)
91    }
92
93    async fn close(&mut self) -> Result<()> {
94        self.deref_mut().close_dyn().await
95    }
96}