Skip to content

Commit

Permalink
derive-proto-encode: Update according to the changes in encoding/proto
Browse files Browse the repository at this point in the history
Signed-off-by: ackintosh <sora.akatsuki@gmail.com>
  • Loading branch information
ackintosh committed Aug 27, 2022
1 parent 15ef4e0 commit 182d7b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
20 changes: 6 additions & 14 deletions derive-proto-encode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ pub fn derive_encode(input: TokenStream) -> TokenStream {
.collect();

quote! {
impl<'a> prometheus_client::encoding::proto::EncodeLabels for &'a #name {
type Iterator = std::vec::IntoIter<prometheus_client::encoding::proto::Label>;

fn encode(self) -> Self::Iterator {
let mut labels = vec![];
impl prometheus_client::encoding::proto::EncodeLabels for #name {
fn encode(&self, labels: &mut Vec<prometheus_client::encoding::proto::Label>) {
#push_labels
labels.into_iter()
}
}
}
Expand All @@ -57,22 +53,18 @@ pub fn derive_encode(input: TokenStream) -> TokenStream {
let mut label = prometheus_client::encoding::proto::Label::default();
label.name = stringify!(#name).to_string();
label.value = stringify!(#ident).to_string();
label
labels.push(label);
}
}
})
.collect();

quote! {
impl<'a> prometheus_client::encoding::proto::EncodeLabels for &'a #name {
type Iterator = std::iter::Once<prometheus_client::encoding::proto::Label>;

fn encode(self) -> Self::Iterator {
let label = match self {
impl prometheus_client::encoding::proto::EncodeLabels for #name {
fn encode(&self, labels: &mut Vec<prometheus_client::encoding::proto::Label>) {
match self {
#match_arms
};

std::iter::once(label)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion derive-proto-encode/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ fn remap_keyword_identifiers() {
r#type: u64,
}

let labels = Labels { r#type: 42 }.encode().collect::<Vec<_>>();
let mut labels = vec![];

Labels { r#type: 42 }.encode(&mut labels);

assert_eq!("type", labels[0].name);
assert_eq!("42", labels[0].value);
Expand Down

0 comments on commit 182d7b0

Please sign in to comment.