-
Notifications
You must be signed in to change notification settings - Fork 818
/
Size.cs
70 lines (56 loc) · 2.12 KB
/
Size.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
using System;
using System.Diagnostics;
namespace Npgsql.Internal;
public enum SizeKind : byte
{
Unknown = 0,
Exact,
UpperBound
}
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public readonly struct Size : IEquatable<Size>
{
readonly int _value;
readonly SizeKind _kind;
Size(SizeKind kind, int value)
{
_value = value;
_kind = kind;
}
public int Value
{
get
{
if (_kind is SizeKind.Unknown)
ThrowHelper.ThrowInvalidOperationException("Cannot get value from default or Unknown kind");
return _value;
}
}
internal int GetValueOrDefault() => _value;
public SizeKind Kind => _kind;
public static Size Create(int byteCount) => new(SizeKind.Exact, byteCount);
public static Size CreateUpperBound(int byteCount) => new(SizeKind.UpperBound, byteCount);
public static Size Unknown { get; } = new(SizeKind.Unknown, 0);
public static Size Zero { get; } = new(SizeKind.Exact, 0);
public Size Combine(Size result)
{
if (_kind is SizeKind.Unknown || result._kind is SizeKind.Unknown)
return Unknown;
if (_kind is SizeKind.UpperBound || result._kind is SizeKind.UpperBound)
return CreateUpperBound((int)Math.Min((long)(_value + result._value), int.MaxValue));
return Create((int)Math.Min((long)(_value + result._value), int.MaxValue));
}
public static implicit operator Size(int value) => Create(value);
string DebuggerDisplay
=> _kind switch
{
SizeKind.Exact or SizeKind.UpperBound => $"{_value} ({_kind})",
SizeKind.Unknown => "Unknown",
_ => throw new ArgumentOutOfRangeException()
};
public bool Equals(Size other) => _value == other._value && _kind == other.Kind;
public override bool Equals(object? obj) => obj is Size other && Equals(other);
public override int GetHashCode() => HashCode.Combine(_value, (int)_kind);
public static bool operator ==(Size left, Size right) => left.Equals(right);
public static bool operator !=(Size left, Size right) => !left.Equals(right);
}