Skip to content

Commit

Permalink
Use Box<[u8]> instead of Vec<u8> in Serialized
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Oct 21, 2024
1 parent e52153d commit 135c7fd
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions crates/hyperqueue/src/common/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl SerializationConfig for TrailingAllowedConfig {
#[derive(Serialize, Deserialize)]
pub struct Serialized<T, C = DefaultConfig> {
#[serde(with = "serde_bytes")]
data: Vec<u8>,
data: Box<[u8]>,
_phantom: PhantomData<(T, C)>,
}

Expand All @@ -55,8 +55,11 @@ impl<T, C> Clone for Serialized<T, C> {

impl<T: Serialize + DeserializeOwned, C: SerializationConfig> Serialized<T, C> {
pub fn new(value: &T) -> bincode::Result<Self> {
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(),
})
}
Expand Down

0 comments on commit 135c7fd

Please sign in to comment.