0

我有一个rusoto_core::ByteStream实现期货Stream特征的方法:

let chunks = vec![b"1234".to_vec(), b"5678".to_vec()];
let stream = ByteStream::new(stream::iter_ok(chunks));

我想将它传递给actix_web 的HttpResponseBuilder::streaming方法。

use actix_web::dev::HttpResponseBuilder; // 0.7.18
use rusoto_core::ByteStream; // 0.36.0

fn example(stream: ByteStream, builder: HttpResponseBuilder) {
    builder.streaming(stream);
}

当我尝试这样做时,我收到以下错误:

error[E0271]: type mismatch resolving `<rusoto_core::stream::ByteStream as futures::stream::Stream>::Item == bytes::bytes::Bytes`
 --> src/main.rs:5:13
  |
5 |     builder.streaming(stream);
  |             ^^^^^^^^^ expected struct `std::vec::Vec`, found struct `bytes::bytes::Bytes`
  |
  = note: expected type `std::vec::Vec<u8>`
             found type `bytes::bytes::Bytes`

我相信原因是streaming()期望 a S: Stream<Item = Bytes, Error>(ie, Item = Bytes) 但我ByteStreamItem = Vec<u8>. 我该如何解决?

我认为解决方案以某种方式解决了flatmap我的问题,ByteStream但我找不到这样的流方法。

这是一个如何使用的示例streaming()

let text = "123";
let (tx, rx_body) = mpsc::unbounded();
let _ = tx.unbounded_send(Bytes::from(text.as_bytes()));

HttpResponse::Ok()
    .streaming(rx_body.map_err(|e| error::ErrorBadRequest("bad request")))
4

1 回答 1

6

我如何flatmap在 Rust 中流式传输?

平面映射将迭代器的迭代器转换为单个迭代器(或流而不是迭代器)。

期货 0.3

Futures 0.3 没有直接的平面图,但是有StreamExt::flatten,可以在StreamExt::map.

use futures::{stream, Stream, StreamExt}; // 0.3.1

fn into_many(i: i32) -> impl Stream<Item = i32> {
    stream::iter(0..i)
}

fn nested() -> impl Stream<Item = i32> {
    let stream_of_number = into_many(5);
    let stream_of_stream_of_number = stream_of_number.map(into_many);
    let flat_stream_of_number = stream_of_stream_of_number.flatten();

    // Returns: 0, 0, 1, 0, 1, 2, 0, 1, 2, 3
    flat_stream_of_number
}

期货 0.1

Futures 0.1 没有直接的平面图,但是有Stream::flatten,可以在Stream::map.

use futures::{stream, Stream}; // 0.1.25

fn into_many(i: i32) -> impl Stream<Item = i32, Error = ()> {
    stream::iter_ok(0..i)
}

fn nested() -> impl Stream<Item = i32, Error = ()> {
    let stream_of_number = into_many(5);
    let stream_of_stream_of_number = stream_of_number.map(into_many);
    let flat_stream_of_number = stream_of_stream_of_number.flatten();

    // Returns: 0, 0, 1, 0, 1, 2, 0, 1, 2, 3
    flat_stream_of_number
}

但是,这并不能解决您的问题

streaming()期望 a S: Stream<Item = Bytes, Error>(ie, Item = Bytes) 但我ByteStreamItem = Vec<u8>

是的,这就是问题所在。使用Bytes::fromviaStream::map将您的流Item从一种类型转换为另一种类型:

use bytes::Bytes; // 0.4.11
use futures::Stream; // 0.1.25 

fn example(stream: ByteStream, mut builder: HttpResponseBuilder) {
    builder.streaming(stream.map(Bytes::from));
}
于 2019-01-30T14:47:48.987 回答