Created
January 29, 2015 21:48
-
-
Save Porges/a5a2bacdcc3650a8620e to your computer and use it in GitHub Desktop.
disposables where ownership can be transferred
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Owned<T> : IDisposable | |
where T : class, IDisposable | |
{ | |
private T _value; | |
public Owned(T value) | |
{ | |
_value = value; | |
} | |
public T Value => _value; | |
public T Exchange(T newValue) => Interlocked.Exchange(ref _value, newValue); | |
public T Release() => Exchange(null); | |
public Owned<T> Move() => Owned.Create(Release()); | |
public void Dispose() => Release()?.Dispose(); | |
} | |
public static class Owned | |
{ | |
public static Owned<T> Create<T>(T value) | |
where T : class, IDisposable | |
{ | |
return new Owned<T>(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment