Skip to content

Commit

Permalink
Make Debug impl for newtypes passthrough to inner type
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Sep 12, 2024
1 parent 0426e4e commit b33f8ed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

- The Debug impl for new type wrappers now passes directly to the inner type.
The full list of new type wrappers is BrokerId, GroupId, ProducerId, TopicName and TransactionalId.
For example GroupId was previously `GroupId("some group")` but is now `"some group"`.

## v0.12.0

- Add `client` feature to enable encoding of requests and decoding of responses (enabled by default)
Expand Down
13 changes: 12 additions & 1 deletion protocol_codegen/src/generate_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ fn encode<T: Encodable>(encodable: &T, bytes: &mut bytes::BytesMut, version: i16

for entity_type in entity_types {
let mut derives = vec![
"Debug",
"Clone",
"Eq",
"PartialEq",
Expand Down Expand Up @@ -567,6 +566,18 @@ fn encode<T: Encodable>(encodable: &T, bytes: &mut bytes::BytesMut, version: i16
entity_type.name
)?;
writeln!(module_file, "}}")?;

writeln!(
module_file,
"impl std::fmt::Debug for {} {{",
entity_type.name
)?;
writeln!(
module_file,
" fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{ self.0.fmt(f) }}",
)?;
writeln!(module_file, "}}")?;

writeln!(
module_file,
"impl NewType<{}> for {} {{}}",
Expand Down
36 changes: 30 additions & 6 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,6 @@ impl Request for ListClientMetricsResourcesRequest {

/// Valid API keys in the Kafka protocol.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ApiKey {
/// API key for request ProduceRequest
ProduceKey = 0,
Expand Down Expand Up @@ -3375,7 +3374,7 @@ impl From<ListClientMetricsResourcesResponse> for ResponseKind {
}

/// The ID of the controller broker.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default, Copy)]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default, Copy)]
pub struct BrokerId(pub i32);

impl From<i32> for BrokerId {
Expand Down Expand Up @@ -3409,10 +3408,15 @@ impl std::cmp::PartialEq<BrokerId> for i32 {
self == &other.0
}
}
impl std::fmt::Debug for BrokerId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl NewType<i32> for BrokerId {}

/// The group id
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
pub struct GroupId(pub StrBytes);

impl From<StrBytes> for GroupId {
Expand Down Expand Up @@ -3446,10 +3450,15 @@ impl std::cmp::PartialEq<GroupId> for StrBytes {
self == &other.0
}
}
impl std::fmt::Debug for GroupId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl NewType<StrBytes> for GroupId {}

/// The first producer ID in this range, inclusive
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default, Copy)]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default, Copy)]
pub struct ProducerId(pub i64);

impl From<i64> for ProducerId {
Expand Down Expand Up @@ -3483,10 +3492,15 @@ impl std::cmp::PartialEq<ProducerId> for i64 {
self == &other.0
}
}
impl std::fmt::Debug for ProducerId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl NewType<i64> for ProducerId {}

///
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
pub struct TopicName(pub StrBytes);

impl From<StrBytes> for TopicName {
Expand Down Expand Up @@ -3520,10 +3534,15 @@ impl std::cmp::PartialEq<TopicName> for StrBytes {
self == &other.0
}
}
impl std::fmt::Debug for TopicName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl NewType<StrBytes> for TopicName {}

///
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
pub struct TransactionalId(pub StrBytes);

impl From<StrBytes> for TransactionalId {
Expand Down Expand Up @@ -3557,4 +3576,9 @@ impl std::cmp::PartialEq<TransactionalId> for StrBytes {
self == &other.0
}
}
impl std::fmt::Debug for TransactionalId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl NewType<StrBytes> for TransactionalId {}

0 comments on commit b33f8ed

Please sign in to comment.