opendal/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.
30pub trait Delete: Unpin + Send + Sync {
31    /// Requests deletion of a resource at the specified path with optional arguments
32    ///
33    /// # Parameters
34    /// - `path`: The path of the resource to delete
35    /// - `args`: Additional arguments for the delete operation
36    ///
37    /// # Returns
38    /// - `Ok(())`: The deletion request has been successfully queued (does not guarantee actual deletion)
39    /// - `Err(err)`: An error occurred and the deletion request was not queued
40    ///
41    /// # Notes
42    /// This method just queues the delete request. The actual deletion will be
43    /// performed when `close` is called.
44    fn delete<'a>(
45        &'a mut self,
46        path: &'a str,
47        args: OpDelete,
48    ) -> impl Future<Output = Result<()>> + MaybeSend + 'a;
49
50    /// Close the deleter and ensure all queued deletions are executed.
51    fn close(&mut self) -> impl Future<Output = Result<()>> + MaybeSend;
52}
53
54impl Delete for () {
55    async fn delete(&mut self, _: &str, _: OpDelete) -> Result<()> {
56        Err(Error::new(
57            ErrorKind::Unsupported,
58            "output deleter doesn't support delete",
59        ))
60    }
61
62    async fn close(&mut self) -> Result<()> {
63        Err(Error::new(
64            ErrorKind::Unsupported,
65            "output deleter doesn't support close",
66        ))
67    }
68}
69
70/// The dyn version of [`Delete`]
71pub trait DeleteDyn: Unpin + Send + Sync {
72    /// The dyn version of [`Delete::delete`]
73    fn delete_dyn<'a>(&'a mut self, path: &'a str, args: OpDelete) -> BoxedFuture<'a, Result<()>>;
74
75    /// The dyn version of [`Delete::close`]
76    fn close_dyn(&mut self) -> BoxedFuture<'_, Result<()>>;
77}
78
79impl<T: Delete + ?Sized> DeleteDyn for T {
80    fn delete_dyn<'a>(&'a mut self, path: &'a str, args: OpDelete) -> BoxedFuture<'a, Result<()>> {
81        Box::pin(Delete::delete(self, path, args))
82    }
83
84    fn close_dyn(&mut self) -> BoxedFuture<'_, Result<()>> {
85        Box::pin(self.close())
86    }
87}
88
89impl<T: DeleteDyn + ?Sized> Delete for Box<T> {
90    fn delete<'a>(
91        &'a mut self,
92        path: &'a str,
93        args: OpDelete,
94    ) -> impl Future<Output = Result<()>> + MaybeSend + 'a {
95        self.deref_mut().delete_dyn(path, args)
96    }
97
98    async fn close(&mut self) -> Result<()> {
99        self.deref_mut().close_dyn().await
100    }
101}