Reputation: 6493
I'm trying to convert the result of a reqwest file download in to a type which implements the Read
trait. Reqwest has given back a Bytes
type. Bytes does not seem to implement Read
trait but in the same library there is Buf
which does implement Read
.
I have been trying to find a way to convert Bytes
to Buf
but I have not found a way. Am I going about this the right way if I want to get something which implements Read
and is there a way to do it by converting Bytes
to Buf
?
Upvotes: 1
Views: 1396
Reputation: 42678
Notice that Bytes
already implements Buf
. But you need to have the trait in scope for using it:
fn main() {
use bytes::Bytes;
use bytes::Buf;
let mut buf = Bytes::from("Hello world");
assert_eq!(b'H', buf.get_u8());
}
With that in mind, if you need a Read
object, you can call the method reader
to get such type:
fn main() {
use bytes::Bytes;
use bytes::Buf;
use std::io::Read;
let mut buf = Bytes::from("Hello world");
let mut reader: Box<dyn Read> = Box::new(buf.reader()) as Box<dyn Read>;
}
Upvotes: 3