1pub mod blocking_impl {
5 use crate::{
6 base::Header,
7 element::Element,
8 functional::Encode,
9 master::{Cluster, Segment},
10 };
11 use std::io::{Read, Write};
12
13 pub trait ReadFrom: Sized {
15 fn read_from<R: Read>(r: &mut R) -> crate::Result<Self>;
17 }
18
19 pub trait ReadElement: Sized + Element {
21 fn read_element<R: Read>(header: &Header, r: &mut R) -> crate::Result<Self> {
23 let body = header.read_body(r)?;
24 Self::decode_body(&mut &body[..])
25 }
26 }
27 impl<T: Element> ReadElement for T {}
28
29 impl Header {
30 pub(crate) fn read_body<R: Read>(&self, r: &mut R) -> crate::Result<Vec<u8>> {
32 let size = if self.size.is_unknown && [Segment::ID, Cluster::ID].contains(&self.id) {
34 return Err(crate::Error::ElementBodySizeUnknown(self.id));
35 } else {
36 *self.size
37 };
38 let cap = size.min(4096) as usize;
40 let mut buf = Vec::with_capacity(cap);
41 let n = std::io::copy(&mut r.take(size), &mut buf)?;
42 if size != n {
43 return Err(crate::Error::OutOfBounds);
44 }
45 Ok(buf)
46 }
47 }
48
49 pub trait WriteTo {
51 fn write_to<W: Write>(&self, w: &mut W) -> crate::Result<()>;
53 }
54
55 impl<T: Encode> WriteTo for T {
56 fn write_to<W: Write>(&self, w: &mut W) -> crate::Result<()> {
57 let mut buf = vec![];
59 self.encode(&mut buf)?;
60 w.write_all(&buf)?;
61 Ok(())
62 }
63 }
64
65 pub trait WriteElement: Sized + Element {
67 fn write_element<W: Write>(&self, header: &Header, w: &mut W) -> crate::Result<()> {
69 header.write_to(w)?;
70 let mut buf = vec![];
71 self.encode_body(&mut buf)?;
72 w.write_all(&buf)?;
73 Ok(())
74 }
75 }
76 impl<T: Element> WriteElement for T {}
77}
78#[cfg(feature = "tokio")]
80#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
81pub mod tokio_impl {
82 use crate::{
83 base::Header,
84 element::Element,
85 master::{Cluster, Segment},
86 };
87
88 use std::future::Future;
89 use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt};
90
91 pub trait AsyncReadFrom: Sized {
93 fn async_read_from<R: tokio::io::AsyncRead + Unpin>(
95 r: &mut R,
96 ) -> impl Future<Output = crate::Result<Self>>;
97 }
98
99 pub trait AsyncReadElement: Sized + Element {
101 fn async_read_element<R: tokio::io::AsyncRead + Unpin>(
103 header: &Header,
104 r: &mut R,
105 ) -> impl std::future::Future<Output = crate::Result<Self>> {
106 async {
107 let body = header.read_body_tokio(r).await?;
108 Self::decode_body(&mut &body[..])
109 }
110 }
111 }
112 impl<T: Element> AsyncReadElement for T {}
113
114 pub trait AsyncWriteTo {
116 fn async_write_to<W: tokio::io::AsyncWrite + Unpin>(
118 &self,
119 w: &mut W,
120 ) -> impl std::future::Future<Output = crate::Result<()>>;
121 }
122
123 impl<T: crate::functional::Encode> AsyncWriteTo for T {
124 async fn async_write_to<W: tokio::io::AsyncWrite + Unpin>(
125 &self,
126 w: &mut W,
127 ) -> crate::Result<()> {
128 let mut buf = vec![];
130 self.encode(&mut buf)?;
131 Ok(w.write_all(&buf).await?)
132 }
133 }
134
135 pub trait AsyncWriteElement: Sized + Element {
137 fn async_write_element<W: tokio::io::AsyncWrite + Unpin>(
139 &self,
140 header: &Header,
141 w: &mut W,
142 ) -> impl std::future::Future<Output = crate::Result<()>> {
143 async {
144 header.async_write_to(w).await?;
145 let mut buf = vec![];
146 self.encode_body(&mut buf)?;
147 Ok(w.write_all(&buf).await?)
148 }
149 }
150 }
151 impl<T: Element> AsyncWriteElement for T {}
152
153 impl Header {
154 pub(crate) async fn read_body_tokio<R: AsyncRead + Unpin>(
156 &self,
157 r: &mut R,
158 ) -> crate::Result<Vec<u8>> {
159 let size = if self.size.is_unknown && [Segment::ID, Cluster::ID].contains(&self.id) {
161 return Err(crate::Error::ElementBodySizeUnknown(self.id));
162 } else {
163 *self.size
164 };
165 let cap = size.min(4096) as usize;
167 let mut buf = Vec::with_capacity(cap);
168 let n = tokio::io::copy(&mut r.take(size), &mut buf).await?;
169 if size != n {
170 return Err(crate::Error::OutOfBounds);
171 }
172 Ok(buf)
173 }
174 }
175}