Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/proc-macro-api/src/transport/postcard.rs')
| -rw-r--r-- | crates/proc-macro-api/src/transport/postcard.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/crates/proc-macro-api/src/transport/postcard.rs b/crates/proc-macro-api/src/transport/postcard.rs new file mode 100644 index 0000000000..75aa90e4c4 --- /dev/null +++ b/crates/proc-macro-api/src/transport/postcard.rs @@ -0,0 +1,30 @@ +//! Postcard encode and decode implementations. + +use std::io::{self, BufRead, Write}; + +use serde::{Serialize, de::DeserializeOwned}; + +pub(crate) fn read<'a, R: BufRead + ?Sized>( + inp: &mut R, + buf: &'a mut Vec<u8>, +) -> io::Result<Option<&'a mut Vec<u8>>> { + buf.clear(); + let n = inp.read_until(0, buf)?; + if n == 0 { + return Ok(None); + } + Ok(Some(buf)) +} + +pub(crate) fn write<W: Write + ?Sized>(out: &mut W, buf: &[u8]) -> io::Result<()> { + out.write_all(buf)?; + out.flush() +} + +pub(crate) fn encode<T: Serialize>(msg: &T) -> io::Result<Vec<u8>> { + postcard::to_allocvec_cobs(msg).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) +} + +pub(crate) fn decode<T: DeserializeOwned>(buf: &mut [u8]) -> io::Result<T> { + postcard::from_bytes_cobs(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) +} |