Trait opendal::Layer

source ·
pub trait Layer {
    fn layer(&self, inner: Arc<dyn Accessor>) -> Arc<dyn Accessor>;
}
Expand description

Layer is used to intercept the operations on the underlying storage.

Struct that implement this trait must accept input Arc<dyn Accessor> as inner, and returns a new Arc<dyn Accessor> as output.

All functions in Accessor requires &self, so it’s implementor’s responsibility to maintain the internal mutability. Please also keep in mind that Accessor requires Send and Sync.

Notes

Inner

It’s required to implement fn inner() -> Option<Arc<dyn Accessor>> for layer’s accessors.

By implement this method, all API calls will be forwarded to inner accessor instead.

List Operations

list and blocking_list operations will set Arc<dyn Accessor> for ObjectEntry.

All layers must make sure the accessor is set correctly, for example:

impl Stream for ExampleStreamer {
    type Item = Result<ObjectEntry>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match Pin::new(&mut (*self.inner)).poll_next(cx) {
            Poll::Ready(Some(Ok(mut de))) => {
                de.set_accessor(self.acc.clone());
                Poll::Ready(Some(Ok(de)))
            }
            v => v,
        }
    }
}

Examples

use std::sync::Arc;

use opendal::Accessor;
use opendal::Layer;

/// Implement the real accessor logic here.
#[derive(Debug)]
struct TraceAccessor {
    inner: Arc<dyn Accessor>,
}

impl Accessor for TraceAccessor {}

/// The public struct that exposed to users.
///
/// Will be used like `op.layer(TraceLayer)`
struct TraceLayer;

impl Layer for TraceLayer {
    fn layer(&self, inner: Arc<dyn Accessor>) -> Arc<dyn Accessor> {
        Arc::new(TraceAccessor { inner })
    }
}

Required Methods

Intercept the operations on the underlying storage.

Implementors