Able
Askowl Base Library Enabler
Disposable.cs
1 // Copyright 2018 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 namespace Askowl {
4  using System;
5 
6  /// <a href="http://bit.ly/2Oo7I8v">Simplified creation of an action that happens at the end of a using statement no matter what else happens. The using is am implicit try/finally block with the IDisposable called as part of finally. It is a struct, so no added heap footprint. `IDisposable Ephemeral() => new Disposable {action = () => whatever};`</a>
7  public struct Disposable : IDisposable {
8  /// <a href="http://bit.ly/2Oo7I8v">Use iDisposable for Greater Good</a>
9  public Action Action;
10 
11  /// <inheritdoc />
12  public void Dispose() { Action?.Invoke(); }
13  }
14 
15  /// <a href="http://bit.ly/2Rj0TUq">`Disposable`, but carrying a payload</a>
16  public struct Disposable<T> : IDisposable {
17  /// <a href="http://bit.ly/2Rj0TUq">Use iDisposable for Greater Good</a>
18  public Action<T> Action;
19 
20  /// <a href="http://bit.ly/2Rj0TUq">The payload</a>
21  public T Value;
22 
23  /// <inheritdoc />
24  public void Dispose() {
25  Action?.Invoke(Value);
26  (Value as IDisposable)?.Dispose();
27  }
28  }
29 }
Definition: Clock.cs:3
Action Action
Use iDisposable for Greater Good
Definition: Disposable.cs:9
T Value
The payload
Definition: Disposable.cs:21
Action< T > Action
Use iDisposable for Greater Good
Definition: Disposable.cs:18
Simplified creation of an action that happens at the end of a using statement no matter what else hap...
Definition: Disposable.cs:7