confluent_kafka/common/
topic_partition_replica.rs1use std::fmt;
18
19#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21#[doc(alias = "org.apache.kafka.common.TopicPartitionReplica")]
22pub struct TopicPartitionReplica {
23 broker_id: i32,
24 partition: i32,
25 topic: String,
26}
27
28impl TopicPartitionReplica {
29 #[doc(alias = "org.apache.kafka.common.TopicPartitionReplica#TopicPartitionReplica")]
37 pub fn new(topic: impl Into<String>, partition: i32, broker_id: i32) -> Self {
38 Self { broker_id, partition, topic: topic.into() }
39 }
40
41 #[doc(alias = "org.apache.kafka.common.TopicPartitionReplica#topic")]
43 pub fn topic(&self) -> &str {
44 &self.topic
45 }
46
47 #[doc(alias = "org.apache.kafka.common.TopicPartitionReplica#partition")]
49 pub fn partition(&self) -> i32 {
50 self.partition
51 }
52
53 #[doc(alias = "org.apache.kafka.common.TopicPartitionReplica#brokerId")]
55 pub fn broker_id(&self) -> i32 {
56 self.broker_id
57 }
58}
59
60impl fmt::Display for TopicPartitionReplica {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 write!(f, "{}-{}-{}", self.topic, self.partition, self.broker_id)
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69 use std::collections::HashSet;
70
71 #[test]
72 fn accessors() {
73 let tpr = TopicPartitionReplica::new("topic", 12, 1);
74 assert_eq!(tpr.topic(), "topic");
75 assert_eq!(tpr.partition(), 12);
76 assert_eq!(tpr.broker_id(), 1);
77 }
78
79 #[test]
80 fn equality_and_hash() {
81 let a = TopicPartitionReplica::new("topic", 12, 1);
82 let b = TopicPartitionReplica::new("topic", 12, 1);
83 let c = TopicPartitionReplica::new("topic", 12, 2);
84 let d = TopicPartitionReplica::new("other", 12, 1);
85 assert_eq!(a, b);
86 assert_ne!(a, c);
87 assert_ne!(a, d);
88
89 let mut set = HashSet::new();
90 set.insert(a.clone());
91 set.insert(b);
92 assert_eq!(set.len(), 1);
93 set.insert(c);
94 assert_eq!(set.len(), 2);
95 }
96
97 #[test]
99 fn display_format() {
100 let tpr = TopicPartitionReplica::new("topic", 12, 3);
101 assert_eq!(tpr.to_string(), "topic-12-3");
102 }
103}