Skip to content

Commit ca0f583

Browse files
divagant-martianackintosh
authored andcommitted
misc fixes
Signed-off-by: ackintosh <[email protected]>
1 parent 639cb0a commit ca0f583

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ harness = false
4646
name = "text"
4747
path = "benches/encoding/text.rs"
4848
harness = false
49+
required-features = []
4950

5051
[[bench]]
5152
name = "proto"
5253
path = "benches/encoding/proto.rs"
5354
harness = false
55+
required-features = ["protobuf"]

benches/encoding/proto.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Benchmark inspired by https://github.com/tikv/rust-prometheus/blob/ab1ca7285d3463504381a5025ae1951e020d6796/benches/text_encoder.rs
1+
// Benchmark inspired by
2+
// https://github.com/tikv/rust-prometheus/blob/ab1ca7285d3463504381a5025ae1951e020d6796/benches/text_encoder.rs:write
23

34
use criterion::{black_box, criterion_group, criterion_main, Criterion};
4-
use prometheus_client::encoding::proto::{encode, Encode, EncodeMetric};
5+
use prometheus_client::encoding::proto::{encode, EncodeMetric};
6+
use prometheus_client::encoding::Encode;
57
use prometheus_client::metrics::counter::Counter;
68
use prometheus_client::metrics::family::Family;
79
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
@@ -17,7 +19,7 @@ pub fn proto(c: &mut Criterion) {
1719
some_number: u64,
1820
}
1921

20-
#[derive(Clone, Hash, PartialEq, Eq)]
22+
#[derive(Clone, Hash, PartialEq, Eq, Encode)]
2123
enum Method {
2224
Get,
2325
#[allow(dead_code)]

benches/encoding/text.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use criterion::{black_box, criterion_group, criterion_main, Criterion};
44
use prometheus_client::encoding::text::{encode, Encode, EncodeMetric};
5+
use prometheus_client::encoding::Encode;
56
use prometheus_client::metrics::counter::Counter;
67
use prometheus_client::metrics::family::Family;
78
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
@@ -33,6 +34,26 @@ pub fn text(c: &mut Criterion) {
3334
Five,
3435
}
3536

37+
#[cfg(feature = "protobuf")]
38+
impl std::fmt::Display for Method {
39+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40+
match self {
41+
Method::Get => f.write_str("Get"),
42+
Method::Put => f.write_str("Put"),
43+
}
44+
}
45+
}
46+
47+
#[cfg(feature = "protobuf")]
48+
impl std::fmt::Display for Status {
49+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50+
match self {
51+
Status::Two => f.write_str("2"),
52+
Status::Four => f.write_str("4"),
53+
Status::Five => f.write_str("5"),
54+
}
55+
}
56+
}
3657
impl Encode for Status {
3758
fn encode(&self, writer: &mut dyn Write) -> Result<(), std::io::Error> {
3859
let status = match self {

derive-encode/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn derive_encode(input: TokenStream) -> TokenStream {
6161
syn::Data::Union(_) => panic!("Can not derive Encode for union."),
6262
};
6363

64-
let mut gen = quote! {
64+
let gen = quote! {
6565
impl prometheus_client::encoding::text::Encode for #name {
6666
fn encode(&self, writer: &mut dyn std::io::Write) -> std::result::Result<(), std::io::Error> {
6767
#body
@@ -71,18 +71,20 @@ pub fn derive_encode(input: TokenStream) -> TokenStream {
7171
}
7272
};
7373

74-
if cfg!(feature = "protobuf") {
74+
#[cfg(feature = "protobuf")]
75+
let gen = {
7576
let protobuf = derive_protobuf_encode(ast);
76-
gen = quote! {
77+
quote! {
7778
#gen
7879

7980
#protobuf
8081
}
81-
}
82+
};
8283

8384
gen.into()
8485
}
8586

87+
#[cfg(feature = "protobuf")]
8688
fn derive_protobuf_encode(ast: DeriveInput) -> TokenStream2 {
8789
let name = &ast.ident;
8890

derive-encode/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use prometheus_client::encoding::text::{encode, Encode};
2+
use prometheus_client::encoding::Encode;
23
use prometheus_client::metrics::counter::Counter;
34
use prometheus_client::metrics::family::Family;
45
use prometheus_client::registry::Registry;

0 commit comments

Comments
 (0)