rustactix-web

HTTP response body from `Arc<[u8]>` in Actix-web?


I'm writing a web server using Actix-web. I have a cache component that stores some data as Arc<[u8]>, now I'd like to send these directly without making an unnecessary copy.

I didn't find any direct way to do this, so I've tried to figure out how to implement the MessageBody trait for my wrapper type, but I'm getting stuck on converting Arc<[u8]> to actix_web::web::Bytes. The docs actually talk about Bytes supporting Arc, but I can't find any way to actually use that.

What is the best way to serve Arc<[u8]> body in Actix-web?


Solution

  • Considering that Bytes is a reference-counted and copy-on-write container for a byte array, it's semantically very similar to Arc<[u8]> but with some extra features. I would suggest changing your cache to store Bytes values instead of Arc<[u8]> and then the problem goes away.