48 lines
1.1 KiB
C#
48 lines
1.1 KiB
C#
using System;
|
|
|
|
namespace bts.utils {
|
|
static class Lazy {
|
|
public static Lazy<T> create<T>() where T : new() =>
|
|
new Lazy<T>(_CreateDefault<T>.impl);
|
|
|
|
static class _CreateDefault<T> where T : new() {
|
|
public static readonly Func<T> impl = () => new T();
|
|
}
|
|
}
|
|
|
|
static class LazyCast {
|
|
public static LazyCast<T> create<T>(object obj) =>
|
|
new LazyCast<T>(obj);
|
|
}
|
|
|
|
readonly struct LazyCast<T> {
|
|
readonly Lazy<T> _lazy;
|
|
|
|
public LazyCast(object obj) {
|
|
_lazy = new Lazy<T>(impl);
|
|
T impl() =>
|
|
(T)obj;
|
|
}
|
|
|
|
public T value => _lazy.Value;
|
|
|
|
public static implicit operator T(LazyCast<T> lazy_identity) =>
|
|
lazy_identity._lazy.Value;
|
|
}
|
|
|
|
readonly struct LazyCast<T, TSource> {
|
|
readonly Lazy<T> _lazy;
|
|
|
|
public LazyCast(TSource obj) {
|
|
_lazy = new Lazy<T>(impl);
|
|
T impl() =>
|
|
(T)(object)obj;
|
|
}
|
|
|
|
public T value => _lazy.Value;
|
|
|
|
public static implicit operator T(LazyCast<T, TSource> lazy_identity) =>
|
|
lazy_identity._lazy.Value;
|
|
}
|
|
}
|