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 API for custom async iterators to wait for dispose #476

Open
timcassell opened this issue Sep 13, 2024 · 0 comments
Open

Expose API for custom async iterators to wait for dispose #476

timcassell opened this issue Sep 13, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@timcassell
Copy link
Owner

await writer.YieldAsync(default).ForLinqExtension(); is used heavily internally to prevent wrapped enumerators from being disposed before the current enumerator is disposed in order to support TempCollections. This method could be useful for users to write their own AsyncEnumerable extensions. The current API is not suitable to make public. I'm thinking a better API would be:

namespace Proto.Promises.CompilerServices
{
    public static class AsyncEnumerableHelpers
    {
        public AsyncStreamYielder<T> WaitForDisposeAsync<T>(AsyncStreamWriter<T> asyncStreamWriter);
    }
}

It is on a helper type in the Proto.Promises.CompilerServices namespace instead of on the AsyncStreamWriter<T> type so that it won't be available to users directly. It's technically not unsafe because we can verify its usage, but it is an unintuitive API that should only be used by advanced users who know what they are doing (similar to many APIs in System.Runtime.CompilerServices namespace).

This API should be the very last instruction used in the async iterator function before the finally block that disposes any wrapped async enumerators. It should be the last instruction inside the try block or (await) using block. This should theoretically be verifiable by an analyzer, but I'm not familiar enough with the flow analysis to be able to write that analyzer. Unless someone more skilled with analyzers wants to take a crack at it, we can just use the existing async iterator analyzers, and rely on runtime validation to ensure no further yields or waits are used after it.

@timcassell timcassell added the enhancement New feature or request label Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant