Search code examples
rustownershiphyper

How can I return an Arc<Vec<u8>> as a Hyper response?


I am writing a server that allocates some compressed data on startup. Now when I serve a hyper response I do not want to copy these bytes, but I cannot figure out a way to do this with hyper.

I have tried implementing HttpBody for my own type, but the lifetime restriction on the trait blocks me from doing this.

Am I missing something? Here is a minimal example of what I am trying to do:

use hyper::{service::Service, Body, Request, Response, Server};
use std::net::SocketAddr;
use std::sync::Arc;

use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

fn main() {
    let addr = SocketAddr::new("0.0.0.0".parse().unwrap(), 8080);
    println!("Server startup...");
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async {
        let app = MakeSvc::new().await;
        let ret = app.clone();
        let server = Server::bind(&addr).serve(app);

        println!("Running on {}", &addr);
        server.await.unwrap();
    })
}

#[derive(Debug, Clone)]
pub struct WrapperApp {
    pub cache: Arc<Vec<u8>>,
}

impl WrapperApp {
    //Let's say I allocate some bytes here.
    async fn new() -> Self {
        Self {
            cache: Arc::new(Vec::new()),
        }
    }
}

impl Service<Request<Body>> for WrapperApp {
    type Response = Response<Body>;
    type Error = hyper::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: Request<Body>) -> Self::Future {
        let a = Arc::clone(&self.cache);
        return Box::pin(async { Ok(Response::builder().body(Body::from(a)).unwrap()) });
    }
}

#[derive(Debug, Clone)]
pub struct MakeSvc {
    app: WrapperApp,
}

impl MakeSvc {
    pub async fn new() -> Self {
        Self {
            app: WrapperApp::new().await,
        }
    }
}

impl<T> Service<T> for MakeSvc {
    type Response = WrapperApp;
    type Error = hyper::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, _: T) -> Self::Future {
        let app = self.app.clone();
        let fut = async move { Ok(app) };
        Box::pin(fut)
    }
}
error[E0277]: the trait bound `Body: From<Arc<Vec<u8>>>` is not satisfied
  --> src/main.rs:50:61
   |
50 |         return Box::pin(async { Ok(Response::builder().body(Body::from(a)).unwrap()) });
   |                                                             ^^^^^^^^^^ the trait `From<Arc<Vec<u8>>>` is not implemented for `Body`
   |
   = help: the following implementations were found:
             <Body as From<&'static [u8]>>
             <Body as From<&'static str>>
             <Body as From<Box<(dyn futures_core::stream::Stream<Item = std::result::Result<hyper::body::Bytes, Box<(dyn std::error::Error + Send + Sync + 'static)>>> + Send + 'static)>>>
             <Body as From<Cow<'static, [u8]>>>
           and 4 others
   = note: required by `from`

The Cargo.toml that goes with this. The example breaks at the place where I am trying to use the ref:

[package]
name = "hyper-ptr"
version = "0.1.0"
authors = ["Pierre Laas <lanklaas123@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hyper={version="0.14", features=["server","http1","tcp"]}
tokio={version="1.0.0", features=["macros","io-util","rt-multi-thread"]}
serde={version="*", features=["derive","rc"]}
serde_json="*"
flate2="*"
openssl="*"
rand="*"

Solution

  • From the documentation, there is no From implementation that doesn't require either 'static references or ownership of the body.

    Instead, you can clone the cache. Body::From's implementation requires ownership of the Vec<u8> or static data (if that is useful for your case).

    Notice that you need to dereference it first:

    use hyper::Body; // 0.14.2
    use std::sync::Arc;
    
    const FOO: [u8; 1] = [8u8]; 
    
    fn main() {
        let cache = Arc::new(vec![0u8]);
        let body = Body::from((*cache).clone());
        let other_body = Body::from(&FOO[..]);
        println!("{:?}", cache);
    }
    

    Playground