nickel-org / nickel.rs

An expressjs inspired web framework for Rust

Home Page:http://nickel-org.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

no method named `write` found for type `nickel::Response ?

plailopo opened this issue · comments

It would to stream on Response, but write method is missing

 router.get("/treep/search", middleware! { |req, mut res|
         res.write(b"Foo");
         return res.send("");
 })

the error:
no method named write found for type nickel::Response<'mw> in the current scope

The Write trait is implemented for Response<'a, D, Streaming>, but the response is not Streaming in your context. If you tell us what you're trying to accomplish, we might be able to suggest another way to do it.

Ok, this is a complete example of my code

  fn get_stream<'mw>(req: &mut Request, mut res: Response<'mw>)
           -> MiddlewareResult<'mw>  {

    stream(&mut res);
    return res.send("");
  }

fn stream<P: ?Sized>(buf: &mut P)
    where P: Write
{
    for x in 0..10 {
        let elm = "foo foo".to_string();
        buf.write( &elm.as_bytes() );
    }
}

That still doesn't tell us anything of your actual use case. Anyway, if you want to use the Write API directly on a response, it is possible:

fn hello_world<'mw>(_req: &mut Request, res: Response<'mw>) -> MiddlewareResult<'mw> {
    let mut res = res.start().ok().unwrap();
    res.write(b"Hello World");
    Ok(Action::Halt(res))
}

You'd need to add error handling code, though.

If your responses are not really long, I'd suggest you write to a memory buffer, using the std::io::Cursor trait, and the respond with that buffer.

Yeah! It work, thanks

This appears to have been resolved, closing.