diff --git a/src/ContextFreeTasks.Shared/AsyncContextFreeTaskMethodBuilder.cs b/src/ContextFreeTasks.Shared/AsyncContextFreeTaskMethodBuilder.cs index e2ebd31..08ed03f 100644 --- a/src/ContextFreeTasks.Shared/AsyncContextFreeTaskMethodBuilder.cs +++ b/src/ContextFreeTasks.Shared/AsyncContextFreeTaskMethodBuilder.cs @@ -4,7 +4,7 @@ using System.Security; using System.Threading; -namespace ContextFreeTasks +namespace ContextFreeTasks.Internal { [StructLayout(LayoutKind.Auto)] public struct AsyncContextFreeTaskMethodBuilder diff --git a/src/ContextFreeTasks.Shared/AsyncContextFreeTaskMethodBuilder_T.cs b/src/ContextFreeTasks.Shared/AsyncContextFreeTaskMethodBuilder_T.cs index 605e8fb..dea09f5 100644 --- a/src/ContextFreeTasks.Shared/AsyncContextFreeTaskMethodBuilder_T.cs +++ b/src/ContextFreeTasks.Shared/AsyncContextFreeTaskMethodBuilder_T.cs @@ -4,7 +4,7 @@ using System.Security; using System.Threading; -namespace ContextFreeTasks +namespace ContextFreeTasks.Internal { [StructLayout(LayoutKind.Auto)] public struct AsyncContextFreeTaskMethodBuilder diff --git a/src/ContextFreeTasks.Shared/AsyncMethodBuilderAttribute.cs b/src/ContextFreeTasks.Shared/AsyncMethodBuilderAttribute.cs index 5df0918..8bcce03 100644 --- a/src/ContextFreeTasks.Shared/AsyncMethodBuilderAttribute.cs +++ b/src/ContextFreeTasks.Shared/AsyncMethodBuilderAttribute.cs @@ -1,7 +1,7 @@ namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Delegate | AttributeTargets.Enum, Inherited = false, AllowMultiple = false)] - public sealed class AsyncMethodBuilderAttribute : Attribute + internal sealed class AsyncMethodBuilderAttribute : Attribute { public AsyncMethodBuilderAttribute(Type builderType) => BuilderType = builderType; public Type BuilderType { get; } diff --git a/src/ContextFreeTasks.Shared/ContextFreeTask.cs b/src/ContextFreeTasks.Shared/ContextFreeTask.cs index 0e84605..710d5fd 100644 --- a/src/ContextFreeTasks.Shared/ContextFreeTask.cs +++ b/src/ContextFreeTasks.Shared/ContextFreeTask.cs @@ -1,13 +1,18 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; +using ContextFreeTasks.Internal; +using static System.Threading.Tasks.Task; namespace ContextFreeTasks { [AsyncMethodBuilder(typeof(AsyncContextFreeTaskMethodBuilder))] public struct ContextFreeTask { - public Task Task { get; } - public ContextFreeTask(Task t) => Task = t; + private readonly Task _task; + public Task Task => _task ?? FromResult(default(object)); + internal ContextFreeTask(Task t) => _task = t; public ContextFreeTaskAwaiter GetAwaiter() => new ContextFreeTaskAwaiter(Task); + public void Wait() => _task?.Wait(); + public ConfiguredTaskAwaitable ConfigureAwait(bool continueOnCapturedContext) => Task.ConfigureAwait(continueOnCapturedContext); } } diff --git a/src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter.cs b/src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter.cs index bde63f0..947d97e 100644 --- a/src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter.cs +++ b/src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter.cs @@ -2,12 +2,12 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; -namespace ContextFreeTasks +namespace ContextFreeTasks.Internal { public struct ContextFreeTaskAwaiter : ICriticalNotifyCompletion { private readonly Task _value; - internal ContextFreeTaskAwaiter(Task value) { _value = value; } + internal ContextFreeTaskAwaiter(Task value) => _value = value; public bool IsCompleted => _value.IsCompleted; public void GetResult() => _value.GetAwaiter().GetResult(); public void OnCompleted(Action continuation) => _value.ConfigureAwait(false).GetAwaiter().OnCompleted(continuation); diff --git a/src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter_T.cs b/src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter_T.cs index fe35932..2d6410d 100644 --- a/src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter_T.cs +++ b/src/ContextFreeTasks.Shared/ContextFreeTaskAwaiter_T.cs @@ -2,12 +2,12 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; -namespace ContextFreeTasks +namespace ContextFreeTasks.Internal { public struct ContextFreeTaskAwaiter : ICriticalNotifyCompletion { private readonly Task _value; - internal ContextFreeTaskAwaiter(Task value) { _value = value; } + internal ContextFreeTaskAwaiter(Task value) => _value = value; public bool IsCompleted => _value.IsCompleted; public T GetResult() => _value.GetAwaiter().GetResult(); public void OnCompleted(Action continuation) => _value.ConfigureAwait(false).GetAwaiter().OnCompleted(continuation); diff --git a/src/ContextFreeTasks.Shared/ContextFreeTask_T.cs b/src/ContextFreeTasks.Shared/ContextFreeTask_T.cs index 86d3f30..e85ac18 100644 --- a/src/ContextFreeTasks.Shared/ContextFreeTask_T.cs +++ b/src/ContextFreeTasks.Shared/ContextFreeTask_T.cs @@ -1,13 +1,19 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; +using ContextFreeTasks.Internal; +using static System.Threading.Tasks.Task; namespace ContextFreeTasks { [AsyncMethodBuilder(typeof(AsyncContextFreeTaskMethodBuilder<>))] public struct ContextFreeTask { - public Task Task { get; } - public ContextFreeTask(Task t) => Task = t; + private readonly Task _task; + public Task Task => _task ?? FromResult(default(T)); + public T Result => GetAwaiter().GetResult(); + internal ContextFreeTask(Task t) => _task = t; public ContextFreeTaskAwaiter GetAwaiter() => new ContextFreeTaskAwaiter(Task); + public void Wait() => _task?.Wait(); + public ConfiguredTaskAwaitable ConfigureAwait(bool continueOnCapturedContext) => Task.ConfigureAwait(continueOnCapturedContext); } } diff --git a/tests/UnitTestNet45/UnitTest1.cs b/tests/UnitTestNet45/UnitTest1.cs index 4eea85e..e1c5b07 100644 --- a/tests/UnitTestNet45/UnitTest1.cs +++ b/tests/UnitTestNet45/UnitTest1.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using ContextFreeTasks; using System.Threading.Tasks; +using ContextFreeTasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTest {