Skip to content

Conversation

@NobodyXu
Copy link
Collaborator

@NobodyXu NobodyXu commented Oct 26, 2025

Fixed #394
Fixed #385

This new traits:

  • remove generics so it is trait object safe and avoid duplicate mono
  • use new WriteBuffer<'_> so we can add uninitialized buffer support to it in the future easily.
trait EncodeV2 {
    fn encode(
        &mut self,
        input: &mut PartialBuffer<&[u8]>,
        output: &mut WriteBuffer<'_>,
    ) -> Result<()>;

    /// Returns whether the internal buffers are flushed
    fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;

    /// Returns whether the internal buffers are flushed and the end of the stream is written
    fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
}

trait DecodeV2 {
    /// Reinitializes this decoder ready to decode a new member/frame of data.
    fn reinit(&mut self) -> Result<()>;

    /// Returns whether the end of the stream has been read
    fn decode(
        &mut self,
        input: &mut PartialBuffer<&[u8]>,
        output: &mut WriteBuffer<'_>,
    ) -> Result<bool>;

    /// Returns whether the internal buffers are flushed
    fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;

    /// Returns whether the internal buffers are flushed
    fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
}

WriteBuffer is a dedicated buffer management utilies, it is designed to support uninitialized operation in a future PR.

Its layout looks like:

|                                       buffer                                    |
| written and initialized | unwritten but initialized | unwritten and uninitialized
#[derive(Debug)]
pub struct WriteBuffer<'a> {
    buffer: &'a mut [u8],
    index: usize,
}

impl<'a> WriteBuffer<'a> {
    pub fn new_initialized(buffer: &'a mut [u8]) -> Self {
        Self { buffer, index: 0 }
    }

    pub fn written(&self) -> &[u8] {
        &self.buffer[..self.index]
    }

    /// Convenient method for `.writen().len()`
    pub fn written_len(&self) -> usize {
        self.index
    }

    /// Buffer has no spare space to write any data
    fn has_no_spare_space(&self) -> bool;

    /// Initialize all uninitialized, unwritten part to initialized, unwritten part
    fn initialize_unwritten(&mut self) {}

    /// Return initialized but unwritten part.
    fn unwritten_initialized_mut(&mut self) -> &mut [u8];

    /// Advance written index within initialized part.
    ///
    /// Note that try to advance into uninitialized part would panic.
    fn advance(&mut self, amount: usize);

    fn reset(&mut self);

    fn copy_unwritten_from<C: AsRef<[u8]>>(&mut self, other: &mut PartialBuffer<C>) -> usize;
}

This new traits:
 - remove generics so it is trait object safe and avoid duplicate mono
 - use new `WriteBuffer<'_>` so we can add uninitialized buffer support
   to it in the future easily.
@NobodyXu NobodyXu force-pushed the refactor/dyn-safe-de-encoder branch from 0d1731d to b9f8e7b Compare October 26, 2025 10:35
@NobodyXu NobodyXu requested review from Nemo157 and robjtede October 26, 2025 10:35
@NobodyXu
Copy link
Collaborator Author

cc @robjtede @Nemo157 I'd like to merge and release this, if you can take a look at the API please it'd be great please!

Corrected code block formatting in documentation.

Signed-off-by: Jiahao XU <[email protected]>
@NobodyXu
Copy link
Collaborator Author

If there's no objection, I'd like to merge this within this weekend.

@NobodyXu
Copy link
Collaborator Author

NobodyXu commented Nov 2, 2025

I'd merge this in today if no objections

@NobodyXu NobodyXu added this pull request to the merge queue Nov 2, 2025
Merged via the queue into main with commit 4ccc5b3 Nov 2, 2025
20 checks passed
@NobodyXu NobodyXu deleted the refactor/dyn-safe-de-encoder branch November 2, 2025 13:44
@codecov
Copy link

codecov bot commented Nov 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (f072ce5) to head (c4b8b34).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff     @@
##   main   #398   +/-   ##
===========================
===========================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions bot mentioned this pull request Nov 2, 2025
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.

De/Encode: allow trait object usage Reduce mono of generics in codecs

2 participants