tokio_proto/streaming/multiplex/
frame.rs

1use super::RequestId;
2
3/// A multiplexed protocol frame
4#[derive(Debug, Clone)]
5pub enum Frame<T, B, E> {
6    /// Either a request or a response.
7    Message {
8        /// Message exchange identifier
9        id: RequestId,
10        /// The message value
11        message: T,
12        /// Set to true when body frames will follow with the same request ID.
13        body: bool,
14        /// Set to `true` when this message does not have a pair in the other
15        /// direction
16        solo: bool,
17    },
18    /// Body frame.
19    Body {
20        /// Message exchange identifier
21        id: RequestId,
22        /// Body chunk. Setting to `None` indicates that the body is done
23        /// streaming and there will be no further body frames sent with the
24        /// given request ID.
25        chunk: Option<B>,
26    },
27    /// Error
28    Error {
29        /// Message exchange identifier
30        id: RequestId,
31        /// Error value
32        error: E,
33    },
34}
35
36impl<T, B, E> Frame<T, B, E> {
37    /// Return the request ID associated with the frame.
38    pub fn request_id(&self) -> RequestId {
39        match *self {
40            Frame::Message { id, .. } => id,
41            Frame::Body { id, .. } => id,
42            Frame::Error { id, .. } => id,
43        }
44    }
45
46    /// Unwraps a frame, yielding the content of the `Message`.
47    pub fn unwrap_msg(self) -> T {
48        match self {
49            Frame::Message { message, .. } => message,
50            Frame::Body { .. } => panic!("called `Frame::unwrap_msg()` on a `Body` value"),
51            Frame::Error { .. } => panic!("called `Frame::unwrap_msg()` on an `Error` value"),
52        }
53    }
54
55    /// Unwraps a frame, yielding the content of the `Body`.
56    pub fn unwrap_body(self) -> Option<B> {
57        match self {
58            Frame::Body { chunk, .. } => chunk,
59            Frame::Message { .. } => panic!("called `Frame::unwrap_body()` on a `Message` value"),
60            Frame::Error { .. } => panic!("called `Frame::unwrap_body()` on an `Error` value"),
61        }
62    }
63
64    /// Unwraps a frame, yielding the content of the `Error`.
65    pub fn unwrap_err(self) -> E {
66        match self {
67            Frame::Error { error, .. } => error,
68            Frame::Body { .. } => panic!("called `Frame::unwrap_err()` on a `Body` value"),
69            Frame::Message { .. } => panic!("called `Frame::unwrap_err()` on a `Message` value"),
70        }
71    }
72}