-
Notifications
You must be signed in to change notification settings - Fork 1
/
Futures.cs
140 lines (116 loc) · 4.03 KB
/
Futures.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This file encapsulates all uses of the Squared APIs.
// TODO: Remove the use of Squared.Threading and Squared.Util in this namespace.
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using Squared.Threading;
namespace crdebug {
public interface IFuture : IDisposable {
bool CompletedSuccessfully { get; }
Exception Exception { get; }
Type ResultType { get; }
FutureAwaitExtensionMethods.IFutureAwaiter GetAwaiter ();
bool TrySetResult (object value);
void SetResult (object value);
void SetException (Exception exception);
void SetException (ExceptionDispatchInfo exception);
}
public class Future<T> : IFuture {
internal readonly Squared.Threading.Future<T> SquaredFuture;
public Future () {
SquaredFuture = new Squared.Threading.Future<T>();
}
public Future (T result)
: this () {
SetResult(result);
}
public Future (Exception exception)
: this () {
SetException(exception);
}
public Future (ExceptionDispatchInfo exception)
: this () {
SetException(exception);
}
public bool TrySetResult (T result) {
if (SquaredFuture.Completed)
return false;
SquaredFuture.SetResult2(result, null);
return true;
}
public void SetResult (T result) {
SquaredFuture.SetResult2(result, null);
}
public void SetException (Exception exception) {
SetException(ExceptionDispatchInfo.Capture(exception));
}
public void SetException (ExceptionDispatchInfo exception) {
SquaredFuture.SetResult2(default(T), exception);
}
public bool TryGetResult (out T result, out Exception exception) {
if (SquaredFuture.Failed) {
result = default(T);
exception = SquaredFuture.Error;
return false;
} else if (SquaredFuture.Completed) {
result = SquaredFuture.Result2;
exception = null;
return true;
} else {
result = default(T);
exception = null;
return false;
}
}
public void Cancel () {
SquaredFuture.Dispose();
}
FutureAwaitExtensionMethods.IFutureAwaiter IFuture.GetAwaiter () {
return ((Squared.Threading.IFuture)SquaredFuture).GetAwaiter();
}
bool IFuture.TrySetResult (object value) {
var f = (Squared.Threading.IFuture)SquaredFuture;
if (f.Completed)
return false;
f.SetResult2(value, null);
return true;
}
void IFuture.SetResult (object value) {
((Squared.Threading.IFuture)SquaredFuture).SetResult2(value, null);
}
void IDisposable.Dispose () {
SquaredFuture.Dispose();
}
public T Result {
get {
return SquaredFuture.Result2;
}
}
public bool CompletedSuccessfully {
get {
return SquaredFuture.Completed && !SquaredFuture.Failed;
}
}
public Exception Exception {
get {
return SquaredFuture.Error;
}
}
public Type ResultType {
get {
return typeof(T);
}
}
public FutureAwaitExtensionMethods.NonThrowingFuture<T> DoNotThrow () {
return SquaredFuture.DoNotThrow();
}
// Why can't it infer this?
public FutureAwaitExtensionMethods.FutureWithDisposedValue<T> ValueWhenDisposed (object value) {
return SquaredFuture.ValueWhenDisposed((T)value);
}
public FutureAwaitExtensionMethods.FutureAwaiter<T> GetAwaiter () {
return SquaredFuture.GetAwaiter();
}
}
}