Files
UniTask/src/UniTask.NetCoreSandbox/Program.cs

240 lines
5.5 KiB
C#
Raw Normal View History

2020-05-05 21:05:32 +09:00
using Cysharp.Threading.Tasks;
2020-05-08 03:23:14 +09:00
using System.Linq;
2020-05-05 21:05:32 +09:00
using System;
2020-05-08 03:23:14 +09:00
using System.Collections.Generic;
using System.Threading;
2020-05-05 21:05:32 +09:00
using System.Threading.Tasks;
2020-05-08 03:23:14 +09:00
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;
using Cysharp.Threading.Tasks.Linq;
2020-05-09 15:33:46 +09:00
using System.Reactive.Linq;
using System.Reactive.Concurrency;
2020-05-05 21:05:32 +09:00
namespace NetCoreSandbox
{
class Program
{
2020-05-08 03:23:14 +09:00
static string FlattenGenArgs(Type type)
{
if (type.IsGenericType)
{
var t = string.Join(", ", type.GetGenericArguments().Select(x => FlattenGenArgs(x)));
return Regex.Replace(type.Name, "`.+", "") + "<" + t + ">";
}
//x.ReturnType.GetGenericArguments()
else
{
return type.Name;
}
}
static async IAsyncEnumerable<int> FooAsync([EnumeratorCancellation]CancellationToken cancellationToken = default)
2020-05-05 21:05:32 +09:00
{
yield return 1;
await Task.Delay(10, cancellationToken);
}
2020-05-10 00:33:46 +09:00
2020-05-10 22:44:40 +09:00
static async Task Main(string[] args)
{
2020-05-11 12:38:32 +09:00
await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10)
.SelectAwait(x => UniTask.Run(() => x))
.SkipLast(6)
2020-05-11 12:02:02 +09:00
)
{
Console.WriteLine(item);
}
2020-05-11 12:38:32 +09:00
2020-05-08 03:23:14 +09:00
}
void Foo()
{
2020-05-10 22:44:40 +09:00
2020-05-11 12:02:02 +09:00
// AsyncEnumerable.Range(1,10).Do(
2020-05-10 22:44:40 +09:00
2020-05-08 03:23:14 +09:00
// AsyncEnumerable.t
var sb = new StringBuilder();
sb.AppendLine(@"using System;
using System.Threading;
namespace Cysharp.Threading.Tasks.Linq
{
");
var chako = typeof(AsyncEnumerable).GetMethods()
.OrderBy(x => x.Name)
.Select(x =>
{
var ret = FlattenGenArgs(x.ReturnType);
var generics = string.Join(", ", x.GetGenericArguments().Select(x => x.Name));
if (x.GetParameters().Length == 0) return "";
var self = x.GetParameters().First();
if (x.GetCustomAttributes(typeof(ExtensionAttribute), true).Length == 0)
{
return "";
}
var arg1Type = FlattenGenArgs(x.GetParameters().First().ParameterType);
var others = string.Join(", ", x.GetParameters().Skip(1).Select(y => FlattenGenArgs(y.ParameterType) + " " + y.Name));
if (!string.IsNullOrEmpty(others))
{
others = ", " + others;
}
var template = $"public static {ret} {x.Name}<{generics}>(this {arg1Type} {self.Name}{others})";
return template.Replace("ValueTask", "UniTask").Replace("IAsyncEnumerable", "IUniTaskAsyncEnumerable").Replace("<>", "");
})
.Where(x => x != "")
.Select(x => x + "\r\n{\r\n throw new NotImplementedException();\r\n}")
.ToArray();
var huga = string.Join("\r\n\r\n", chako);
foreach (var item in typeof(AsyncEnumerable).GetMethods().Select(x => x.Name).Distinct())
{
if (item.EndsWith("AwaitAsync") || item.EndsWith("AwaitWithCancellationAsync") || item.EndsWith("WithCancellation"))
{
continue;
}
var item2 = item.Replace("Async", "");
item2 = item2.Replace("Await", "");
var format = @"
internal sealed class {0}
{{
}}
";
sb.Append(string.Format(format, item2));
}
sb.Append("}");
Console.WriteLine(sb.ToString());
2020-05-05 21:05:32 +09:00
}
2020-05-08 03:23:14 +09:00
public static async IAsyncEnumerable<int> AsyncGen()
{
await UniTask.SwitchToThreadPool();
yield return 10;
await UniTask.SwitchToThreadPool();
yield return 100;
}
}
class MyEnumerable : IEnumerable<int>
{
public IEnumerator<int> GetEnumerator()
2020-05-05 21:34:11 +09:00
{
2020-05-08 03:23:14 +09:00
return new MyEnumerator();
}
2020-05-07 11:27:27 +09:00
2020-05-08 03:23:14 +09:00
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
2020-05-07 11:27:27 +09:00
2020-05-08 03:23:14 +09:00
class MyEnumerator : IEnumerator<int>
{
public int Current => throw new NotImplementedException();
2020-05-07 11:27:27 +09:00
2020-05-08 03:23:14 +09:00
object IEnumerator.Current => throw new NotImplementedException();
2020-05-07 11:27:27 +09:00
2020-05-08 03:23:14 +09:00
public void Dispose()
{
Console.WriteLine("Called Dispose");
2020-05-05 21:34:11 +09:00
}
2020-05-08 03:23:14 +09:00
public bool MoveNext()
{
throw new NotImplementedException();
}
2020-05-05 21:05:32 +09:00
2020-05-08 03:23:14 +09:00
public void Reset()
2020-05-05 21:05:32 +09:00
{
2020-05-08 03:23:14 +09:00
throw new NotImplementedException();
}
}
2020-05-05 21:05:32 +09:00
2020-05-08 03:23:14 +09:00
public class MyClass<T>
{
public CustomAsyncEnumerator<T> GetAsyncEnumerator()
{
//IAsyncEnumerable
return new CustomAsyncEnumerator<T>();
}
}
2020-05-05 21:05:32 +09:00
2020-05-08 03:23:14 +09:00
public struct CustomAsyncEnumerator<T>
{
int count;
public T Current
{
get
{
return default;
}
2020-05-05 21:05:32 +09:00
}
2020-05-08 03:23:14 +09:00
public UniTask<bool> MoveNextAsync()
{
if (count++ == 3)
{
return UniTask.FromResult(false);
//return false;
}
return UniTask.FromResult(true);
}
2020-05-05 21:05:32 +09:00
2020-05-08 03:23:14 +09:00
public UniTask DisposeAsync()
{
return default;
}
2020-05-05 21:05:32 +09:00
}
2020-05-08 03:23:14 +09:00
2020-05-05 21:05:32 +09:00
}