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

Be able to split Read + Write out of CustomStream for Sync across threads? #247

Open
brandonros opened this issue Nov 11, 2023 · 0 comments

Comments

@brandonros
Copy link

Use case: I'm being really silly and make a super non-serious "sync only, no async" "WebSocket server" (if you can call it that) based off request.upgrade() from this library.

pub struct CustomStream<R, W> {

The problem is, I want to have 2 threads.

1 that reads from client -> server

1 that writes server -> client

I haven't found a good way to "non-lockingly + non-blockingly" handle this because custom_stream doesn't have .split()

impl<R, W> Split for CustomStream<R, W>
where
    R: Read,
    W: Write,
{
    type Reader = R;
    type Writer = W;

    fn split(self) -> (R, W) {
        (self.reader, self.writer)
    }
}

I don't know what we would run into in terms of borrow/lifecycle issues, but I'm having a hard time figuring out how to achieve this trait wise.

    type Reader: Read;
    type Writer: Write;

    fn split(self) -> (Self::Reader, Self::Writer);
}
pub trait ReadWrite: Read + Write {}
pub trait ReadWriteSplit: ReadWrite + Split {}
impl<T> ReadWrite for T where T: Read + Write {}
impl<T: ReadWrite + Split> ReadWriteSplit for T {}

I was trying to do this (kind of dirty/gross/might be the wrong approach) but I am not sure if it is right. Would be interested to hear some feedback/if what I am trying is a bad idea.

An alternative would be on CustomStream to be able to get .reader() .writer() somehow?

@brandonros brandonros changed the title Be able to split Read + Write out of CustomStream Be able to split Read + Write out of CustomStream for Sync across threads? Nov 11, 2023
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

No branches or pull requests

1 participant