tokio_proto/streaming/pipeline/
frame.rs

1/// A pipelined protocol frame
2#[derive(Debug, Clone)]
3pub enum Frame<T, B, E> {
4    /// Either a request or a response
5    Message {
6        /// The message value
7        message: T,
8        /// Set to true when body frames will follow
9        body: bool,
10    },
11    /// Body frame. None indicates that the body is done streaming.
12    Body {
13        /// Body chunk. Setting to `None` indicates that the body is done
14        /// streaming and there will be no further body frames sent with the
15        /// given request ID.
16        chunk: Option<B>,
17    },
18    /// Error
19    Error {
20        /// Error value
21        error: E,
22    },
23}
24
25impl<T, B, E> Frame<T, B, E> {
26    /// Unwraps a frame, yielding the content of the `Message`.
27    pub fn unwrap_msg(self) -> T {
28        match self {
29            Frame::Message { message, .. } => message,
30            Frame::Body { .. } => panic!("called `Frame::unwrap_msg()` on a `Body` value"),
31            Frame::Error { .. } => panic!("called `Frame::unwrap_msg()` on an `Error` value"),
32        }
33    }
34
35    /// Unwraps a frame, yielding the content of the `Body`.
36    pub fn unwrap_body(self) -> Option<B> {
37        match self {
38            Frame::Body { chunk } => chunk,
39            Frame::Message { .. } => panic!("called `Frame::unwrap_body()` on a `Message` value"),
40            Frame::Error { .. } => panic!("called `Frame::unwrap_body()` on an `Error` value"),
41        }
42    }
43
44    /// Unwraps a frame, yielding the content of the `Error`.
45    pub fn unwrap_err(self) -> E {
46        match self {
47            Frame::Error { error } => error,
48            Frame::Body { .. } => panic!("called `Frame::unwrap_err()` on a `Body` value"),
49            Frame::Message { .. } => panic!("called `Frame::unwrap_err()` on a `Message` value"),
50        }
51    }
52}