Skip to content

Conversation

@andrew-manifold
Copy link

@andrew-manifold andrew-manifold commented Oct 14, 2025

A much belated follow up to address the issues from this PR. If this is accepted then I'll close out the older PR. I was never able to figure out the underlying issue in unsafe land so opted to eventually go a safer but still quite flexible approach. Instead of having a hard requirement to use bincode explicitly under the hood, serialization & deserialization are now managed via ShmDeserializer, ShmZeroCopyDeserializer and ShmSerializer traits. The api is admittedly a bit more verbose then the original but the benefit is that now users can customize the ser backend while keeping the ipmpsc api untouched. For example, below is the trait impl for speedy

use speedy::{LittleEndian, Readable, Writable};
use super::*;

#[derive(Debug)]
pub struct SpeedyZeroCopyDeserializer<T>(pub T);

impl<'de, T> ShmZeroCopyDeserializer<'de> for SpeedyZeroCopyDeserializer<T>
where
    T: Readable<'de, LittleEndian>,
{
    type Error = speedy::Error;

    fn deserialize_from_bytes(bytes: &'de [u8]) -> std::result::Result<Self, Self::Error> {
        Ok(Self(T::read_from_buffer(bytes)?))
    }
}

#[derive(Debug)]
pub struct SpeedyDeserializer<T>(pub T);

impl<T> ShmDeserializer for SpeedyDeserializer<T>
where T: for<'de> Readable<'de, LittleEndian>
{
    type Error = speedy::Error;

    fn deserialize_from_bytes<'de>(bytes: &'de [u8]) -> std::result::Result<Self, Self::Error> {
        Ok(Self(T::read_from_buffer(bytes)?))
    }
}

#[derive(Debug)]
pub struct SpeedySerializer<T: Writable<LittleEndian>>(pub T);

impl<T: Writable<LittleEndian>> ShmSerializer for SpeedySerializer<T> {
    type Error = speedy::Error;

    fn serialize(&self) -> std::result::Result<Vec<u8>, Self::Error> {
        Ok(self.0.write_to_vec()?)
    }
}

unit tests pass as do the benchmarking tests in ipc-benchmarks. Additionally included is a default set of bincode wrapper traits to work with existing primitives (String, Vec etc) but the wrapper can be skipped for net new structs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant