如何发送包含在 include_bytes 中的文件!作为铁反应?
How do I send a file included with include_bytes! as an Iron response?
我正在尝试在 Iron 应用程序中发送包含在 include_bytes!
二进制文件中的文件。我想为我的应用程序得到一个文件,它只需要很少的 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();
}
当然,这不起作用,因为 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)))
由于我对 Rust 和 Iron 很陌生,所以我不知道如何处理这个问题。我试图从 Iron 文档中学习一些东西,但我认为我的 Rust 知识不足以真正理解它们,尤其是这个 modifier::Modifier
特征应该是什么。
我怎样才能做到这一点?我可以将我的静态资源的类型转换成 Iron 可以接受的类型吗?或者我是否需要以某种方式实现这个 Modifier
特性?
编译器建议替代方案 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>>
为了确保切片足够长的寿命,将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();
}
顺便说一下,我试图在文档中找到 Modifier
的实现,但不幸的是我认为它们没有列出。但是,我在 the source for the iron::modifiers
module.
中找到了 Modifier<Response>
的一些实现
我正在尝试在 Iron 应用程序中发送包含在 include_bytes!
二进制文件中的文件。我想为我的应用程序得到一个文件,它只需要很少的 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();
}
当然,这不起作用,因为 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)))
由于我对 Rust 和 Iron 很陌生,所以我不知道如何处理这个问题。我试图从 Iron 文档中学习一些东西,但我认为我的 Rust 知识不足以真正理解它们,尤其是这个 modifier::Modifier
特征应该是什么。
我怎样才能做到这一点?我可以将我的静态资源的类型转换成 Iron 可以接受的类型吗?或者我是否需要以某种方式实现这个 Modifier
特性?
编译器建议替代方案 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>>
为了确保切片足够长的寿命,将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();
}
顺便说一下,我试图在文档中找到 Modifier
的实现,但不幸的是我认为它们没有列出。但是,我在 the source for the iron::modifiers
module.
Modifier<Response>
的一些实现