Skip to main content

confluent_kafka/common/
topic_partition.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! A topic name and partition number.
16
17use std::fmt;
18use std::sync::Arc;
19
20/// A topic name and partition number.
21#[derive(Clone, Debug, Eq)]
22#[doc(alias = "org.apache.kafka.common.TopicPartition")]
23pub struct TopicPartition {
24    partition: i32,
25    topic: Arc<str>,
26}
27
28impl TopicPartition {
29    /// Creates a new `TopicPartition` with the given topic and partition.
30    ///
31    /// # Arguments
32    ///
33    /// - `topic` - the topic name
34    /// - `partition` - the partition number
35    #[doc(alias = "org.apache.kafka.common.TopicPartition#TopicPartition")]
36    pub fn new(topic: impl Into<Arc<str>>, partition: i32) -> Self {
37        Self { partition, topic: topic.into() }
38    }
39
40    /// Returns the partition number.
41    #[doc(alias = "org.apache.kafka.common.TopicPartition#partition")]
42    pub fn partition(&self) -> i32 {
43        self.partition
44    }
45
46    /// Returns the topic name.
47    #[doc(alias = "org.apache.kafka.common.TopicPartition#topic")]
48    pub fn topic(&self) -> &str {
49        &self.topic
50    }
51
52    /// Returns a shared reference to the topic name.
53    pub(crate) fn topic_arc(&self) -> &Arc<str> {
54        &self.topic
55    }
56}
57
58impl PartialEq for TopicPartition {
59    fn eq(&self, other: &Self) -> bool {
60        self.partition == other.partition && self.topic == other.topic
61    }
62}
63
64impl std::hash::Hash for TopicPartition {
65    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
66        self.partition.hash(state);
67        self.topic.hash(state);
68    }
69}
70
71impl fmt::Display for TopicPartition {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        write!(f, "{}-{}", self.topic, self.partition)
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_topic_partition_creation() {
83        let tp = TopicPartition::new("mytopic".to_string(), 5);
84        assert_eq!(tp.partition(), 5);
85        assert_eq!(tp.topic(), "mytopic");
86    }
87
88    #[test]
89    fn test_topic_partition_equality() {
90        let tp1 = TopicPartition::new("test".to_string(), 0);
91        let tp2 = TopicPartition::new("test".to_string(), 0);
92        let tp3 = TopicPartition::new("test".to_string(), 1);
93        let tp4 = TopicPartition::new("other".to_string(), 0);
94
95        assert_eq!(tp1, tp2);
96        assert_ne!(tp1, tp3);
97        assert_ne!(tp1, tp4);
98    }
99
100    #[test]
101    fn test_topic_partition_hash() {
102        use std::collections::HashSet;
103        let mut set = HashSet::new();
104        set.insert(TopicPartition::new("test".to_string(), 0));
105        set.insert(TopicPartition::new("test".to_string(), 0));
106        assert_eq!(set.len(), 1);
107
108        set.insert(TopicPartition::new("test".to_string(), 1));
109        assert_eq!(set.len(), 2);
110    }
111
112    #[test]
113    fn test_topic_partition_display() {
114        let tp = TopicPartition::new("mytopic".to_string(), 5);
115        assert_eq!(tp.to_string(), "mytopic-5");
116    }
117}