I have implemented IAsyncDisposable with an ActionOnAsyncDispose struct as shown below. My understanding is that the compiler will not box it when it is in an async using statement:
ActionOnDisposeAsync x = ...;
await using (x) {
...
}
Correct? So far so good. My question is this, when I configure await on it like so:
ActionOnDisposeAsync x = ...;
await using (x.ConfigureAwait()) {
...
}
will x be boxed? What about if I put the ConfigureAwait in a method, Caf():
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static public ConfiguredAsyncDisposable Caf(this ActionOnDisposeAsync disposable)
=> disposable.ConfigureAwait(false);
ActionOnDisposeAsync x = ...;
await using (x.Caf()) {
...
}
Can I avoid boxing in that case? I was not able to find documentation on what exactly my using variable needs to implement in order to have the effect of ConfigureAwait. There doesn't seem to be any public way of constructing a ConfiguredAsyncDisposable either.
Here is ActionOnDisposeAsync:
public readonly struct ActionOnDisposeAsync : IAsyncDisposable, IEquatable<ActionOnDisposeAsync>
{
public ActionOnDisposeAsync(Func<Task> actionAsync)
{
this.ActionAsync = actionAsync;
}
public ActionOnDisposeAsync( Action actionSync)
{
this.ActionAsync = () => { actionSync(); return Task.CompletedTask; };
}
private Func<Task> ActionAsync { get; }
public async ValueTask DisposeAsync()
{
if (this.ActionAsync != null) {
await this.ActionAsync();
}
}
...
}