如何发送包含在 include_bytes 中的文件!作为铁的回应? [英] How do I send a file included with include_bytes! as an Iron response?

查看:42
本文介绍了如何发送包含在 include_bytes 中的文件!作为铁的回应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Iron 应用程序中的 include_bytes! 发送我包含在二进制文件中的文件.我想为我的应用程序生成一个文件,它只需要很少的 HTML、CSS 和 JS 文件.这是我正在摆弄的一个小型测试设置:

I'm trying to send a file that I included in the binary with include_bytes! in an Iron application. I want to end up with a single file for my application and it only needs very few HTML, CSS and JS files. Here's a small test setup that I'm fiddling with:

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]

Of course, this doesn't work since index_html is of type &[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 特性应该是什么.

Since I'm quite new to Rust and Iron, I don't have an idea how to approach this. I tried to learn something from the Iron docs but I think my Rust knowledge is not sufficient to really understand them, especially what this modifier::Modifier trait is supposed to be.

我怎样才能做到这一点?我可以将我的静态资源的类型转换成 Iron 可以接受的东西,还是我需要以某种方式实现这个 Modifier 特性?

How can I achieve this? Can I covert the type of my static resource into something that Iron will accept or do I need to implement this Modifier trait somehow?

推荐答案

编译器建议替代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 变量会更容易,而且由于我们必须指定常量的类型,让我们将其指定为 &'静态 [u8].

To ensure the slice lives long enough, it's easier to replace the index_html variable with a global constant, and since we must specify the type of constants, let's specify it as &'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 的实现,但不幸的是,我认为它们没有列出.但是,我在 Modifier 的源代码中找到了一些实现.code>iron::modifiers 模块.

By the way, I tried to find implementations for Modifier in the documentation, but I think they're not listed, unfortunately. However, I found some implementations for Modifier<Response> in the source for the iron::modifiers module.

这篇关于如何发送包含在 include_bytes 中的文件!作为铁的回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆