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