mirror of
https://github.com/Cardidi/ca2d-unity-toolkit.git
synced 2025-12-20 01:06:03 +08:00
51 lines
1.1 KiB
C#
51 lines
1.1 KiB
C#
using System;
|
|
using UnityEngine.Assertions;
|
|
|
|
namespace Ca2d.Toolkit
|
|
{
|
|
[Serializable]
|
|
public struct Option<T>
|
|
{
|
|
public bool Enabled;
|
|
|
|
public T Value;
|
|
|
|
public T ValueOrDefault()
|
|
{
|
|
return Enabled ? Value : default;
|
|
}
|
|
|
|
public T ValueOrDefault(T def)
|
|
{
|
|
return Enabled ? Value : def;
|
|
}
|
|
|
|
public T ValueOrDefault(Func<T> def)
|
|
{
|
|
Assert.IsNotNull(def);
|
|
return Enabled ? Value : def();
|
|
}
|
|
|
|
public T ValueOrDefault<TSelector>(TSelector src, Func<TSelector, T> def)
|
|
{
|
|
Assert.IsNotNull(def);
|
|
return Enabled ? Value : def(src);
|
|
}
|
|
|
|
public Option(T value)
|
|
{
|
|
Enabled = true;
|
|
Value = value;
|
|
}
|
|
|
|
public static implicit operator T(Option<T> wrapper)
|
|
{
|
|
return wrapper.Value;
|
|
}
|
|
|
|
public static implicit operator Option<T>(T value)
|
|
{
|
|
return new Option<T>(value);
|
|
}
|
|
}
|
|
} |