1use std::collections::HashMap;
32use std::pin::Pin;
33use std::time::Duration;
34
35use crate::common::Error;
36use crate::common::KafkaFuture;
37use crate::common::PartitionInfo;
38use crate::common::TopicPartition;
39use crate::common::metrics::MetricsView;
40use crate::consumer::ConsumerGroupMetadata;
41use crate::consumer::OffsetAndMetadata;
42use crate::producer::Callback;
43#[cfg(feature = "ffi")]
44use crate::producer::KafkaProducer;
45#[cfg(feature = "ffi")]
46use crate::producer::MockProducer;
47use crate::producer::Producer;
48use crate::producer::ProducerRecord;
49use crate::producer::RecordMetadata;
50#[cfg(feature = "ffi")]
51use crate::producer::TrySendOutcome;
52
53type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
54
55mod private {
56 #[expect(unnameable_types)]
58 pub trait Sealed<K, V> {}
59}
60
61impl<K, V, P: super::Producer<K, V>> private::Sealed<K, V> for P {}
62
63#[doc(alias = "rust-only")]
89pub trait DynProducer<K, V>: private::Sealed<K, V> + Send + Sync {
90 fn init_transactions<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
96 where
97 K: 'a,
98 V: 'a;
99
100 fn begin_transaction(&self) -> Result<(), Error>;
106
107 fn send_offsets_to_transaction<'a>(
118 &'a self,
119 offsets: HashMap<TopicPartition, OffsetAndMetadata>,
120 group_metadata: &'a dyn ConsumerGroupMetadata,
121 ) -> BoxFuture<'a, Result<(), Error>>
122 where
123 K: 'a,
124 V: 'a;
125
126 fn commit_transaction<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
132 where
133 K: 'a,
134 V: 'a;
135
136 fn abort_transaction<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
142 where
143 K: 'a,
144 V: 'a;
145
146 fn send<'a>(&'a self, record: ProducerRecord<K, V>) -> BoxFuture<'a, Result<KafkaFuture<RecordMetadata>, Error>>
160 where
161 K: 'a,
162 V: 'a;
163
164 fn send_with_callback<'a>(
179 &'a self,
180 record: ProducerRecord<K, V>,
181 callback: Option<Box<dyn Callback>>,
182 ) -> BoxFuture<'a, Result<KafkaFuture<RecordMetadata>, Error>>
183 where
184 K: 'a,
185 V: 'a;
186
187 fn flush<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
193 where
194 K: 'a,
195 V: 'a;
196
197 fn partitions_for<'a>(&'a self, topic: &'a str) -> BoxFuture<'a, Result<Vec<PartitionInfo>, Error>>
211 where
212 K: 'a,
213 V: 'a;
214
215 fn metrics(&self) -> MetricsView;
221
222 fn close<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
228 where
229 K: 'a,
230 V: 'a;
231
232 fn close_with_timeout<'a>(&'a self, timeout: Duration) -> BoxFuture<'a, Result<(), Error>>
242 where
243 K: 'a,
244 V: 'a;
245}
246
247impl<K, V, P: Producer<K, V>> DynProducer<K, V> for P {
248 fn init_transactions<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
249 where
250 K: 'a,
251 V: 'a,
252 {
253 Box::pin(Producer::init_transactions(self))
254 }
255
256 fn begin_transaction(&self) -> Result<(), Error> {
257 Producer::begin_transaction(self)
258 }
259
260 fn send_offsets_to_transaction<'a>(
261 &'a self,
262 offsets: HashMap<TopicPartition, OffsetAndMetadata>,
263 group_metadata: &'a dyn ConsumerGroupMetadata,
264 ) -> BoxFuture<'a, Result<(), Error>>
265 where
266 K: 'a,
267 V: 'a,
268 {
269 Box::pin(Producer::send_offsets_to_transaction(self, offsets, group_metadata))
270 }
271
272 fn commit_transaction<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
273 where
274 K: 'a,
275 V: 'a,
276 {
277 Box::pin(Producer::commit_transaction(self))
278 }
279
280 fn abort_transaction<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
281 where
282 K: 'a,
283 V: 'a,
284 {
285 Box::pin(Producer::abort_transaction(self))
286 }
287
288 fn send<'a>(&'a self, record: ProducerRecord<K, V>) -> BoxFuture<'a, Result<KafkaFuture<RecordMetadata>, Error>>
289 where
290 K: 'a,
291 V: 'a,
292 {
293 Box::pin(Producer::send(self, record))
294 }
295
296 fn send_with_callback<'a>(
297 &'a self,
298 record: ProducerRecord<K, V>,
299 callback: Option<Box<dyn Callback>>,
300 ) -> BoxFuture<'a, Result<KafkaFuture<RecordMetadata>, Error>>
301 where
302 K: 'a,
303 V: 'a,
304 {
305 Box::pin(Producer::send_with_callback(self, record, callback))
306 }
307
308 fn flush<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
309 where
310 K: 'a,
311 V: 'a,
312 {
313 Box::pin(Producer::flush(self))
314 }
315
316 fn partitions_for<'a>(&'a self, topic: &'a str) -> BoxFuture<'a, Result<Vec<PartitionInfo>, Error>>
317 where
318 K: 'a,
319 V: 'a,
320 {
321 Box::pin(Producer::partitions_for(self, topic))
322 }
323
324 fn metrics(&self) -> MetricsView {
325 Producer::metrics(self)
326 }
327
328 fn close<'a>(&'a self) -> BoxFuture<'a, Result<(), Error>>
329 where
330 K: 'a,
331 V: 'a,
332 {
333 Box::pin(Producer::close(self))
334 }
335
336 fn close_with_timeout<'a>(&'a self, timeout: Duration) -> BoxFuture<'a, Result<(), Error>>
337 where
338 K: 'a,
339 V: 'a,
340 {
341 Box::pin(Producer::close_with_timeout(self, timeout))
342 }
343}
344
345impl<K, V> Producer<K, V> for dyn DynProducer<K, V> + '_ {
349 fn init_transactions(&self) -> impl Future<Output = Result<(), Error>> + Send {
350 DynProducer::init_transactions(self)
351 }
352
353 fn begin_transaction(&self) -> Result<(), Error> {
354 DynProducer::begin_transaction(self)
355 }
356
357 async fn send_offsets_to_transaction(
361 &self,
362 offsets: HashMap<TopicPartition, OffsetAndMetadata>,
363 group_metadata: &dyn ConsumerGroupMetadata,
364 ) -> Result<(), Error> {
365 DynProducer::send_offsets_to_transaction(self, offsets, group_metadata).await
366 }
367
368 fn commit_transaction(&self) -> impl Future<Output = Result<(), Error>> + Send {
369 DynProducer::commit_transaction(self)
370 }
371
372 fn abort_transaction(&self) -> impl Future<Output = Result<(), Error>> + Send {
373 DynProducer::abort_transaction(self)
374 }
375
376 fn send(
377 &self,
378 record: ProducerRecord<K, V>,
379 ) -> impl Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send {
380 DynProducer::send(self, record)
381 }
382
383 fn send_with_callback(
384 &self,
385 record: ProducerRecord<K, V>,
386 callback: Option<Box<dyn Callback>>,
387 ) -> impl Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send {
388 DynProducer::send_with_callback(self, record, callback)
389 }
390
391 fn flush(&self) -> impl Future<Output = Result<(), Error>> + Send {
392 DynProducer::flush(self)
393 }
394
395 async fn partitions_for(&self, topic: &str) -> Result<Vec<PartitionInfo>, Error> {
399 DynProducer::partitions_for(self, topic).await
400 }
401
402 fn metrics(&self) -> MetricsView {
403 DynProducer::metrics(self)
404 }
405
406 fn close(&self) -> impl Future<Output = Result<(), Error>> + Send {
407 DynProducer::close(self)
408 }
409
410 fn close_with_timeout(&self, timeout: Duration) -> impl Future<Output = Result<(), Error>> + Send {
411 DynProducer::close_with_timeout(self, timeout)
412 }
413}
414
415#[cfg(feature = "ffi")]
432pub(crate) trait DynTrySend<K, V>: DynProducer<K, V> {
433 fn try_send<'a>(
435 &'a self,
436 record: ProducerRecord<K, V>,
437 callback: Option<Box<dyn Callback>>,
438 ) -> BoxFuture<'a, Result<TrySendOutcome, Error>>
439 where
440 K: 'a,
441 V: 'a;
442
443 fn has_unclosed_sender(&self) -> bool;
447}
448
449#[cfg(feature = "ffi")]
450impl<K, V> DynTrySend<K, V> for KafkaProducer<K, V>
451where
452 K: Send + Sync,
453 V: Send + Sync,
454{
455 fn try_send<'a>(
456 &'a self,
457 record: ProducerRecord<K, V>,
458 callback: Option<Box<dyn Callback>>,
459 ) -> BoxFuture<'a, Result<TrySendOutcome, Error>>
460 where
461 K: 'a,
462 V: 'a,
463 {
464 Box::pin(KafkaProducer::try_send(self, record, callback))
465 }
466
467 fn has_unclosed_sender(&self) -> bool {
468 self.is_running()
469 }
470}
471
472#[cfg(feature = "ffi")]
476impl<K, V> DynTrySend<K, V> for MockProducer<K, V>
477where
478 K: Send + Sync,
479 V: Send + Sync,
480{
481 fn try_send<'a>(
482 &'a self,
483 record: ProducerRecord<K, V>,
484 callback: Option<Box<dyn Callback>>,
485 ) -> BoxFuture<'a, Result<TrySendOutcome, Error>>
486 where
487 K: 'a,
488 V: 'a,
489 {
490 Box::pin(async move {
491 Producer::send_with_callback(self, record, callback)
492 .await
493 .map(TrySendOutcome::Accepted)
494 })
495 }
496
497 fn has_unclosed_sender(&self) -> bool {
498 false
499 }
500}
501
502#[cfg(test)]
503mod tests {
504 use super::*;
505 use crate::producer::MockProducer;
506 use std::sync::Arc;
507
508 fn record(value: &str) -> ProducerRecord<String, String> {
509 ProducerRecord::with_key("topic".to_string(), Some("key".to_string()), Some(value.to_string()))
510 }
511
512 #[cfg(feature = "ffi")]
516 #[tokio::test]
517 async fn test_mock_try_send_is_always_accepted() {
518 let mock = MockProducer::<String, String>::with_auto_complete(true);
519 let producer: &dyn DynTrySend<String, String> = &mock;
520
521 let outcome = producer.try_send(record("value"), None).await.expect("try_send");
522
523 let future = match outcome {
524 TrySendOutcome::Accepted(future) => future,
525 other => panic!("expected Accepted, got {other:?}"),
526 };
527 assert_eq!(future.get().await.expect("metadata").offset(), 0);
528 assert_eq!(mock.history().len(), 1);
529 }
530
531 async fn send_through_generic<P: Producer<String, String> + ?Sized>(producer: &P, value: &str) -> i64 {
532 let future = Producer::send(producer, record(value)).await.expect("send");
533 future.get().await.expect("metadata").offset()
534 }
535
536 #[tokio::test]
537 async fn test_different_producers_in_one_vec() {
538 let auto = MockProducer::<String, String>::with_auto_complete(true);
539 let manual = MockProducer::<String, String>::with_auto_complete(false);
540 let producers: Vec<&dyn DynProducer<String, String>> = vec![&auto, &manual];
541
542 for (index, producer) in producers.iter().enumerate() {
543 DynProducer::send(*producer, record(&format!("value{index}")))
544 .await
545 .expect("send");
546 }
547
548 assert_eq!(vec![record("value0")], auto.history());
549 assert_eq!(vec![record("value1")], manual.history());
550 assert!(manual.complete_next(), "the manual mock still holds the uncompleted send");
551 }
552
553 #[tokio::test]
554 async fn test_boxed_producers_in_one_vec() {
555 let producers: Vec<Box<dyn DynProducer<String, String>>> = vec![
556 Box::new(MockProducer::<String, String>::with_auto_complete(true)),
557 Box::new(MockProducer::<String, String>::with_auto_complete(true)),
558 ];
559
560 for producer in &producers {
561 let future = DynProducer::send(&**producer, record("value0")).await.expect("send");
562 assert!(future.is_done(), "an auto-complete mock completes the send immediately");
563 DynProducer::flush(&**producer).await.expect("flush");
564 }
565 }
566
567 #[tokio::test]
568 async fn test_dyn_producer_is_a_producer() {
569 let boxed: Box<dyn DynProducer<String, String>> =
570 Box::new(MockProducer::<String, String>::with_auto_complete(true));
571
572 assert_eq!(0, send_through_generic(&*boxed, "value0").await);
573 assert_eq!(1, send_through_generic(&*boxed, "value1").await);
574 }
575
576 #[test]
577 fn test_errors_pass_through_unchanged() {
578 let mock = MockProducer::<String, String>::with_auto_complete(true);
579 let as_dyn: &dyn DynProducer<String, String> = &mock;
580
581 let error = DynProducer::begin_transaction(as_dyn).expect_err("begin_transaction before init_transactions");
582 let expected = Producer::begin_transaction(&mock).expect_err("same call on the mock itself");
583 assert_eq!(expected.to_string(), error.to_string());
584 }
585
586 #[tokio::test]
587 async fn test_dyn_producer_futures_can_be_spawned() {
588 let shared: Arc<dyn DynProducer<String, String>> =
589 Arc::new(MockProducer::<String, String>::with_auto_complete(true));
590 let task = tokio::spawn({
591 let shared = shared.clone();
592 async move { DynProducer::send(&*shared, record("value0")).await.map(|_| ()) }
593 });
594 task.await.expect("join").expect("send");
595 }
596}