Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Style: make poll_read_exact a general trait for vmess and other streams #675

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

VendettaReborn
Copy link
Contributor

🤔 This is a ...

  • New feature
  • Bug fix
  • Performance optimization
  • Enhancement feature
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Workflow
  • Other (about what?)

💡 Background and solution

there exists some similar code in vmess and shadowtls stream, which i found when i re-read the code.

the poll_read_exact method is especially useful for poll based packet decoding, many proxy projects have their own implement, but there are some ways to make it a general method with a base trait to provide the (inner stream, BytesMut, read_pos).
so here comes the base trait ReadExactBase, and ReadExact trait is automatically implemented for all types which have implemented ReadExactBase

pub trait ReadExactBase {
    /// inner stream to be polled
    type I: AsyncRead + Unpin;
    /// prepare the inner stream, read buffer and read position
    fn decompose(&mut self) -> (&mut Self::I, &mut BytesMut, &mut usize);
}

pub trait ReadExt: ReadExactBase {
    fn poll_read_exact(
        &mut self,
        cx: &mut std::task::Context,
        size: usize,
    ) -> Poll<std::io::Result<()>>;
}

☑️ Self-Check before Merge

⚠️ Please check all items below before requesting a reviewing. ⚠️

  • Doc is updated/provided or not needed
  • Changelog is provided or not needed

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