mirror of
https://github.com/neondatabase/neon.git
synced 2026-05-27 01:50:38 +00:00
bin_ser: expand serialize error type, add serialized_size
This commit is contained in:
committed by
Eric Seppanen
parent
858ca3a4ce
commit
1ec157653e
@@ -46,8 +46,23 @@ impl From<bincode::Error> for DeserializeError {
|
||||
/// This probably means our [`Write`] failed, e.g. we tried
|
||||
/// to write beyond the end of a buffer.
|
||||
#[derive(Debug, Error)]
|
||||
#[error("serialize error")]
|
||||
pub struct SerializeError;
|
||||
pub enum SerializeError {
|
||||
/// The serializer isn't able to serialize the supplied data.
|
||||
#[error("serialize error")]
|
||||
BadInput,
|
||||
/// While serializing into a `Write` sink, an `io::Error` occurred.
|
||||
#[error("serialize error: {0}")]
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
impl From<bincode::Error> for SerializeError {
|
||||
fn from(e: bincode::Error) -> Self {
|
||||
match *e {
|
||||
bincode::ErrorKind::Io(io_err) => SerializeError::Io(io_err),
|
||||
_ => SerializeError::BadInput,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A shortcut that configures big-endian binary serialization
|
||||
///
|
||||
@@ -95,12 +110,12 @@ pub trait BeSer: Serialize + DeserializeOwned {
|
||||
/// This is useful for most `Write` types except `&mut [u8]`, which
|
||||
/// can more easily use [`ser_into_slice`](Self::ser_into_slice).
|
||||
fn ser_into<W: Write>(&self, w: &mut W) -> Result<(), SerializeError> {
|
||||
be_coder().serialize_into(w, &self).or(Err(SerializeError))
|
||||
be_coder().serialize_into(w, &self).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
/// Serialize into a new heap-allocated buffer
|
||||
fn ser(&self) -> Result<Vec<u8>, SerializeError> {
|
||||
be_coder().serialize(&self).or(Err(SerializeError))
|
||||
be_coder().serialize(&self).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
/// Deserialize from a byte slice
|
||||
@@ -114,6 +129,14 @@ pub trait BeSer: Serialize + DeserializeOwned {
|
||||
fn des_from<R: Read>(r: &mut R) -> Result<Self, DeserializeError> {
|
||||
be_coder().deserialize_from(r).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
/// Compute the serialized size of a data structure
|
||||
///
|
||||
/// Note: it may be faster to serialize to a buffer and then measure the
|
||||
/// buffer length, than to call `serialized_size` and then `ser_into`.
|
||||
fn serialized_size(&self) -> Result<u64, SerializeError> {
|
||||
be_coder().serialized_size(self).map_err(|e| e.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Binary serialize/deserialize helper functions (Big Endian)
|
||||
@@ -132,12 +155,12 @@ pub trait LeSer: Serialize + DeserializeOwned {
|
||||
/// This is useful for most `Write` types except `&mut [u8]`, which
|
||||
/// can more easily use [`ser_into_slice`](Self::ser_into_slice).
|
||||
fn ser_into<W: Write>(&self, w: &mut W) -> Result<(), SerializeError> {
|
||||
le_coder().serialize_into(w, &self).or(Err(SerializeError))
|
||||
le_coder().serialize_into(w, &self).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
/// Serialize into a new heap-allocated buffer
|
||||
fn ser(&self) -> Result<Vec<u8>, SerializeError> {
|
||||
le_coder().serialize(&self).or(Err(SerializeError))
|
||||
le_coder().serialize(&self).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
/// Deserialize from a byte slice
|
||||
@@ -151,6 +174,14 @@ pub trait LeSer: Serialize + DeserializeOwned {
|
||||
fn des_from<R: Read>(r: &mut R) -> Result<Self, DeserializeError> {
|
||||
le_coder().deserialize_from(r).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
/// Compute the serialized size of a data structure
|
||||
///
|
||||
/// Note: it may be faster to serialize to a buffer and then measure the
|
||||
/// buffer length, than to call `serialized_size` and then `ser_into`.
|
||||
fn serialized_size(&self) -> Result<u64, SerializeError> {
|
||||
le_coder().serialized_size(self).map_err(|e| e.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> BeSer for T where T: Serialize + DeserializeOwned {}
|
||||
@@ -207,6 +238,8 @@ mod tests {
|
||||
fn be_short() {
|
||||
use super::BeSer;
|
||||
|
||||
assert_eq!(SHORT1.serialized_size().unwrap(), 5);
|
||||
|
||||
let encoded = SHORT1.ser().unwrap();
|
||||
assert_eq!(encoded, SHORT1_ENC_BE);
|
||||
|
||||
@@ -237,6 +270,8 @@ mod tests {
|
||||
fn le_short() {
|
||||
use super::LeSer;
|
||||
|
||||
assert_eq!(SHORT1.serialized_size().unwrap(), 5);
|
||||
|
||||
let encoded = SHORT1.ser().unwrap();
|
||||
assert_eq!(encoded, SHORT1_ENC_LE);
|
||||
|
||||
@@ -267,6 +302,8 @@ mod tests {
|
||||
fn be_long() {
|
||||
use super::BeSer;
|
||||
|
||||
assert_eq!(LONG1.serialized_size().unwrap(), 30);
|
||||
|
||||
let msg = LONG1;
|
||||
|
||||
let encoded = msg.ser().unwrap();
|
||||
@@ -283,6 +320,8 @@ mod tests {
|
||||
fn le_long() {
|
||||
use super::LeSer;
|
||||
|
||||
assert_eq!(LONG1.serialized_size().unwrap(), 30);
|
||||
|
||||
let msg = LONG1;
|
||||
|
||||
let encoded = msg.ser().unwrap();
|
||||
|
||||
@@ -37,6 +37,5 @@ fn test1() {
|
||||
let dec1 = decode_header_data(&mut buf1);
|
||||
let dec2 = decode2(&mut buf2);
|
||||
assert_eq!(dec1, dec2);
|
||||
eprintln!("{} {}", buf1.len(), buf2.len());
|
||||
assert_eq!(buf1, buf2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user