1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use futures::stream;
use futures::stream::Stream;

use metadata::Metadata;

use futures_grpc::GrpcStreamSend;
use error::Error;

#[derive(Debug, Default)]
pub struct RequestOptions {
    pub metadata: Metadata,
}

impl RequestOptions {
    pub fn new() -> RequestOptions {
        Default::default()
    }
}

/// Excluding initial metadata which is passed separately
pub struct StreamingRequest<T : Send + 'static>(pub GrpcStreamSend<T>);

impl<T : Send + 'static> StreamingRequest<T> {

    // constructors

    pub fn new<S>(stream: S) -> StreamingRequest<T>
        where S : Stream<Item=T, Error=Error> + Send + 'static
    {
        StreamingRequest(Box::new(stream))
    }

    pub fn once(item: T) -> StreamingRequest<T> {
        StreamingRequest::new(stream::once(Ok(item)))
    }

    pub fn iter<I>(iter: I) -> StreamingRequest<T>
        where
            I : IntoIterator<Item=T>,
            I::IntoIter : Send + 'static,
    {
        StreamingRequest::new(stream::iter(iter.into_iter().map(Ok)))
    }

    pub fn single(item: T) -> StreamingRequest<T> {
        StreamingRequest::new(stream::once(Ok(item)))
    }

    pub fn empty() -> StreamingRequest<T> {
        StreamingRequest::new(stream::empty())
    }

    pub fn err(err: Error) -> StreamingRequest<T> {
        StreamingRequest::new(stream::once(Err(err)))
    }
}