madsim_rdkafka/std/
metadata.rs1use std::ffi::CStr;
4use std::slice;
5
6use rdkafka_sys as rdsys;
7use rdkafka_sys::types::*;
8
9use crate::error::IsError;
10use crate::util::{KafkaDrop, NativePtr};
11
12pub struct MetadataBroker(RDKafkaMetadataBroker);
14
15impl MetadataBroker {
16 pub fn id(&self) -> i32 {
18 self.0.id
19 }
20
21 pub fn host(&self) -> &str {
23 unsafe {
24 CStr::from_ptr(self.0.host)
25 .to_str()
26 .expect("Broker host is not a valid UTF-8 string")
27 }
28 }
29
30 pub fn port(&self) -> i32 {
32 self.0.port
33 }
34}
35
36pub struct MetadataPartition(RDKafkaMetadataPartition);
38
39impl MetadataPartition {
40 pub fn id(&self) -> i32 {
42 self.0.id
43 }
44
45 pub fn leader(&self) -> i32 {
47 self.0.leader
48 }
49
50 pub fn error(&self) -> Option<RDKafkaRespErr> {
54 if self.0.err.is_error() {
55 Some(self.0.err)
56 } else {
57 None
58 }
59 }
60
61 pub fn replicas(&self) -> &[i32] {
63 unsafe { slice::from_raw_parts(self.0.replicas, self.0.replica_cnt as usize) }
64 }
65
66 pub fn isr(&self) -> &[i32] {
68 unsafe { slice::from_raw_parts(self.0.isrs, self.0.isr_cnt as usize) }
69 }
70}
71
72pub struct MetadataTopic(RDKafkaMetadataTopic);
74
75impl MetadataTopic {
76 pub fn name(&self) -> &str {
78 unsafe {
79 CStr::from_ptr(self.0.topic)
80 .to_str()
81 .expect("Topic name is not a valid UTF-8 string")
82 }
83 }
84
85 pub fn partitions(&self) -> &[MetadataPartition] {
87 unsafe {
88 slice::from_raw_parts(
89 self.0.partitions as *const MetadataPartition,
90 self.0.partition_cnt as usize,
91 )
92 }
93 }
94
95 pub fn error(&self) -> Option<RDKafkaRespErr> {
98 if self.0.err.is_error() {
99 Some(self.0.err)
100 } else {
101 None
102 }
103 }
104}
105
106pub struct Metadata(NativePtr<RDKafkaMetadata>);
111
112unsafe impl KafkaDrop for RDKafkaMetadata {
113 const TYPE: &'static str = "metadata";
114 const DROP: unsafe extern "C" fn(*mut Self) = drop_metadata;
115}
116
117unsafe extern "C" fn drop_metadata(ptr: *mut RDKafkaMetadata) {
118 rdsys::rd_kafka_metadata_destroy(ptr as *const _)
119}
120
121impl Metadata {
122 pub(crate) unsafe fn from_ptr(ptr: *const RDKafkaMetadata) -> Metadata {
124 Metadata(NativePtr::from_ptr(ptr as *mut _).unwrap())
125 }
126
127 pub fn orig_broker_id(&self) -> i32 {
129 self.0.orig_broker_id
130 }
131
132 pub fn orig_broker_name(&self) -> &str {
134 unsafe {
135 CStr::from_ptr(self.0.orig_broker_name)
136 .to_str()
137 .expect("Broker name is not a valid UTF-8 string")
138 }
139 }
140
141 pub fn brokers(&self) -> &[MetadataBroker] {
143 unsafe {
144 slice::from_raw_parts(
145 self.0.brokers as *const MetadataBroker,
146 self.0.broker_cnt as usize,
147 )
148 }
149 }
150
151 pub fn topics(&self) -> &[MetadataTopic] {
153 unsafe {
154 slice::from_raw_parts(
155 self.0.topics as *const MetadataTopic,
156 self.0.topic_cnt as usize,
157 )
158 }
159 }
160}
161
162unsafe impl Send for Metadata {}
163unsafe impl Sync for Metadata {}