我正在尝试include_bytes!在Iron应用程序中发送包含在二进制文件中的文件.我想最终为我的应用程序提供一个文件,它只需要很少的HTML,CSS和JS文件.这是一个我正在摆弄的小型测试设置:
extern crate iron;
use iron::prelude::*;
use iron::status;
use iron::mime::Mime;
fn main() {
let index_html = include_bytes!("static/index.html");
println!("Hello, world!");
Iron::new(| _: &mut Request| {
let content_type = "text/html".parse::<Mime>().unwrap();
Ok(Response::with((content_type, status::Ok, index_html)))
}).http("localhost:8001").unwrap();
}
Run Code Online (Sandbox Code Playgroud)
当然,这种index_html类型不起作用&[u8; 78]
src/main.rs:16:12: 16:26 error: the trait `modifier::Modifier<iron::response::Response>` is not implemented for the type `&[u8; 78]` [E0277]
src/main.rs:16 Ok(Response::with((content_type, status::Ok, index_html)))
Run Code Online (Sandbox Code Playgroud)
由于我对Rust和Iron很新,我不知道如何处理这个问题.我试图从Iron文档中学到一些东西,但我认为我的Rust知识不足以真正理解它们,特别是这个modifier::Modifier特性应该是什么.
我怎样才能做到这一点?我可以将我的静态资源类型转换为Iron将接受的东西,或者我需要以Modifier某种方式实现此特征吗?
Fra*_*gné 10
编译器提出了另一种选择impl:
src/main.rs:13:12: 13:26 help: the following implementations were found:
src/main.rs:13:12: 13:26 help: <&'a [u8] as modifier::Modifier<iron::response::Response>>
Run Code Online (Sandbox Code Playgroud)
为了确保切片的寿命足够长,index_html用全局常量替换变量更容易,因为我们必须指定常量的类型,所以我们将其指定为&'static [u8].
extern crate iron;
use iron::prelude::*;
use iron::status;
use iron::mime::Mime;
const INDEX_HTML: &'static [u8] = include_bytes!("static/index.html");
fn main() {
println!("Hello, world!");
Iron::new(| _: &mut Request| {
let content_type = "text/html".parse::<Mime>().unwrap();
Ok(Response::with((content_type, status::Ok, INDEX_HTML)))
}).http("localhost:8001").unwrap();
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我试图Modifier在文档中找到实现,但不幸的是我认为它们没有列出.然而,我发现了一些实现了Modifier<Response>在源的iron::modifiers模块.