Skip to main content

opendal_core/raw/oio/write/
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/// Writer is a type-erased [`Write`].
25pub type Writer = Box<dyn WriteDyn>;
26
27/// Write is the async sink used by services and layers.
28pub trait Write: Unpin + Send + Sync {
29    /// Write the entire buffer into the writer.
30    ///
31    /// `Ok(())` means all bytes from `bs` have been accepted. Implementations
32    /// must return an error instead of treating a partial write as success.
33    fn write(&mut self, bs: Buffer) -> impl Future<Output = Result<()>> + MaybeSend;
34
35    /// Close the writer and make sure all data has been flushed.
36    fn close(&mut self) -> impl Future<Output = Result<Metadata>> + MaybeSend;
37
38    /// Abort the pending writer.
39    fn abort(&mut self) -> impl Future<Output = Result<()>> + MaybeSend;
40}
41
42impl Write for () {
43    async fn write(&mut self, _: Buffer) -> Result<()> {
44        unimplemented!("write is required to be implemented for oio::Write")
45    }
46
47    async fn close(&mut self) -> Result<Metadata> {
48        Err(Error::new(
49            ErrorKind::Unsupported,
50            "output writer doesn't support close",
51        ))
52    }
53
54    async fn abort(&mut self) -> Result<()> {
55        Err(Error::new(
56            ErrorKind::Unsupported,
57            "output writer doesn't support abort",
58        ))
59    }
60}
61
62/// WriteDyn is the object-safe version of [`Write`] used by [`Writer`].
63pub trait WriteDyn: Unpin + Send + Sync {
64    /// The dyn version of [`Write::write`].
65    fn write_dyn(&mut self, bs: Buffer) -> BoxedFuture<'_, Result<()>>;
66
67    /// The dyn version of [`Write::close`].
68    fn close_dyn(&mut self) -> BoxedFuture<'_, Result<Metadata>>;
69
70    /// The dyn version of [`Write::abort`].
71    fn abort_dyn(&mut self) -> BoxedFuture<'_, Result<()>>;
72}
73
74impl<T: Write + ?Sized> WriteDyn for T {
75    fn write_dyn(&mut self, bs: Buffer) -> BoxedFuture<'_, Result<()>> {
76        Box::pin(self.write(bs))
77    }
78
79    fn close_dyn(&mut self) -> BoxedFuture<'_, Result<Metadata>> {
80        Box::pin(self.close())
81    }
82
83    fn abort_dyn(&mut self) -> BoxedFuture<'_, Result<()>> {
84        Box::pin(self.abort())
85    }
86}
87
88impl<T: WriteDyn + ?Sized> Write for Box<T> {
89    async fn write(&mut self, bs: Buffer) -> Result<()> {
90        self.deref_mut().write_dyn(bs).await
91    }
92
93    async fn close(&mut self) -> Result<Metadata> {
94        self.deref_mut().close_dyn().await
95    }
96
97    async fn abort(&mut self) -> Result<()> {
98        self.deref_mut().abort_dyn().await
99    }
100}