2017-04-12 79 views
1

我遇到了一个编译错误,我不太了解从一个Hyper的master分支的例子的轻微修改。考虑下面的代码:编译错误与Hyper

extern crate futures; 
extern crate hyper; 

use futures::future::FutureResult; 
use hyper::header::{ContentLength, ContentType}; 
use hyper::server::{Http, Service, Request, Response, Server, NewService}; 
use hyper::Body; 
use std::fmt::Display; 
use std::result; 

static PHRASE: &'static [u8] = b"Hello World!"; 

#[derive(Clone, Copy)] 
pub struct MyService {} 

impl Service for MyService { 
    type Request = Request; 
    type Response = Response; 
    type Error = hyper::Error; 
    type Future = FutureResult<Response, hyper::Error>; 
    fn call(&self, _req: Request) -> Self::Future { 
     return futures::future::ok(Response::new() 
      .with_header(ContentLength(PHRASE.len() as u64)) 
      .with_header(ContentType::plaintext()) 
      .with_body(PHRASE)); 
    } 
} 

#[derive(Clone)] 
pub struct MyServer {} 

#[derive(Debug)] 
pub struct MyServeError(String); 
impl<T: Display> From<T> for MyServeError { 
    fn from(e: T) -> MyServeError { 
     return MyServeError(format!("{}", e)); 
    } 
} 

type Result<T> = result::Result<T, MyServeError>; 


impl MyServer { 
    pub fn new() -> MyServer { 
     return MyServer {}; 
    } 

    fn get_server(&self) -> Result<Server<&MyServer, Body>> { 
     let addr = format!("127.0.0.1:8080").parse()?; 
     return Ok(Http::new().bind(&addr, self)?); 
    } 
} 

impl NewService for MyServer { 
    type Request = Request; 
    type Response = Response; 
    type Instance = MyService; 
    type Error = hyper::Error; 

    fn new_service(&self) -> std::io::Result<Self::Instance> { 
     let service = MyService {}; 
     Ok(service) 
    } 
} 

我得到这个编译错误:

Compiling hyper-problem-demo v0.1.0 (file:///.../hyper-problem-demo) 
error[E0277]: the trait bound `MyServer: std::ops::Fn<()>` is not satisfied 
    --> src/lib.rs:50:31 
    | 
50 |   return Ok(Http::new().bind(&addr, self)?); 
    |        ^^^^ the trait `std::ops::Fn<()>` is not implemented for `MyServer` 
    | 
    = note: required because of the requirements on the impl of `std::ops::FnOnce<()>` for `&MyServer` 
    = note: required because of the requirements on the impl of `hyper::server::NewService` for `&MyServer` 

error[E0277]: the trait bound `MyServer: std::ops::FnOnce<()>` is not satisfied 
    --> src/lib.rs:50:31 
    | 
50 |   return Ok(Http::new().bind(&addr, self)?); 
    |        ^^^^ the trait `std::ops::FnOnce<()>` is not implemented for `MyServer` 
    | 
    = note: required because of the requirements on the impl of `hyper::server::NewService` for `&MyServer` 

对此我真的不明白。我的意图只是使用MyServer对象创建超实用的MyService的新实例,因此实现NewService似乎有意义,但我不明白为什么这需要实施Fn()NewService实际上是为Fn() -> io::Result<Service实施的,所以也许这是冲突莫名其妙?

有一个完整的示例项目here

+0

基本相同,http://stackoverflow.com/q/40922505/ 155423 – Shepmaster

回答

1

您已经为MyServer实施了NewService,但是您提供bind a &MyServer,它无法找到NewService的实施。

你去很大程度上取决于你为什么要做到这一点,将取决于,但你可以实现的解决方案NewService&MyServer

impl<'a> NewService for &'a MyServer { 
    ... 
}