opendal/raw/oio/list/
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::oio::Entry;
22use crate::raw::*;
23use crate::*;
24
25/// The boxed version of [`List`]
26pub type Lister = Box<dyn ListDyn>;
27
28/// Page trait is used by [`raw::Accessor`] to implement `list` operation.
29pub trait List: Unpin + Send + Sync {
30    /// Fetch a new page of [`Entry`]
31    ///
32    /// `Ok(None)` means all pages have been returned. Any following call
33    /// to `next` will always get the same result.
34    fn next(&mut self) -> impl Future<Output = Result<Option<Entry>>> + MaybeSend;
35}
36
37impl List for () {
38    async fn next(&mut self) -> Result<Option<Entry>> {
39        Ok(None)
40    }
41}
42
43impl<P: List> List for Option<P> {
44    async fn next(&mut self) -> Result<Option<Entry>> {
45        match self {
46            Some(p) => p.next().await,
47            None => Ok(None),
48        }
49    }
50}
51
52pub trait ListDyn: Unpin + Send + Sync {
53    fn next_dyn(&mut self) -> BoxedFuture<Result<Option<Entry>>>;
54}
55
56impl<T: List + ?Sized> ListDyn for T {
57    fn next_dyn(&mut self) -> BoxedFuture<Result<Option<Entry>>> {
58        Box::pin(self.next())
59    }
60}
61
62impl<T: ListDyn + ?Sized> List for Box<T> {
63    async fn next(&mut self) -> Result<Option<Entry>> {
64        self.deref_mut().next_dyn().await
65    }
66}
67
68/// BlockingList is the blocking version of [`List`].
69pub trait BlockingList: Send {
70    /// Fetch a new page of [`Entry`]
71    ///
72    /// `Ok(None)` means all pages have been returned. Any following call
73    /// to `next` will always get the same result.
74    fn next(&mut self) -> Result<Option<Entry>>;
75}
76
77/// BlockingLister is a boxed [`BlockingList`]
78pub type BlockingLister = Box<dyn BlockingList>;
79
80impl<P: BlockingList + ?Sized> BlockingList for Box<P> {
81    fn next(&mut self) -> Result<Option<Entry>> {
82        (**self).next()
83    }
84}
85
86impl BlockingList for () {
87    fn next(&mut self) -> Result<Option<Entry>> {
88        Ok(None)
89    }
90}
91
92impl<P: BlockingList> BlockingList for Option<P> {
93    fn next(&mut self) -> Result<Option<Entry>> {
94        match self {
95            Some(p) => p.next(),
96            None => Ok(None),
97        }
98    }
99}