特性 std::convert::From<String> 未为 hyper::body::Body 实现
Trait std::convert::From<String> not implemented for hyper::body::Body
lazy_static::lazy_static! {
static ref file_data: String = fs::read_to_string("static/login.html").expect("unable to read from static/login.html");
}
#[tokio::main]
async fn main() {
// code omitted
let login = warp::path("login").map(move || warp::reply::html(file_data));
// code omitted
}
编译错误:
error[E0277]: the trait bound `hyper::body::body::Body: std::convert::From<file_data>` is not satisfied
--> src/main.rs:45:49
|
45 | let login = warp::path("login").map(move || warp::reply::html(file_data));
| ^^^^^^^^^^^^^^^^^ the trait `std::convert::From<file_data>` is not implemented for `hyper::body::body::Body`
|
::: /home/ichi/.cargo/registry/src/github.com-1ecc6299db9ec823/warp-0.2.3/src/reply.rs:170:11
|
170 | Body: From<T>,
| ------- required by this bound in `warp::reply::html`
|
= help: the following implementations were found:
<hyper::body::body::Body as std::convert::From<&'static [u8]>>
<hyper::body::body::Body as std::convert::From<&'static str>>
<hyper::body::body::Body as std::convert::From<bytes::bytes::Bytes>>
<hyper::body::body::Body as std::convert::From<std::borrow::Cow<'static, [u8]>>>
and 4 others
我正在尝试将 String
传递给闭包。根据 documentation,From<String>
是为 hyper::body::Body
实现的,而 file_data
的类型是 String
。那么为什么我会收到此错误?
这里的问题是 lazy_static
创建了一个引用您的数据的包装器类型,而 hyper
不知道如何处理它。您可以使用 file_data.clone()
克隆引用的数据并从中构造一个主体,但在这种情况下实际上有一个更简单的方法。由于您的字符串具有固定值并且 Body
实现了 From<&'static str>
,因此您实际上根本不需要堆分配的 String
或 lazy_static
:您可以使用以下代码它只是使用了一个可以直接使用的常量字符串切片。
const FILE_DATA: &str = "test";
#[tokio::main]
async fn main() {
// code omitted
let login = warp::path("login").map(move || warp::reply::html(FILE_DATA));
// code omitted
}
For a given static ref NAME: TYPE = EXPR;
, the macro generates a unique type that implements Deref<TYPE>
and stores it in a static with name NAME
. (Attributes end up attaching to this type.)
即变量的类型是不是TYPE
!
这就是您看到错误的原因
the trait `std::convert::From<file_data>` is not implemented for `hyper::body::body::Body`
^^^^^^^^^
你可能更幸运地显式取消引用或克隆全局变量:warp::reply::html(&*file_data)
或 warp::reply::html(file_data.clone())
.
lazy_static::lazy_static! {
static ref file_data: String = fs::read_to_string("static/login.html").expect("unable to read from static/login.html");
}
#[tokio::main]
async fn main() {
// code omitted
let login = warp::path("login").map(move || warp::reply::html(file_data));
// code omitted
}
编译错误:
error[E0277]: the trait bound `hyper::body::body::Body: std::convert::From<file_data>` is not satisfied
--> src/main.rs:45:49
|
45 | let login = warp::path("login").map(move || warp::reply::html(file_data));
| ^^^^^^^^^^^^^^^^^ the trait `std::convert::From<file_data>` is not implemented for `hyper::body::body::Body`
|
::: /home/ichi/.cargo/registry/src/github.com-1ecc6299db9ec823/warp-0.2.3/src/reply.rs:170:11
|
170 | Body: From<T>,
| ------- required by this bound in `warp::reply::html`
|
= help: the following implementations were found:
<hyper::body::body::Body as std::convert::From<&'static [u8]>>
<hyper::body::body::Body as std::convert::From<&'static str>>
<hyper::body::body::Body as std::convert::From<bytes::bytes::Bytes>>
<hyper::body::body::Body as std::convert::From<std::borrow::Cow<'static, [u8]>>>
and 4 others
我正在尝试将 String
传递给闭包。根据 documentation,From<String>
是为 hyper::body::Body
实现的,而 file_data
的类型是 String
。那么为什么我会收到此错误?
这里的问题是 lazy_static
创建了一个引用您的数据的包装器类型,而 hyper
不知道如何处理它。您可以使用 file_data.clone()
克隆引用的数据并从中构造一个主体,但在这种情况下实际上有一个更简单的方法。由于您的字符串具有固定值并且 Body
实现了 From<&'static str>
,因此您实际上根本不需要堆分配的 String
或 lazy_static
:您可以使用以下代码它只是使用了一个可以直接使用的常量字符串切片。
const FILE_DATA: &str = "test";
#[tokio::main]
async fn main() {
// code omitted
let login = warp::path("login").map(move || warp::reply::html(FILE_DATA));
// code omitted
}
For a given
static ref NAME: TYPE = EXPR;
, the macro generates a unique type that implementsDeref<TYPE>
and stores it in a static with nameNAME
. (Attributes end up attaching to this type.)
即变量的类型是不是TYPE
!
这就是您看到错误的原因
the trait `std::convert::From<file_data>` is not implemented for `hyper::body::body::Body`
^^^^^^^^^
你可能更幸运地显式取消引用或克隆全局变量:warp::reply::html(&*file_data)
或 warp::reply::html(file_data.clone())
.