opendal/layers/
type_eraser.rs1use crate::raw::*;
19use crate::*;
20use std::fmt::Debug;
21use std::fmt::Formatter;
22
23pub struct TypeEraseLayer;
32
33impl<A: Access> Layer<A> for TypeEraseLayer {
34 type LayeredAccess = TypeEraseAccessor<A>;
35
36 fn layer(&self, inner: A) -> Self::LayeredAccess {
37 TypeEraseAccessor { inner }
38 }
39}
40
41pub struct TypeEraseAccessor<A: Access> {
43 inner: A,
44}
45
46impl<A: Access> Debug for TypeEraseAccessor<A> {
47 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48 self.inner.fmt(f)
49 }
50}
51
52impl<A: Access> LayeredAccess for TypeEraseAccessor<A> {
53 type Inner = A;
54 type Reader = oio::Reader;
55 type Writer = oio::Writer;
56 type Lister = oio::Lister;
57 type Deleter = oio::Deleter;
58 type BlockingReader = oio::BlockingReader;
59 type BlockingWriter = oio::BlockingWriter;
60 type BlockingLister = oio::BlockingLister;
61 type BlockingDeleter = oio::BlockingDeleter;
62
63 fn inner(&self) -> &Self::Inner {
64 &self.inner
65 }
66
67 async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
68 self.inner
69 .read(path, args)
70 .await
71 .map(|(rp, r)| (rp, Box::new(r) as oio::Reader))
72 }
73
74 async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
75 self.inner
76 .write(path, args)
77 .await
78 .map(|(rp, w)| (rp, Box::new(w) as oio::Writer))
79 }
80
81 async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
82 self.inner
83 .delete()
84 .await
85 .map(|(rp, p)| (rp, Box::new(p) as oio::Deleter))
86 }
87
88 async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
89 self.inner
90 .list(path, args)
91 .await
92 .map(|(rp, p)| (rp, Box::new(p) as oio::Lister))
93 }
94
95 fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)> {
96 self.inner
97 .blocking_read(path, args)
98 .map(|(rp, r)| (rp, Box::new(r) as oio::BlockingReader))
99 }
100
101 fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)> {
102 self.inner
103 .blocking_write(path, args)
104 .map(|(rp, w)| (rp, Box::new(w) as oio::BlockingWriter))
105 }
106
107 fn blocking_delete(&self) -> Result<(RpDelete, Self::BlockingDeleter)> {
108 self.inner
109 .blocking_delete()
110 .map(|(rp, p)| (rp, Box::new(p) as oio::BlockingDeleter))
111 }
112
113 fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingLister)> {
114 self.inner
115 .blocking_list(path, args)
116 .map(|(rp, p)| (rp, Box::new(p) as oio::BlockingLister))
117 }
118}