-
Notifications
You must be signed in to change notification settings - Fork 10
/
label.rs
159 lines (133 loc) · 3.46 KB
/
label.rs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0
use super::*;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub enum LabelValue {
Str(StringId),
Num {
num: i64,
num_unit: Option<StringId>,
},
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct Label {
key: StringId,
value: LabelValue,
}
impl Label {
pub fn has_num_value(&self) -> bool {
matches!(self.value, LabelValue::Num { .. })
}
pub fn has_string_value(&self) -> bool {
matches!(self.value, LabelValue::Str(_))
}
pub fn get_key(&self) -> StringId {
self.key
}
pub fn get_value(&self) -> &LabelValue {
&self.value
}
pub fn num(key: StringId, num: i64, num_unit: Option<StringId>) -> Self {
Self {
key,
value: LabelValue::Num { num, num_unit },
}
}
pub fn str(key: StringId, v: StringId) -> Self {
Self {
key,
value: LabelValue::Str(v),
}
}
}
impl From<Label> for pprof::Label {
fn from(l: Label) -> Self {
Self::from(&l)
}
}
impl From<&Label> for pprof::Label {
fn from(l: &Label) -> pprof::Label {
let key = l.key.to_raw_id();
match l.value {
LabelValue::Str(str) => Self {
key,
str: str.to_raw_id(),
num: 0,
num_unit: 0,
},
LabelValue::Num { num, num_unit } => Self {
key,
str: 0,
num,
num_unit: num_unit.map(StringId::into_raw_id).unwrap_or_default(),
},
}
}
}
impl Item for Label {
type Id = LabelId;
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
pub struct LabelId(u32);
impl Id for LabelId {
type RawId = usize;
fn from_offset(inner: usize) -> Self {
let index: u32 = inner.try_into().expect("LabelId to fit into a u32");
Self(index)
}
fn to_raw_id(&self) -> Self::RawId {
self.0 as Self::RawId
}
}
impl LabelId {
#[inline]
pub fn to_offset(&self) -> usize {
self.0 as usize
}
}
/// A canonical representation for sets of labels.
/// You should only use the impl functions to modify this.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
pub struct LabelSet {
// Guaranteed to be sorted by [Self::new]
sorted_labels: Box<[LabelId]>,
}
impl LabelSet {
pub fn iter(&self) -> core::slice::Iter<'_, LabelId> {
self.sorted_labels.iter()
}
pub fn new(mut v: Vec<LabelId>) -> Self {
v.sort_unstable();
let sorted_labels = v.into_boxed_slice();
Self { sorted_labels }
}
}
impl Item for LabelSet {
type Id = LabelSetId;
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[repr(transparent)]
#[cfg_attr(test, derive(bolero_generator::TypeGenerator))]
pub struct LabelSetId(u32);
impl Id for LabelSetId {
type RawId = usize;
fn from_offset(inner: usize) -> Self {
let index: u32 = inner.try_into().expect("LabelSetId to fit into a u32");
Self(index)
}
fn to_raw_id(&self) -> Self::RawId {
self.0 as Self::RawId
}
}
impl LabelSetId {
#[inline]
pub fn to_offset(&self) -> usize {
self.0 as usize
}
}
impl From<LabelSetId> for u32 {
fn from(value: LabelSetId) -> Self {
value.0
}
}