opendal_core/raw/layer.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;
19
20use crate::raw::*;
21use crate::*;
22
23/// Layer intercepts an operator's composed runtime resources.
24///
25/// A layer receives the current stack for each plane and returns the stack that
26/// should be used by operators built with this layer. Implementations can wrap
27/// the operation service, the operation context (HTTP transport and executor),
28/// or both.
29///
30/// [`Operator`][crate::Operator] applies layers by first composing the service
31/// stack with [`Layer::apply_service`], then composing [`OperationContext`] with
32/// [`Layer::apply_context`]. The context hook receives the final service stack
33/// so resource wrappers can observe service identity or capability when needed.
34///
35/// Hooks take `&self`, so layers that keep mutable state must use interior
36/// mutability. That state must remain `Send` and `Sync` because layers are
37/// shared across cloned operators and concurrent operations.
38pub trait Layer: Send + Sync + Debug + Unpin + 'static {
39 /// Intercept the operation service stack.
40 ///
41 /// Operation layers should return a service that forwards unchanged
42 /// operations to `inner`.
43 fn apply_service(&self, srv: Servicer) -> Servicer {
44 srv
45 }
46
47 /// Intercept the operation context (HTTP transport and executor).
48 ///
49 /// Return `inner` unchanged for layers that do not affect HTTP requests or
50 /// spawned tasks.
51 fn apply_context(&self, srv: Servicer, inner: OperationContext) -> OperationContext {
52 let _ = srv;
53 inner
54 }
55}