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

Expose the guard API to allow users to enter tokio runtime #34

Open
ItsSunnyMonster opened this issue Sep 22, 2024 · 0 comments · May be fixed by #39
Open

Expose the guard API to allow users to enter tokio runtime #34

ItsSunnyMonster opened this issue Sep 22, 2024 · 0 comments · May be fixed by #39

Comments

@ItsSunnyMonster
Copy link

I came across #21 which is similar to this issue but it seems like only the Drop part of that issue was implemented.

What I have is an enum which contains two variants: one contains a File from the async_fs crate and one contains a Compat<TempFile> from the async_tempfile crate (which uses tokio), and I have implementations of AsyncRead and AsyncSeek for this enum which just calls the poll_read function of the underlying types. I need to somehow enter the tokio runtime when the variant is the one containing the tempfile.

I could just document the fact that my type has to be used with compat, but this is not great for two reasons: the users would have to put compat everywhere they use my type, and also they would be unnecessarily entering a tokio runtime every single time the enum's variant is the one containing the async file.

Here's some code to better illustrate my point:

pub enum AssetFileReader<'r, R>
where
    R: AsyncRead + AsyncSeek + Unpin,
{
    // This "DirectFileReader" thing just holds a reference to an async File
    // It also forwards the async File AsyncRead and AsyncSeek implementations
    Normal(DirectFileReader<'r, R>),
    Decompressed(Compat<TempFile>),
}

impl<'r, R: AsyncRead + AsyncSeek + Unpin> AsyncRead for AssetFileReader<'r, R> {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut [u8],
    ) -> Poll<futures_lite::io::Result<usize>> {
        let this = self.get_mut();

        match this {
            AssetFileReader::Normal(r) => Pin::new(r).poll_read(cx, buf),
            AssetFileReader::Decompressed(r) => {
                // This is perhaps what the API should look like?
                let _guard = enter_tokio_runtime();
                Pin::new(r).poll_read(cx, buf)
            },
        }
    }
}

// The AsyncSeek implementation is similar
@ItsSunnyMonster ItsSunnyMonster linked a pull request Dec 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant