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

possible Op poll simplification #137

Closed
FrankReh opened this issue Oct 21, 2022 · 3 comments
Closed

possible Op poll simplification #137

FrankReh opened this issue Oct 21, 2022 · 3 comments

Comments

@FrankReh
Copy link
Collaborator

Look at this simplication I found possible in the src/driver/read.rs

    pub(crate) async fn read(self) -> BufResult<usize, T> {
        let complete = self.await;

        // Convert the operation result to `usize`
        let res = complete.result.map(|v| v as usize);
        // Recover the buffer
        let mut buf = complete.data.buf;

        // If the operation was successful, advance the initialized cursor.
        if let Ok(n) = res {
            // Safety: the kernel wrote `n` bytes to the buffer.
            unsafe {
                buf.set_init(n);
            }
        }

        (res, buf)
    }

    /* old
    pub(crate) async fn read(mut self) -> BufResult<usize, T> {
        crate::future::poll_fn(move |cx| self.poll_i(cx)).await
    }

    fn poll_i(&mut self, cx: &mut Context<'_>) -> Poll<BufResult<usize, T>> {
        use std::future::Future;
        use std::pin::Pin;

        let complete = ready!(Pin::new(self).poll(cx));

        // Convert the operation result to `usize`
        let res = complete.result.map(|v| v as usize);
        // Recover the buffer
        let mut buf = complete.data.buf;

        // If the operation was successful, advance the initialized cursor.
        if let Ok(n) = res {
            // Safety: the kernel wrote `n` bytes to the buffer.
            unsafe {
                buf.set_init(n);
            }
        }

        Poll::Ready((res, buf))
    }
    */

It compiles and runs. Is fewer lines of code, one function instead of three, probably less cpu and less stack. Maybe less heap.

Any other driver operations that followed the earlier model might be easily swapped for this too.

Is anything important being lost with this?

@FrankReh
Copy link
Collaborator Author

I think this is possible as a result of the introduction of the Completion trait.

@Noah-Kennedy
Copy link
Contributor

Yeah the completion trait was meant to also allow this sort of work. I had actually meant to do this not long after I introduced that PR, but didn't get around to it.

@ollie-etl
Copy link
Collaborator

@FrankReh I think this is closed?

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

3 participants