Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
async stream の生成結果
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
public static class Program {
public static async Task Main() {
IAsyncEnumerable<int> asyncStream = YieldReturnSample();
// foreach でなく await foreach で消費
await foreach (int x in asyncStream)
{
Console.WriteLine(x);
}
}
public static async IAsyncEnumerable<int> YieldReturnSample() {
foreach (var x in Enumerable.Range(1, 10)) {
yield return await Delay(x);
}
}
private static async Task<int> Delay(int millisec) {
await Task.Delay(millisec);
return millisec ^ 2;
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Security.Permissions;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
public static class Program
{
[CompilerGenerated]
private sealed class <Main>d__0 : IAsyncStateMachine
{
public int <>1__state;
public AsyncTaskMethodBuilder <>t__builder;
private IAsyncEnumerable<int> <asyncStream>5__1;
private IAsyncEnumerator<int> <>s__2;
private object <>s__3;
private int <>s__4;
private int <x>5__5;
private bool <>s__6;
private ValueTaskAwaiter<bool> <>u__1;
private ValueTaskAwaiter <>u__2;
private void MoveNext()
{
int num = <>1__state;
try
{
ValueTaskAwaiter awaiter;
if (num != 0)
{
if (num == 1)
{
awaiter = <>u__2;
<>u__2 = default(ValueTaskAwaiter);
num = (<>1__state = -1);
goto IL_016b;
}
<asyncStream>5__1 = YieldReturnSample();
<>s__2 = <asyncStream>5__1.GetAsyncEnumerator(default(CancellationToken));
<>s__3 = null;
<>s__4 = 0;
}
object obj2;
try
{
if (num != 0)
{
goto IL_0077;
}
ValueTaskAwaiter<bool> awaiter2 = <>u__1;
<>u__1 = default(ValueTaskAwaiter<bool>);
num = (<>1__state = -1);
goto IL_00d8;
IL_0077:
awaiter2 = <>s__2.MoveNextAsync().GetAwaiter();
if (!awaiter2.IsCompleted)
{
num = (<>1__state = 0);
<>u__1 = awaiter2;
<Main>d__0 stateMachine = this;
<>t__builder.AwaitUnsafeOnCompleted(ref awaiter2, ref stateMachine);
return;
}
goto IL_00d8;
IL_00d8:
<>s__6 = awaiter2.GetResult();
if (<>s__6)
{
<x>5__5 = <>s__2.Current;
Console.WriteLine(<x>5__5);
goto IL_0077;
}
}
catch (object obj)
{
obj2 = (<>s__3 = obj);
}
if (<>s__2 != null)
{
awaiter = <>s__2.DisposeAsync().GetAwaiter();
if (!awaiter.IsCompleted)
{
num = (<>1__state = 1);
<>u__2 = awaiter;
<Main>d__0 stateMachine = this;
<>t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
return;
}
goto IL_016b;
}
goto IL_0173;
IL_016b:
awaiter.GetResult();
goto IL_0173;
IL_0173:
obj2 = <>s__3;
if (obj2 != null)
{
Exception ex = obj2 as Exception;
if (ex == null)
{
throw obj2;
}
ExceptionDispatchInfo.Capture(ex).Throw();
}
int <>s__7 = <>s__4;
<>s__3 = null;
<>s__2 = null;
}
catch (Exception exception)
{
<>1__state = -2;
<>t__builder.SetException(exception);
return;
}
<>1__state = -2;
<>t__builder.SetResult();
}
void IAsyncStateMachine.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
this.MoveNext();
}
[DebuggerHidden]
private void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
//ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
this.SetStateMachine(stateMachine);
}
}
[CompilerGenerated]
private sealed class <YieldReturnSample>d__1 : IAsyncEnumerable<int>, IAsyncEnumerator<int>, IAsyncDisposable, IValueTaskSource<bool>, IValueTaskSource, IAsyncStateMachine
{
public int <>1__state;
public AsyncIteratorMethodBuilder <>t__builder;
public ManualResetValueTaskSourceCore<bool> <>v__promiseOfValueOrEnd;
private int <>2__current;
private bool <>w__disposeMode;
private int <>l__initialThreadId;
private IEnumerator<int> <>s__1;
private int <x>5__2;
private int <>s__3;
private TaskAwaiter<int> <>u__1;
int IAsyncEnumerator<int>.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
[DebuggerHidden]
public <YieldReturnSample>d__1(int <>1__state)
{
this.<>1__state = <>1__state;
<>l__initialThreadId = Environment.CurrentManagedThreadId;
<>t__builder = AsyncIteratorMethodBuilder.Create();
}
private void MoveNext()
{
int num = <>1__state;
try
{
switch (num)
{
default:
if (!<>w__disposeMode)
{
num = (<>1__state = -1);
<>s__1 = Enumerable.Range(1, 10).GetEnumerator();
break;
}
goto end_IL_0007;
case -4:
case 0:
break;
}
try
{
TaskAwaiter<int> awaiter;
if (num != -4)
{
if (num == 0)
{
awaiter = <>u__1;
<>u__1 = default(TaskAwaiter<int>);
num = (<>1__state = -1);
goto IL_00dd;
}
goto IL_0119;
}
num = (<>1__state = -1);
if (!<>w__disposeMode)
{
goto IL_0119;
}
goto end_IL_0056;
IL_0119:
if (<>s__1.MoveNext())
{
<x>5__2 = <>s__1.Current;
awaiter = Delay(<x>5__2).GetAwaiter();
if (!awaiter.IsCompleted)
{
num = (<>1__state = 0);
<>u__1 = awaiter;
<YieldReturnSample>d__1 stateMachine = this;
<>t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
return;
}
goto IL_00dd;
}
goto end_IL_0056;
IL_00dd:
<>s__3 = awaiter.GetResult();
<>2__current = <>s__3;
num = (<>1__state = -4);
goto IL_0185;
end_IL_0056:;
}
finally
{
if (num == -1 && <>s__1 != null)
{
<>s__1.Dispose();
}
}
if (!<>w__disposeMode)
{
<>s__1 = null;
}
end_IL_0007:;
}
catch (Exception exception)
{
<>1__state = -2;
<>v__promiseOfValueOrEnd.SetException(exception);
return;
}
<>1__state = -2;
<>v__promiseOfValueOrEnd.SetResult(false);
return;
IL_0185:
<>v__promiseOfValueOrEnd.SetResult(true);
}
void IAsyncStateMachine.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
this.MoveNext();
}
[DebuggerHidden]
private void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
//ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
this.SetStateMachine(stateMachine);
}
[DebuggerHidden]
IAsyncEnumerator<int> IAsyncEnumerable<int>.GetAsyncEnumerator(CancellationToken cancellationToken)
{
<YieldReturnSample>d__1 result;
if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
{
<>1__state = -3;
result = this;
<>w__disposeMode = false;
}
else
{
result = new <YieldReturnSample>d__1(-3);
}
return result;
}
[DebuggerHidden]
ValueTask<bool> IAsyncEnumerator<int>.MoveNextAsync()
{
if (<>1__state == -2)
{
return default(ValueTask<bool>);
}
<>v__promiseOfValueOrEnd.Reset();
<YieldReturnSample>d__1 stateMachine = this;
<>t__builder.MoveNext(ref stateMachine);
short version = <>v__promiseOfValueOrEnd.Version;
if (<>v__promiseOfValueOrEnd.GetStatus(version) == ValueTaskSourceStatus.Succeeded)
{
return new ValueTask<bool>(<>v__promiseOfValueOrEnd.GetResult(version));
}
return new ValueTask<bool>(this, version);
}
[DebuggerHidden]
bool IValueTaskSource<bool>.GetResult(short token)
{
return <>v__promiseOfValueOrEnd.GetResult(token);
}
[DebuggerHidden]
ValueTaskSourceStatus IValueTaskSource<bool>.GetStatus(short token)
{
return <>v__promiseOfValueOrEnd.GetStatus(token);
}
[DebuggerHidden]
void IValueTaskSource<bool>.OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
{
<>v__promiseOfValueOrEnd.OnCompleted(continuation, state, token, flags);
}
[DebuggerHidden]
void IValueTaskSource.GetResult(short token)
{
<>v__promiseOfValueOrEnd.GetResult(token);
}
[DebuggerHidden]
ValueTaskSourceStatus IValueTaskSource.GetStatus(short token)
{
return <>v__promiseOfValueOrEnd.GetStatus(token);
}
[DebuggerHidden]
void IValueTaskSource.OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
{
<>v__promiseOfValueOrEnd.OnCompleted(continuation, state, token, flags);
}
[DebuggerHidden]
ValueTask IAsyncDisposable.DisposeAsync()
{
if (<>1__state >= -1)
{
throw new NotSupportedException();
}
if (<>1__state == -2)
{
return default(ValueTask);
}
<>w__disposeMode = true;
<>v__promiseOfValueOrEnd.Reset();
<YieldReturnSample>d__1 stateMachine = this;
<>t__builder.MoveNext(ref stateMachine);
return new ValueTask(this, <>v__promiseOfValueOrEnd.Version);
}
}
[CompilerGenerated]
private sealed class <Delay>d__2 : IAsyncStateMachine
{
public int <>1__state;
public AsyncTaskMethodBuilder<int> <>t__builder;
public int millisec;
private TaskAwaiter <>u__1;
private void MoveNext()
{
int num = <>1__state;
int result;
try
{
TaskAwaiter awaiter;
if (num != 0)
{
awaiter = Task.Delay(millisec).GetAwaiter();
if (!awaiter.IsCompleted)
{
num = (<>1__state = 0);
<>u__1 = awaiter;
<Delay>d__2 stateMachine = this;
<>t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
return;
}
}
else
{
awaiter = <>u__1;
<>u__1 = default(TaskAwaiter);
num = (<>1__state = -1);
}
awaiter.GetResult();
result = (millisec ^ 2);
}
catch (Exception exception)
{
<>1__state = -2;
<>t__builder.SetException(exception);
return;
}
<>1__state = -2;
<>t__builder.SetResult(result);
}
void IAsyncStateMachine.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
this.MoveNext();
}
[DebuggerHidden]
private void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
//ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
this.SetStateMachine(stateMachine);
}
}
[AsyncStateMachine(typeof(<Main>d__0))]
[DebuggerStepThrough]
public static Task Main()
{
<Main>d__0 stateMachine = new <Main>d__0();
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>1__state = -1;
AsyncTaskMethodBuilder <>t__builder = stateMachine.<>t__builder;
<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncIteratorStateMachine(typeof(<YieldReturnSample>d__1))]
public static IAsyncEnumerable<int> YieldReturnSample()
{
return new <YieldReturnSample>d__1(-2);
}
[AsyncStateMachine(typeof(<Delay>d__2))]
[DebuggerStepThrough]
private static Task<int> Delay(int millisec)
{
<Delay>d__2 stateMachine = new <Delay>d__2();
stateMachine.millisec = millisec;
stateMachine.<>t__builder = AsyncTaskMethodBuilder<int>.Create();
stateMachine.<>1__state = -1;
AsyncTaskMethodBuilder<int> <>t__builder = stateMachine.<>t__builder;
<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment