opendal/layers/
type_eraser.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::fmt::Debug;
19use std::fmt::Formatter;
20
21use crate::raw::*;
22use crate::*;
23
24/// TypeEraseLayer will erase the types on internal accessor.
25///
26/// For example, we will erase `Self::Reader` to `oio::Reader` (`Box<dyn oio::Read>`).
27///
28/// # Notes
29///
30/// TypeEraseLayer is not a public accessible layer that can be used by
31/// external users. We use this layer to erase any generic types.
32pub struct TypeEraseLayer;
33
34impl<A: Access> Layer<A> for TypeEraseLayer {
35    type LayeredAccess = TypeEraseAccessor<A>;
36
37    fn layer(&self, inner: A) -> Self::LayeredAccess {
38        TypeEraseAccessor { inner }
39    }
40}
41
42/// Provide reader wrapper for backend.
43pub struct TypeEraseAccessor<A: Access> {
44    inner: A,
45}
46
47impl<A: Access> Debug for TypeEraseAccessor<A> {
48    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49        self.inner.fmt(f)
50    }
51}
52
53impl<A: Access> LayeredAccess for TypeEraseAccessor<A> {
54    type Inner = A;
55    type Reader = oio::Reader;
56    type Writer = oio::Writer;
57    type Lister = oio::Lister;
58    type Deleter = oio::Deleter;
59
60    fn inner(&self) -> &Self::Inner {
61        &self.inner
62    }
63
64    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
65        self.inner
66            .read(path, args)
67            .await
68            .map(|(rp, r)| (rp, Box::new(r) as oio::Reader))
69    }
70
71    async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
72        self.inner
73            .write(path, args)
74            .await
75            .map(|(rp, w)| (rp, Box::new(w) as oio::Writer))
76    }
77
78    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
79        self.inner
80            .delete()
81            .await
82            .map(|(rp, p)| (rp, Box::new(p) as oio::Deleter))
83    }
84
85    async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
86        self.inner
87            .list(path, args)
88            .await
89            .map(|(rp, p)| (rp, Box::new(p) as oio::Lister))
90    }
91}