pub struct Event {
pub inner: Arc<EventInner>,
pub state: Option<Arc<dyn Any + Send + Sync>>,
pub body: Bytes,
}Expand description
Request event containing all request information
/ 包含所有请求信息的请求事件
The Event type provides lazy, cached access to request data through
helper functions, avoiding the need for complex Axum extractor signatures.
Event 类型通过辅助函数提供延迟缓存的数据访问,无需复杂的 Axum 提取器签名。
§Cloning
§克隆
Event is cheap to clone because it uses Arc internally.
Clones share the same inner data.
Event 克隆成本低,因为内部使用 Arc。克隆共享相同的内部数据。
§Thread Safety
§线程安全
Event can be safely shared between threads using Arc.
Event 可以使用 Arc 在线程间安全共享。
Fields§
§inner: Arc<EventInner>Inner event data / 内部事件数据
state: Option<Arc<dyn Any + Send + Sync>>Application state (type-erased, stored as Arc<dyn Any + Send + Sync>) / 应用状态(类型擦除,存储为 Arc<dyn Any + Send + Sync>)
body: BytesRequest body bytes / 请求体字节数据
Implementations§
Source§impl Event
impl Event
Sourcepub fn new(
method: Method,
path: String,
raw_uri: Uri,
headers: HeaderMap,
params: HashMap<String, String>,
query: HashMap<String, String>,
body: Bytes,
) -> Self
pub fn new( method: Method, path: String, raw_uri: Uri, headers: HeaderMap, params: HashMap<String, String>, query: HashMap<String, String>, body: Bytes, ) -> Self
Create a new Event with manual data
/ 使用手动数据创建新 Event
This function is typically called by the #[route] macro generated wrapper code.
此函数通常由 #[route] 宏生成的包装代码调用。
§Parameters
§参数
method- HTTP method / HTTP 方法path- Request path / 请求路径raw_uri- Original URI (for query parsing) / 原始 URI(用于查询解析)headers- Request headers / 请求头params- Path parameters / 路径参数query- Query parameters / 查询参数body- Request body bytes / 请求体字节数据
Sourcepub fn uri(&self) -> &Uri
pub fn uri(&self) -> &Uri
Get the original URI
/ 获取原始 URI
Returns the complete URI including path and query string.
返回完整的 URI,包括路径和查询字符串。
Sourcepub fn headers(&self) -> &HeaderMap
pub fn headers(&self) -> &HeaderMap
Get request headers
/ 获取请求头
Returns a reference to the complete header map.
返回完整请求头的引用。
Sourcepub fn params(&self) -> &HashMap<String, String>
pub fn params(&self) -> &HashMap<String, String>
Get path parameters (lazy cached)
/ 获取路径参数(延迟缓存)
Returns a reference to the path parameters map.
返回路径参数映射的引用。
Note: For more convenient access, use get_param
or get_param_required.
注意:为了更方便的访问,请使用 get_param
或 get_param_required。
Sourcepub fn query(&self) -> &HashMap<String, String>
pub fn query(&self) -> &HashMap<String, String>
Get query parameters (lazy cached)
/ 获取查询参数(延迟缓存)
Query parameters are parsed from the URI on first access and cached.
查询参数在首次访问时从 URI 解析并缓存。
Note: For more convenient access, use get_query
or get_query_param.
注意:为了更方便的访问,请使用 get_query
或 get_query_param。
Sourcepub fn state<T: Clone + Send + Sync + 'static>(&self) -> Option<T>
pub fn state<T: Clone + Send + Sync + 'static>(&self) -> Option<T>
Get a value from the application state
/ 从应用状态获取值
§Type Parameters
§类型参数
T- The type to retrieve (must beClone + Send + Sync + 'static) 要检索的类型(必须是Clone + Send + Sync + 'static)
§Example
§示例
use astrea::prelude::*;
struct DatabasePool {
// ...
}
#[route]
async fn handler(event: Event) -> Result<Response> {
let pool = event.state::<DatabasePool>()
.ok_or_else(|| RouteError::internal("Database pool not found"))?;
// Use pool...
}Sourcepub fn parse_json<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T>
pub fn parse_json<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T>
Parse JSON body from bytes
/ 从字节解析 JSON 请求体
This is a convenience method typically used by the generated wrapper code.
这是一个便捷方法,通常由生成的包装代码使用。
§Errors
§错误
Returns RouteError::BadRequest if the JSON is invalid.
如果 JSON 无效,返回 RouteError::BadRequest。
§Example
§示例
#[derive(Deserialize)]
struct CreateUserRequest {
name: String,
email: String,
}
#[route]
async fn handler(event: Event, bytes: Bytes) -> Result<Response> {
let body: CreateUserRequest = event.parse_json(&bytes)?;
json(json!({ "message": format!("User {} created", body.name) }))
}Sourcepub fn parse_form<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T>
pub fn parse_form<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T>
Parse form data from bytes
/ 从字节解析表单数据
Parses URL-encoded form data from the request body.
从请求体解析 URL 编码的表单数据。
§Errors
§错误
Returns RouteError::BadRequest if the form data is invalid.
如果表单数据无效,返回 RouteError::BadRequest。
§Example
§示例
#[derive(Deserialize)]
struct LoginForm {
username: String,
password: String,
}
#[route]
async fn handler(event: Event, bytes: Bytes) -> Result<Response> {
let form: LoginForm = event.parse_form(&bytes)?;
// Process login...
}