json_streaming/nonblocking/
io.rs1use async_trait::async_trait;
2use core::error::Error;
3
4#[async_trait]
15pub trait NonBlockingWrite {
16 type Error: Error;
17
18 async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>;
19}
20
21#[cfg(all(test, not(feature="tokio")))]
22#[async_trait]
23impl NonBlockingWrite for Vec<u8> {
24 type Error = std::io::Error;
25
26 async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
27 self.extend_from_slice(buf);
28 Ok(())
29 }
30}
31
32#[cfg(feature = "tokio")]
33#[async_trait]
36impl <W: tokio::io::AsyncWrite + Unpin + Send> NonBlockingWrite for W {
37 type Error = std::io::Error;
38
39 async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
40 tokio::io::AsyncWriteExt::write_all(self, buf).await
41 }
42}
43
44
45#[async_trait]
46pub trait NonBlockingRead {
57 type Error: Error;
58
59 async fn read(&mut self) -> Result<Option<u8>, Self::Error>;
60}
61
62#[cfg(all(test, not(feature = "tokio")))]
63#[async_trait]
64impl NonBlockingRead for std::io::Cursor<Vec<u8>> {
65 type Error = std::io::Error;
66
67 async fn read(&mut self) -> Result<Option<u8>, Self::Error> {
68 let mut result = [0u8; 1];
69 let num_read = std::io::Read::read(self, &mut result)?;
70 if num_read == 1 {
71 Ok(Some(result[0]))
72 }
73 else {
74 Ok(None)
75 }
76 }
77}
78
79#[cfg(feature = "tokio")]
80#[async_trait]
83impl <R: tokio::io::AsyncRead + Unpin + Send> NonBlockingRead for R {
84 type Error = std::io::Error;
85
86 async fn read(&mut self) -> Result<Option<u8>, Self::Error> {
87 let mut result = [0u8; 1];
88 let num_read = tokio::io::AsyncReadExt::read(self, &mut result).await?;
89 if num_read == 1 {
90 Ok(Some(result[0]))
91 }
92 else {
93 Ok(None)
94 }
95 }
96}