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

style: make poll_read_exact a general trait #689

Merged
merged 1 commit into from
Jan 29, 2025

Conversation

VendettaReborn
Copy link
Contributor

re-open of #675

🤔 This is a ...

  • Code style optimization

💡 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<()>>;
}

Copy link
Member

@ibigbug ibigbug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@VendettaReborn VendettaReborn enabled auto-merge (squash) January 28, 2025 14:50
@VendettaReborn VendettaReborn merged commit e3f5a04 into Watfaq:master Jan 29, 2025
26 of 27 checks passed
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.

2 participants