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
/// The body of a stream message.
#[derive(Debug, Clone)]
pub struct StreamBody {
	/// The message data.
	pub data: Vec<u8>,
}

impl StreamBody {
	/// Create a new stream body.
	fn new(data: Vec<u8>) -> Self {
		Self { data }
	}
}

impl crate::Body for StreamBody {
	fn empty() -> Self {
		Self::new(Vec::new())
	}

	fn from_error(message: &str) -> Self {
		Self::new(message.as_bytes().into())
	}

	fn as_error(&self) -> Result<&str, std::str::Utf8Error> {
		std::str::from_utf8(&self.data)
	}

	fn into_error(self) -> Result<String, std::string::FromUtf8Error> {
		String::from_utf8(self.data)
	}
}

impl<T> From<T> for StreamBody
where
	Vec<u8>: From<T>,
{
	fn from(other: T) -> Self {
		Self { data: other.into() }
	}
}

impl AsRef<[u8]> for StreamBody {
	fn as_ref(&self) -> &[u8] {
		&self.data
	}
}

impl std::ops::Deref for StreamBody {
	type Target = [u8];

	fn deref(&self) -> &[u8] {
		&self.data
	}
}