Context
2025年8月17日大约 1 分钟hyperlanewebrusttypectx
提示
hyperlane
框架的 Context
作为中间件和路由处理函数的唯一的参数类型,其内部保存了上下文数据,具体类型定义如下:
/// Represents the internal state of the application context.
///
/// This structure holds all the data associated with a single request-response cycle,
/// including the stream, request, response, and any custom attributes.
#[derive(Clone, Data, Default, CustomDebug, DisplayDebug)]
pub(crate) struct ContextInner {
/// A flag indicating whether the request handling has been aborted.
#[get(pub(super))]
#[get_mut(pub(super))]
#[set(pub(super))]
aborted: bool,
/// A flag indicating whether the connection has been closed.
#[get(pub(super))]
#[get_mut(pub(super))]
#[set(pub(super))]
closed: bool,
/// The underlying network stream for the connection.
#[get(pub(super))]
#[get_mut(pub(super))]
#[set(pub(super))]
stream: OptionArcRwLockStream,
/// The incoming HTTP request.
#[get(pub(super))]
#[get_mut(pub(super))]
#[set(pub(super))]
request: Request,
/// The outgoing HTTP response.
#[get(pub(super))]
#[get_mut(pub(super))]
#[set(pub(super))]
response: Response,
/// Parameters extracted from the route path.
#[get(pub(super))]
#[get_mut(pub(super))]
#[set(pub(super))]
route_params: RouteParams,
/// A collection of custom attributes for sharing data within the request lifecycle.
#[get(pub(super))]
#[get_mut(pub(super))]
#[set(pub(super))]
attributes: HashMapArcAnySendSync,
}
/// The main application context, providing thread-safe access to request and response data.
///
/// This is a wrapper around `ContextInner` that uses an `Arc<RwLock<>>` to allow
/// for shared, mutable access across asynchronous tasks.
#[derive(Clone, Default, Getter, CustomDebug, DisplayDebug)]
pub struct Context(#[get(pub(super))] pub(super) ArcRwLock<ContextInner>);