diff --git a/crates/hyperqueue/src/common/serialization.rs b/crates/hyperqueue/src/common/serialization.rs index dc4ae881b..afc18b5ea 100644 --- a/crates/hyperqueue/src/common/serialization.rs +++ b/crates/hyperqueue/src/common/serialization.rs @@ -29,7 +29,7 @@ impl SerializationConfig for TrailingAllowedConfig { #[derive(Serialize, Deserialize)] pub struct Serialized { #[serde(with = "serde_bytes")] - data: Vec, + data: Box<[u8]>, _phantom: PhantomData<(T, C)>, } @@ -55,8 +55,11 @@ impl Clone for Serialized { impl Serialized { pub fn new(value: &T) -> bincode::Result { + let result = C::config().serialize(value)?; + // Check that we're not reallocating needlessly in `into_boxed_slice` + debug_assert_eq!(result.capacity(), result.len()); Ok(Self { - data: C::config().serialize(value)?, + data: result.into_boxed_slice(), _phantom: Default::default(), }) }