我正在研究 Iron 的源代码Response::with()
,试图了解它如何将元组作为修饰符应用于响应。
据我了解,修饰符只是一个构建器对象,它接受对当前上下文 ( self
) 的引用并将您希望构建的对象作为参数(只要您实现该modify
函数)。
假设我们有以下代码:
use iron::modifiers::Header;
fn hello_world(_: &mut Request) -> IronResult<Response> {
let string = get_file_as_string("./public/index.html");
let content_type = Header(ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![])));
Ok(Response::with((status::Ok, string, content_type)))
}
通过文档挖掘,我可以看到Response::with()
Iron 的实现如下:
pub fn new() -> Response {
Response {
status: None, // Start with no response code.
body: None, // Start with no body.
headers: Headers::new(),
extensions: TypeMap::new()
}
}
/// Construct a Response with the specified modifier pre-applied.
pub fn with<M: Modifier<Response>>(m: M) -> Response {
Response::new().set(m)
}
我正在努力查看我的对象元组是如何转换为修饰符的?我希望看到一个 foreach 可能迭代每个修饰符,但在这里我只看到一个 set 操作。
有人可以在这里解释执行顺序并揭示实际发生的情况吗?