如何将Multipart :: from_request与tiny_http :: Request一起使用?

时间:2019-05-30 16:23:03

标签: rust traits

我正在尝试在tiny_http板条箱中使用multipart集成:

extern crate multipart;
extern crate tiny_http;

use multipart::server::tiny_http as tiny_http_trait;
use multipart::server::Multipart;
use tiny_http::*;

fn main() {
    let server = Server::http("0.0.0.0:8080").unwrap();
    loop {
        match server.recv() {
            Ok(request) => {
                let mult = Multipart::from_request(request).unwrap();
            }
            Err(s) => eprintln!("{:#?}", s),
        };
    }
}

尝试从请求中提取多部分表单数据时出现以下错误:

error[E0277]: the trait bound `tiny_http::Request: multipart::server::HttpRequest` is not satisfied
  --> src/main.rs:13:28
   |
13 |                 let mult = Multipart::from_request(request).unwrap();
   |                            ^^^^^^^^^^^^^^^^^^^^^^^ the trait `multipart::server::HttpRequest` is not implemented for `tiny_http::Request`
   |
   = help: the following implementations were found:
             <&'r mut tiny_http::Request as multipart::server::HttpRequest>
   = note: required by `multipart::server::Multipart::<()>::from_request`

该特征实现确实存在,并且我可以直接将其与HttpRequest函数一起使用:

let boundary = <&mut Request as HttpRequest>::multipart_boundary(&&mut request);

我似乎无法使用相同的语法将tiny_http::Request用作multipart::server::HttpRequest中的multipart::server::Multipart::from_request(...)

这是我的Cargo.toml

[package]
name = "db_interface"
version = "0.1.0"
authors = [""]
edition = "2018"

[dependencies]
tiny_http = "0.6.2"

[dependencies.multipart]
version = "0.16.1"
features = ["tiny_http"]

1 个答案:

答案 0 :(得分:1)

正如编译器告诉您的,您需要一个可变的引用:

<&'r mut tiny_http::Request as multipart::server::HttpRequest>

将请求声明为可变的,然后对其进行引用:

match server.recv() {
    Ok(mut request) => {
        let mult = Multipart::from_request(&mut request).unwrap();
    }
    Err(s) => eprintln!("{:#?}", s),
};