opendal-core 0.56.0

Apache OpenDALâ„¢: One Layer, All Storage.
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use std::future::Future;
use std::ops::DerefMut;

use crate::raw::BoxedFuture;
use crate::raw::MaybeSend;
use crate::raw::OpDelete;
use crate::*;

/// Deleter is a type erased [`Delete`]
pub type Deleter = Box<dyn DeleteDyn>;

/// The Delete trait defines interfaces for performing deletion operations.
pub trait Delete: Unpin + Send + Sync {
    /// Requests deletion of a resource at the specified path with optional arguments
    ///
    /// # Parameters
    /// - `path`: The path of the resource to delete
    /// - `args`: Additional arguments for the delete operation
    ///
    /// # Returns
    /// - `Ok(())`: The deletion request has been successfully queued (does not guarantee actual deletion)
    /// - `Err(err)`: An error occurred and the deletion request was not queued
    ///
    /// # Notes
    /// This method just queues the delete request. The actual deletion will be
    /// performed when `close` is called.
    fn delete<'a>(
        &'a mut self,
        path: &'a str,
        args: OpDelete,
    ) -> impl Future<Output = Result<()>> + MaybeSend + 'a;

    /// Close the deleter and ensure all queued deletions are executed.
    fn close(&mut self) -> impl Future<Output = Result<()>> + MaybeSend;
}

impl Delete for () {
    async fn delete(&mut self, _: &str, _: OpDelete) -> Result<()> {
        Err(Error::new(
            ErrorKind::Unsupported,
            "output deleter doesn't support delete",
        ))
    }

    async fn close(&mut self) -> Result<()> {
        Err(Error::new(
            ErrorKind::Unsupported,
            "output deleter doesn't support close",
        ))
    }
}

/// The dyn version of [`Delete`]
pub trait DeleteDyn: Unpin + Send + Sync {
    /// The dyn version of [`Delete::delete`]
    fn delete_dyn<'a>(&'a mut self, path: &'a str, args: OpDelete) -> BoxedFuture<'a, Result<()>>;

    /// The dyn version of [`Delete::close`]
    fn close_dyn(&mut self) -> BoxedFuture<'_, Result<()>>;
}

impl<T: Delete + ?Sized> DeleteDyn for T {
    fn delete_dyn<'a>(&'a mut self, path: &'a str, args: OpDelete) -> BoxedFuture<'a, Result<()>> {
        Box::pin(Delete::delete(self, path, args))
    }

    fn close_dyn(&mut self) -> BoxedFuture<'_, Result<()>> {
        Box::pin(self.close())
    }
}

impl<T: DeleteDyn + ?Sized> Delete for Box<T> {
    fn delete<'a>(
        &'a mut self,
        path: &'a str,
        args: OpDelete,
    ) -> impl Future<Output = Result<()>> + MaybeSend + 'a {
        self.deref_mut().delete_dyn(path, args)
    }

    async fn close(&mut self) -> Result<()> {
        self.deref_mut().close_dyn().await
    }
}