ruma_common/serde/buf.rs
1use bytes::BufMut;
2use serde::Serialize;
3
4/// Converts a byte slice to a buffer by copying.
5pub fn slice_to_buf<B: Default + BufMut>(s: &[u8]) -> B {
6 let mut buf = B::default();
7 buf.put_slice(s);
8 buf
9}
10
11/// Creates a buffer and writes a serializable value to it.
12pub fn json_to_buf<B: Default + BufMut, T: Serialize>(val: &T) -> serde_json::Result<B> {
13 let mut buf = B::default().writer();
14 serde_json::to_writer(&mut buf, val)?;
15 Ok(buf.into_inner())
16}