forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadOnlyTagCollection.cs
More file actions
62 lines (53 loc) · 2.03 KB
/
ReadOnlyTagCollection.cs
File metadata and controls
62 lines (53 loc) · 2.03 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
namespace OpenTelemetry;
/// <summary>
/// A read-only collection of tag key/value pairs.
/// </summary>
// Note: Does not implement IReadOnlyCollection<> or IEnumerable<> to
// prevent accidental boxing.
public readonly struct ReadOnlyTagCollection
{
internal readonly KeyValuePair<string, object?>[] KeyAndValues;
internal ReadOnlyTagCollection(KeyValuePair<string, object?>[]? keyAndValues)
{
this.KeyAndValues = keyAndValues ?? Array.Empty<KeyValuePair<string, object?>>();
}
/// <summary>
/// Gets the number of tags in the collection.
/// </summary>
public int Count => this.KeyAndValues.Length;
/// <summary>
/// Returns an enumerator that iterates through the tags.
/// </summary>
/// <returns><see cref="Enumerator"/>.</returns>
public Enumerator GetEnumerator() => new(this);
/// <summary>
/// Enumerates the elements of a <see cref="ReadOnlyTagCollection"/>.
/// </summary>
// Note: Does not implement IEnumerator<> to prevent accidental boxing.
public struct Enumerator
{
private readonly ReadOnlyTagCollection source;
private int index;
internal Enumerator(ReadOnlyTagCollection source)
{
this.source = source;
this.index = -1;
}
/// <summary>
/// Gets the tag at the current position of the enumerator.
/// </summary>
public readonly KeyValuePair<string, object?> Current
=> this.source.KeyAndValues[this.index];
/// <summary>
/// Advances the enumerator to the next element of the <see
/// cref="ReadOnlyTagCollection"/>.
/// </summary>
/// <returns><see langword="true"/> if the enumerator was
/// successfully advanced to the next element; <see
/// langword="false"/> if the enumerator has passed the end of the
/// collection.</returns>
public bool MoveNext() => ++this.index < this.source.Count;
}
}