Skip to main content

confluent_kafka/consumer/
dyn_consumer.rs

1// Copyright 2026 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//! The dyn-compatible view of [`Consumer`].
16//!
17//! Has no Java counterpart (`definition-of-done.md` §7). A Java interface is
18//! always usable as a runtime-polymorphic type, so a `List<Consumer<K, V>>`
19//! can hold a `KafkaConsumer` next to a `MockConsumer`. The Rust
20//! [`Consumer`] trait returns `impl Future` from its asynchronous methods,
21//! which keeps statically dispatched calls free of a boxed future, but makes
22//! the trait itself not dyn-compatible. `DynConsumer` restores the Java
23//! capability (CLAUDE.md §3, "traits must be dyn-compatible") by boxing each
24//! future, and only callers that go through `dyn` pay for it. It is the
25//! consumer counterpart of [`DynProducer`](crate::producer::DynProducer).
26
27use std::collections::{HashMap, HashSet};
28use std::pin::Pin;
29use std::sync::Arc;
30use std::time::Duration;
31
32use crate::common::metrics::MetricsView;
33use crate::common::{Error, PartitionInfo, TopicPartition};
34use crate::consumer::{
35    CloseOptions, Consumer, ConsumerGroupMetadata, ConsumerHandle, ConsumerRebalanceListener, ConsumerRecords,
36    OffsetAndMetadata, OffsetAndTimestamp, OffsetCommitCallback, SubscriptionPattern,
37};
38
39type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
40
41mod private {
42    // Deliberately unnameable: it seals `DynConsumer` to `Consumer` implementors.
43    #[expect(unnameable_types)]
44    pub trait Sealed<K, V> {}
45}
46
47impl<K, V, C> private::Sealed<K, V> for C
48where
49    K: Send + 'static,
50    V: Send + 'static,
51    C: Consumer<K, V>,
52{
53}
54
55/// A dyn-compatible [`Consumer`], for holding different consumer
56/// implementations behind one type, e.g. `Box<dyn DynConsumer<K, V>>`.
57///
58/// Every [`Consumer`] implements `DynConsumer` automatically, and the trait is
59/// sealed, so it cannot be implemented directly: implement [`Consumer`]
60/// instead. Each method forwards to the [`Consumer`] method of the same name;
61/// asynchronous methods return the future boxed, which costs one allocation per
62/// call. `dyn DynConsumer<K, V>` implements [`Consumer`] in turn, so a boxed
63/// consumer can be passed as `&mut *boxed` to generic code taking
64/// `C: Consumer<K, V> + ?Sized`, and its methods can be called with only
65/// [`Consumer`] in scope.
66///
67/// With both traits in scope a method call on a concrete consumer (a
68/// `KafkaConsumer` or `MockConsumer`) is ambiguous, since both apply to it;
69/// call it as `Consumer::poll(&mut consumer, timeout)`, or import only one of
70/// the two traits. A call on a `dyn DynConsumer` resolves to `DynConsumer`'s
71/// own method either way.
72///
73/// ```
74/// use confluent_kafka::consumer::{DynConsumer, MockConsumer};
75///
76/// let consumers: Vec<Box<dyn DynConsumer<String, String>>> = vec![
77///     Box::new(MockConsumer::<String, String>::new("earliest").unwrap()),
78///     Box::new(MockConsumer::<String, String>::new("latest").unwrap()),
79/// ];
80/// assert_eq!(2, consumers.len());
81/// ```
82// the dyn-compatible companion of the `Consumer` trait (CLAUDE.md §3)
83#[doc(alias = "rust-only")]
84pub trait DynConsumer<K, V>: private::Sealed<K, V> + Send + 'static
85where
86    K: Send + 'static,
87    V: Send + 'static,
88{
89    /// See [`Consumer::assignment`].
90    ///
91    /// # Returns
92    ///
93    /// As [`Consumer::assignment`].
94    fn assignment(&mut self) -> HashSet<TopicPartition>;
95
96    /// See [`Consumer::subscription`].
97    ///
98    /// # Returns
99    ///
100    /// As [`Consumer::subscription`].
101    fn subscription(&mut self) -> HashSet<String>;
102
103    /// See [`Consumer::paused`].
104    ///
105    /// # Returns
106    ///
107    /// As [`Consumer::paused`].
108    fn paused(&mut self) -> HashSet<TopicPartition>;
109
110    /// See [`Consumer::group_metadata`].
111    ///
112    /// # Returns
113    ///
114    /// As [`Consumer::group_metadata`].
115    fn group_metadata(&mut self) -> Arc<dyn ConsumerGroupMetadata>;
116
117    /// See [`Consumer::current_lag`].
118    ///
119    /// # Arguments
120    ///
121    /// - `topic_partition` - as [`Consumer::current_lag`].
122    ///
123    /// # Returns
124    ///
125    /// As [`Consumer::current_lag`].
126    ///
127    /// # Errors
128    ///
129    /// As [`Consumer::current_lag`].
130    fn current_lag<'a>(&'a mut self, topic_partition: &'a TopicPartition) -> BoxFuture<'a, Result<Option<i64>, Error>>;
131
132    /// See [`Consumer::metrics`].
133    ///
134    /// # Returns
135    ///
136    /// As [`Consumer::metrics`].
137    fn metrics(&self) -> MetricsView;
138
139    /// See [`Consumer::subscribe_with_topics`].
140    ///
141    /// # Arguments
142    ///
143    /// - `topics` - as [`Consumer::subscribe_with_topics`].
144    ///
145    /// # Errors
146    ///
147    /// As [`Consumer::subscribe_with_topics`].
148    fn subscribe_with_topics<'a>(&'a mut self, topics: Vec<String>) -> BoxFuture<'a, Result<(), Error>>;
149
150    /// See [`Consumer::subscribe_with_topics_listener`].
151    ///
152    /// # Arguments
153    ///
154    /// - `topics` - as [`Consumer::subscribe_with_topics_listener`].
155    /// - `listener` - as [`Consumer::subscribe_with_topics_listener`].
156    ///
157    /// # Errors
158    ///
159    /// As [`Consumer::subscribe_with_topics_listener`].
160    fn subscribe_with_topics_listener<'a>(
161        &'a mut self,
162        topics: Vec<String>,
163        listener: Arc<dyn ConsumerRebalanceListener>,
164    ) -> BoxFuture<'a, Result<(), Error>>;
165
166    /// See [`Consumer::subscribe_with_pattern`].
167    ///
168    /// # Arguments
169    ///
170    /// - `pattern` - as [`Consumer::subscribe_with_pattern`].
171    ///
172    /// # Errors
173    ///
174    /// As [`Consumer::subscribe_with_pattern`].
175    fn subscribe_with_pattern<'a>(&'a mut self, pattern: SubscriptionPattern) -> BoxFuture<'a, Result<(), Error>>;
176
177    /// See [`Consumer::subscribe_with_pattern_listener`].
178    ///
179    /// # Arguments
180    ///
181    /// - `pattern` - as [`Consumer::subscribe_with_pattern_listener`].
182    /// - `listener` - as [`Consumer::subscribe_with_pattern_listener`].
183    ///
184    /// # Errors
185    ///
186    /// As [`Consumer::subscribe_with_pattern_listener`].
187    fn subscribe_with_pattern_listener<'a>(
188        &'a mut self,
189        pattern: SubscriptionPattern,
190        listener: Arc<dyn ConsumerRebalanceListener>,
191    ) -> BoxFuture<'a, Result<(), Error>>;
192
193    /// See [`Consumer::assign`].
194    ///
195    /// # Arguments
196    ///
197    /// - `partitions` - as [`Consumer::assign`].
198    ///
199    /// # Errors
200    ///
201    /// As [`Consumer::assign`].
202    fn assign<'a>(&'a mut self, partitions: Vec<TopicPartition>) -> BoxFuture<'a, Result<(), Error>>;
203
204    /// See [`Consumer::unsubscribe`].
205    ///
206    /// # Errors
207    ///
208    /// As [`Consumer::unsubscribe`].
209    fn unsubscribe<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>>;
210
211    /// See [`Consumer::poll`].
212    ///
213    /// # Arguments
214    ///
215    /// - `timeout` - as [`Consumer::poll`].
216    ///
217    /// # Returns
218    ///
219    /// As [`Consumer::poll`].
220    ///
221    /// # Errors
222    ///
223    /// As [`Consumer::poll`].
224    fn poll<'a>(&'a mut self, timeout: Duration) -> BoxFuture<'a, Result<ConsumerRecords<K, V>, Error>>;
225
226    /// See [`Consumer::commit_sync`].
227    ///
228    /// # Errors
229    ///
230    /// As [`Consumer::commit_sync`].
231    fn commit_sync<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>>;
232
233    /// See [`Consumer::commit_sync_with_timeout`].
234    ///
235    /// # Arguments
236    ///
237    /// - `timeout` - as [`Consumer::commit_sync_with_timeout`].
238    ///
239    /// # Errors
240    ///
241    /// As [`Consumer::commit_sync_with_timeout`].
242    fn commit_sync_with_timeout<'a>(&'a mut self, timeout: Duration) -> BoxFuture<'a, Result<(), Error>>;
243
244    /// See [`Consumer::commit_sync_with_offsets`].
245    ///
246    /// # Arguments
247    ///
248    /// - `offsets` - as [`Consumer::commit_sync_with_offsets`].
249    ///
250    /// # Errors
251    ///
252    /// As [`Consumer::commit_sync_with_offsets`].
253    fn commit_sync_with_offsets<'a>(
254        &'a mut self,
255        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
256    ) -> BoxFuture<'a, Result<(), Error>>;
257
258    /// See [`Consumer::commit_sync_with_offsets_timeout`].
259    ///
260    /// # Arguments
261    ///
262    /// - `offsets` - as [`Consumer::commit_sync_with_offsets_timeout`].
263    /// - `timeout` - as [`Consumer::commit_sync_with_offsets_timeout`].
264    ///
265    /// # Errors
266    ///
267    /// As [`Consumer::commit_sync_with_offsets_timeout`].
268    fn commit_sync_with_offsets_timeout<'a>(
269        &'a mut self,
270        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
271        timeout: Duration,
272    ) -> BoxFuture<'a, Result<(), Error>>;
273
274    /// See [`Consumer::commit_async`].
275    ///
276    /// # Errors
277    ///
278    /// As [`Consumer::commit_async`].
279    fn commit_async<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>>;
280
281    /// See [`Consumer::commit_async_with_callback`].
282    ///
283    /// # Arguments
284    ///
285    /// - `callback` - as [`Consumer::commit_async_with_callback`].
286    ///
287    /// # Errors
288    ///
289    /// As [`Consumer::commit_async_with_callback`].
290    fn commit_async_with_callback<'a>(
291        &'a mut self,
292        callback: Arc<dyn OffsetCommitCallback>,
293    ) -> BoxFuture<'a, Result<(), Error>>;
294
295    /// See [`Consumer::commit_async_with_offsets_callback`].
296    ///
297    /// # Arguments
298    ///
299    /// - `offsets` - as [`Consumer::commit_async_with_offsets_callback`].
300    /// - `callback` - as [`Consumer::commit_async_with_offsets_callback`].
301    ///
302    /// # Errors
303    ///
304    /// As [`Consumer::commit_async_with_offsets_callback`].
305    fn commit_async_with_offsets_callback<'a>(
306        &'a mut self,
307        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
308        callback: Arc<dyn OffsetCommitCallback>,
309    ) -> BoxFuture<'a, Result<(), Error>>;
310
311    /// See [`Consumer::seek_with_offset`].
312    ///
313    /// # Arguments
314    ///
315    /// - `partition` - as [`Consumer::seek_with_offset`].
316    /// - `offset` - as [`Consumer::seek_with_offset`].
317    ///
318    /// # Errors
319    ///
320    /// As [`Consumer::seek_with_offset`].
321    fn seek_with_offset<'a>(&'a mut self, partition: TopicPartition, offset: i64) -> BoxFuture<'a, Result<(), Error>>;
322
323    /// See [`Consumer::seek_with_offset_and_metadata`].
324    ///
325    /// # Arguments
326    ///
327    /// - `partition` - as [`Consumer::seek_with_offset_and_metadata`].
328    /// - `offset_and_metadata` - as [`Consumer::seek_with_offset_and_metadata`].
329    ///
330    /// # Errors
331    ///
332    /// As [`Consumer::seek_with_offset_and_metadata`].
333    fn seek_with_offset_and_metadata<'a>(
334        &'a mut self,
335        partition: TopicPartition,
336        offset_and_metadata: OffsetAndMetadata,
337    ) -> BoxFuture<'a, Result<(), Error>>;
338
339    /// See [`Consumer::seek_to_beginning`].
340    ///
341    /// # Arguments
342    ///
343    /// - `partitions` - as [`Consumer::seek_to_beginning`].
344    ///
345    /// # Errors
346    ///
347    /// As [`Consumer::seek_to_beginning`].
348    fn seek_to_beginning<'a>(&'a mut self, partitions: &'a [TopicPartition]) -> BoxFuture<'a, Result<(), Error>>;
349
350    /// See [`Consumer::seek_to_end`].
351    ///
352    /// # Arguments
353    ///
354    /// - `partitions` - as [`Consumer::seek_to_end`].
355    ///
356    /// # Errors
357    ///
358    /// As [`Consumer::seek_to_end`].
359    fn seek_to_end<'a>(&'a mut self, partitions: &'a [TopicPartition]) -> BoxFuture<'a, Result<(), Error>>;
360
361    /// See [`Consumer::position`].
362    ///
363    /// # Arguments
364    ///
365    /// - `partition` - as [`Consumer::position`].
366    ///
367    /// # Returns
368    ///
369    /// As [`Consumer::position`].
370    ///
371    /// # Errors
372    ///
373    /// As [`Consumer::position`].
374    fn position<'a>(&'a mut self, partition: &'a TopicPartition) -> BoxFuture<'a, Result<i64, Error>>;
375
376    /// See [`Consumer::position_with_timeout`].
377    ///
378    /// # Arguments
379    ///
380    /// - `partition` - as [`Consumer::position_with_timeout`].
381    /// - `timeout` - as [`Consumer::position_with_timeout`].
382    ///
383    /// # Returns
384    ///
385    /// As [`Consumer::position_with_timeout`].
386    ///
387    /// # Errors
388    ///
389    /// As [`Consumer::position_with_timeout`].
390    fn position_with_timeout<'a>(
391        &'a mut self,
392        partition: &'a TopicPartition,
393        timeout: Duration,
394    ) -> BoxFuture<'a, Result<i64, Error>>;
395
396    /// See [`Consumer::committed`].
397    ///
398    /// # Arguments
399    ///
400    /// - `partitions` - as [`Consumer::committed`].
401    ///
402    /// # Returns
403    ///
404    /// As [`Consumer::committed`].
405    ///
406    /// # Errors
407    ///
408    /// As [`Consumer::committed`].
409    fn committed<'a>(
410        &'a mut self,
411        partitions: &'a [TopicPartition],
412    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, Option<OffsetAndMetadata>>, Error>>;
413
414    /// See [`Consumer::committed_with_timeout`].
415    ///
416    /// # Arguments
417    ///
418    /// - `partitions` - as [`Consumer::committed_with_timeout`].
419    /// - `timeout` - as [`Consumer::committed_with_timeout`].
420    ///
421    /// # Returns
422    ///
423    /// As [`Consumer::committed_with_timeout`].
424    ///
425    /// # Errors
426    ///
427    /// As [`Consumer::committed_with_timeout`].
428    fn committed_with_timeout<'a>(
429        &'a mut self,
430        partitions: &'a [TopicPartition],
431        timeout: Duration,
432    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, Option<OffsetAndMetadata>>, Error>>;
433
434    /// See [`Consumer::partitions_for`].
435    ///
436    /// # Arguments
437    ///
438    /// - `topic` - as [`Consumer::partitions_for`].
439    ///
440    /// # Returns
441    ///
442    /// As [`Consumer::partitions_for`].
443    ///
444    /// # Errors
445    ///
446    /// As [`Consumer::partitions_for`].
447    fn partitions_for<'a>(&'a mut self, topic: &'a str) -> BoxFuture<'a, Result<Vec<PartitionInfo>, Error>>;
448
449    /// See [`Consumer::partitions_for_with_timeout`].
450    ///
451    /// # Arguments
452    ///
453    /// - `topic` - as [`Consumer::partitions_for_with_timeout`].
454    /// - `timeout` - as [`Consumer::partitions_for_with_timeout`].
455    ///
456    /// # Returns
457    ///
458    /// As [`Consumer::partitions_for_with_timeout`].
459    ///
460    /// # Errors
461    ///
462    /// As [`Consumer::partitions_for_with_timeout`].
463    fn partitions_for_with_timeout<'a>(
464        &'a mut self,
465        topic: &'a str,
466        timeout: Duration,
467    ) -> BoxFuture<'a, Result<Vec<PartitionInfo>, Error>>;
468
469    /// See [`Consumer::list_topics`].
470    ///
471    /// # Returns
472    ///
473    /// As [`Consumer::list_topics`].
474    ///
475    /// # Errors
476    ///
477    /// As [`Consumer::list_topics`].
478    fn list_topics<'a>(&'a mut self) -> BoxFuture<'a, Result<HashMap<String, Vec<PartitionInfo>>, Error>>;
479
480    /// See [`Consumer::list_topics_with_timeout`].
481    ///
482    /// # Arguments
483    ///
484    /// - `timeout` - as [`Consumer::list_topics_with_timeout`].
485    ///
486    /// # Returns
487    ///
488    /// As [`Consumer::list_topics_with_timeout`].
489    ///
490    /// # Errors
491    ///
492    /// As [`Consumer::list_topics_with_timeout`].
493    fn list_topics_with_timeout<'a>(
494        &'a mut self,
495        timeout: Duration,
496    ) -> BoxFuture<'a, Result<HashMap<String, Vec<PartitionInfo>>, Error>>;
497
498    /// See [`Consumer::offsets_for_times`].
499    ///
500    /// # Arguments
501    ///
502    /// - `timestamps_to_search` - as [`Consumer::offsets_for_times`].
503    ///
504    /// # Returns
505    ///
506    /// As [`Consumer::offsets_for_times`].
507    ///
508    /// # Errors
509    ///
510    /// As [`Consumer::offsets_for_times`].
511    fn offsets_for_times<'a>(
512        &'a mut self,
513        timestamps_to_search: HashMap<TopicPartition, i64>,
514    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, Option<OffsetAndTimestamp>>, Error>>;
515
516    /// See [`Consumer::offsets_for_times_with_timeout`].
517    ///
518    /// # Arguments
519    ///
520    /// - `timestamps_to_search` - as [`Consumer::offsets_for_times_with_timeout`].
521    /// - `timeout` - as [`Consumer::offsets_for_times_with_timeout`].
522    ///
523    /// # Returns
524    ///
525    /// As [`Consumer::offsets_for_times_with_timeout`].
526    ///
527    /// # Errors
528    ///
529    /// As [`Consumer::offsets_for_times_with_timeout`].
530    fn offsets_for_times_with_timeout<'a>(
531        &'a mut self,
532        timestamps_to_search: HashMap<TopicPartition, i64>,
533        timeout: Duration,
534    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, Option<OffsetAndTimestamp>>, Error>>;
535
536    /// See [`Consumer::beginning_offsets`].
537    ///
538    /// # Arguments
539    ///
540    /// - `partitions` - as [`Consumer::beginning_offsets`].
541    ///
542    /// # Returns
543    ///
544    /// As [`Consumer::beginning_offsets`].
545    ///
546    /// # Errors
547    ///
548    /// As [`Consumer::beginning_offsets`].
549    fn beginning_offsets<'a>(
550        &'a mut self,
551        partitions: &'a [TopicPartition],
552    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, i64>, Error>>;
553
554    /// See [`Consumer::beginning_offsets_with_timeout`].
555    ///
556    /// # Arguments
557    ///
558    /// - `partitions` - as [`Consumer::beginning_offsets_with_timeout`].
559    /// - `timeout` - as [`Consumer::beginning_offsets_with_timeout`].
560    ///
561    /// # Returns
562    ///
563    /// As [`Consumer::beginning_offsets_with_timeout`].
564    ///
565    /// # Errors
566    ///
567    /// As [`Consumer::beginning_offsets_with_timeout`].
568    fn beginning_offsets_with_timeout<'a>(
569        &'a mut self,
570        partitions: &'a [TopicPartition],
571        timeout: Duration,
572    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, i64>, Error>>;
573
574    /// See [`Consumer::end_offsets`].
575    ///
576    /// # Arguments
577    ///
578    /// - `partitions` - as [`Consumer::end_offsets`].
579    ///
580    /// # Returns
581    ///
582    /// As [`Consumer::end_offsets`].
583    ///
584    /// # Errors
585    ///
586    /// As [`Consumer::end_offsets`].
587    fn end_offsets<'a>(
588        &'a mut self,
589        partitions: &'a [TopicPartition],
590    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, i64>, Error>>;
591
592    /// See [`Consumer::end_offsets_with_timeout`].
593    ///
594    /// # Arguments
595    ///
596    /// - `partitions` - as [`Consumer::end_offsets_with_timeout`].
597    /// - `timeout` - as [`Consumer::end_offsets_with_timeout`].
598    ///
599    /// # Returns
600    ///
601    /// As [`Consumer::end_offsets_with_timeout`].
602    ///
603    /// # Errors
604    ///
605    /// As [`Consumer::end_offsets_with_timeout`].
606    fn end_offsets_with_timeout<'a>(
607        &'a mut self,
608        partitions: &'a [TopicPartition],
609        timeout: Duration,
610    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, i64>, Error>>;
611
612    /// See [`Consumer::pause`].
613    ///
614    /// # Arguments
615    ///
616    /// - `partitions` - as [`Consumer::pause`].
617    ///
618    /// # Errors
619    ///
620    /// As [`Consumer::pause`].
621    fn pause<'a>(&'a mut self, partitions: &'a [TopicPartition]) -> BoxFuture<'a, Result<(), Error>>;
622
623    /// See [`Consumer::resume`].
624    ///
625    /// # Arguments
626    ///
627    /// - `partitions` - as [`Consumer::resume`].
628    ///
629    /// # Errors
630    ///
631    /// As [`Consumer::resume`].
632    fn resume<'a>(&'a mut self, partitions: &'a [TopicPartition]) -> BoxFuture<'a, Result<(), Error>>;
633
634    /// See [`Consumer::enforce_rebalance`].
635    ///
636    /// # Errors
637    ///
638    /// As [`Consumer::enforce_rebalance`].
639    fn enforce_rebalance<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>>;
640
641    /// See [`Consumer::enforce_rebalance_with_reason`].
642    ///
643    /// # Arguments
644    ///
645    /// - `reason` - as [`Consumer::enforce_rebalance_with_reason`].
646    ///
647    /// # Errors
648    ///
649    /// As [`Consumer::enforce_rebalance_with_reason`].
650    fn enforce_rebalance_with_reason<'a>(&'a mut self, reason: &'a str) -> BoxFuture<'a, Result<(), Error>>;
651
652    /// See [`Consumer::close`].
653    ///
654    /// # Errors
655    ///
656    /// As [`Consumer::close`].
657    fn close<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>>;
658
659    /// See [`Consumer::close_with_options`].
660    ///
661    /// # Arguments
662    ///
663    /// - `options` - as [`Consumer::close_with_options`].
664    ///
665    /// # Errors
666    ///
667    /// As [`Consumer::close_with_options`].
668    fn close_with_options<'a>(&'a mut self, options: CloseOptions) -> BoxFuture<'a, Result<(), Error>>;
669
670    /// See [`Consumer::wakeup`].
671    fn wakeup(&self);
672
673    /// See [`Consumer::handle`].
674    ///
675    /// # Returns
676    ///
677    /// As [`Consumer::handle`].
678    fn handle(&self) -> ConsumerHandle;
679}
680
681impl<K, V, C> DynConsumer<K, V> for C
682where
683    K: Send + 'static,
684    V: Send + 'static,
685    C: Consumer<K, V>,
686{
687    fn assignment(&mut self) -> HashSet<TopicPartition> {
688        Consumer::assignment(self)
689    }
690
691    fn subscription(&mut self) -> HashSet<String> {
692        Consumer::subscription(self)
693    }
694
695    fn paused(&mut self) -> HashSet<TopicPartition> {
696        Consumer::paused(self)
697    }
698
699    fn group_metadata(&mut self) -> Arc<dyn ConsumerGroupMetadata> {
700        Consumer::group_metadata(self)
701    }
702
703    fn current_lag<'a>(&'a mut self, topic_partition: &'a TopicPartition) -> BoxFuture<'a, Result<Option<i64>, Error>> {
704        Box::pin(Consumer::current_lag(self, topic_partition))
705    }
706
707    fn metrics(&self) -> MetricsView {
708        Consumer::metrics(self)
709    }
710
711    fn subscribe_with_topics<'a>(&'a mut self, topics: Vec<String>) -> BoxFuture<'a, Result<(), Error>> {
712        Box::pin(Consumer::subscribe_with_topics(self, topics))
713    }
714
715    fn subscribe_with_topics_listener<'a>(
716        &'a mut self,
717        topics: Vec<String>,
718        listener: Arc<dyn ConsumerRebalanceListener>,
719    ) -> BoxFuture<'a, Result<(), Error>> {
720        Box::pin(Consumer::subscribe_with_topics_listener(self, topics, listener))
721    }
722
723    fn subscribe_with_pattern<'a>(&'a mut self, pattern: SubscriptionPattern) -> BoxFuture<'a, Result<(), Error>> {
724        Box::pin(Consumer::subscribe_with_pattern(self, pattern))
725    }
726
727    fn subscribe_with_pattern_listener<'a>(
728        &'a mut self,
729        pattern: SubscriptionPattern,
730        listener: Arc<dyn ConsumerRebalanceListener>,
731    ) -> BoxFuture<'a, Result<(), Error>> {
732        Box::pin(Consumer::subscribe_with_pattern_listener(self, pattern, listener))
733    }
734
735    fn assign<'a>(&'a mut self, partitions: Vec<TopicPartition>) -> BoxFuture<'a, Result<(), Error>> {
736        Box::pin(Consumer::assign(self, partitions))
737    }
738
739    fn unsubscribe<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>> {
740        Box::pin(Consumer::unsubscribe(self))
741    }
742
743    fn poll<'a>(&'a mut self, timeout: Duration) -> BoxFuture<'a, Result<ConsumerRecords<K, V>, Error>> {
744        Box::pin(Consumer::poll(self, timeout))
745    }
746
747    fn commit_sync<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>> {
748        Box::pin(Consumer::commit_sync(self))
749    }
750
751    fn commit_sync_with_timeout<'a>(&'a mut self, timeout: Duration) -> BoxFuture<'a, Result<(), Error>> {
752        Box::pin(Consumer::commit_sync_with_timeout(self, timeout))
753    }
754
755    fn commit_sync_with_offsets<'a>(
756        &'a mut self,
757        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
758    ) -> BoxFuture<'a, Result<(), Error>> {
759        Box::pin(Consumer::commit_sync_with_offsets(self, offsets))
760    }
761
762    fn commit_sync_with_offsets_timeout<'a>(
763        &'a mut self,
764        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
765        timeout: Duration,
766    ) -> BoxFuture<'a, Result<(), Error>> {
767        Box::pin(Consumer::commit_sync_with_offsets_timeout(self, offsets, timeout))
768    }
769
770    fn commit_async<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>> {
771        Box::pin(Consumer::commit_async(self))
772    }
773
774    fn commit_async_with_callback<'a>(
775        &'a mut self,
776        callback: Arc<dyn OffsetCommitCallback>,
777    ) -> BoxFuture<'a, Result<(), Error>> {
778        Box::pin(Consumer::commit_async_with_callback(self, callback))
779    }
780
781    fn commit_async_with_offsets_callback<'a>(
782        &'a mut self,
783        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
784        callback: Arc<dyn OffsetCommitCallback>,
785    ) -> BoxFuture<'a, Result<(), Error>> {
786        Box::pin(Consumer::commit_async_with_offsets_callback(self, offsets, callback))
787    }
788
789    fn seek_with_offset<'a>(&'a mut self, partition: TopicPartition, offset: i64) -> BoxFuture<'a, Result<(), Error>> {
790        Box::pin(Consumer::seek_with_offset(self, partition, offset))
791    }
792
793    fn seek_with_offset_and_metadata<'a>(
794        &'a mut self,
795        partition: TopicPartition,
796        offset_and_metadata: OffsetAndMetadata,
797    ) -> BoxFuture<'a, Result<(), Error>> {
798        Box::pin(Consumer::seek_with_offset_and_metadata(self, partition, offset_and_metadata))
799    }
800
801    fn seek_to_beginning<'a>(&'a mut self, partitions: &'a [TopicPartition]) -> BoxFuture<'a, Result<(), Error>> {
802        Box::pin(Consumer::seek_to_beginning(self, partitions))
803    }
804
805    fn seek_to_end<'a>(&'a mut self, partitions: &'a [TopicPartition]) -> BoxFuture<'a, Result<(), Error>> {
806        Box::pin(Consumer::seek_to_end(self, partitions))
807    }
808
809    fn position<'a>(&'a mut self, partition: &'a TopicPartition) -> BoxFuture<'a, Result<i64, Error>> {
810        Box::pin(Consumer::position(self, partition))
811    }
812
813    fn position_with_timeout<'a>(
814        &'a mut self,
815        partition: &'a TopicPartition,
816        timeout: Duration,
817    ) -> BoxFuture<'a, Result<i64, Error>> {
818        Box::pin(Consumer::position_with_timeout(self, partition, timeout))
819    }
820
821    fn committed<'a>(
822        &'a mut self,
823        partitions: &'a [TopicPartition],
824    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, Option<OffsetAndMetadata>>, Error>> {
825        Box::pin(Consumer::committed(self, partitions))
826    }
827
828    fn committed_with_timeout<'a>(
829        &'a mut self,
830        partitions: &'a [TopicPartition],
831        timeout: Duration,
832    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, Option<OffsetAndMetadata>>, Error>> {
833        Box::pin(Consumer::committed_with_timeout(self, partitions, timeout))
834    }
835
836    fn partitions_for<'a>(&'a mut self, topic: &'a str) -> BoxFuture<'a, Result<Vec<PartitionInfo>, Error>> {
837        Box::pin(Consumer::partitions_for(self, topic))
838    }
839
840    fn partitions_for_with_timeout<'a>(
841        &'a mut self,
842        topic: &'a str,
843        timeout: Duration,
844    ) -> BoxFuture<'a, Result<Vec<PartitionInfo>, Error>> {
845        Box::pin(Consumer::partitions_for_with_timeout(self, topic, timeout))
846    }
847
848    fn list_topics<'a>(&'a mut self) -> BoxFuture<'a, Result<HashMap<String, Vec<PartitionInfo>>, Error>> {
849        Box::pin(Consumer::list_topics(self))
850    }
851
852    fn list_topics_with_timeout<'a>(
853        &'a mut self,
854        timeout: Duration,
855    ) -> BoxFuture<'a, Result<HashMap<String, Vec<PartitionInfo>>, Error>> {
856        Box::pin(Consumer::list_topics_with_timeout(self, timeout))
857    }
858
859    fn offsets_for_times<'a>(
860        &'a mut self,
861        timestamps_to_search: HashMap<TopicPartition, i64>,
862    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, Option<OffsetAndTimestamp>>, Error>> {
863        Box::pin(Consumer::offsets_for_times(self, timestamps_to_search))
864    }
865
866    fn offsets_for_times_with_timeout<'a>(
867        &'a mut self,
868        timestamps_to_search: HashMap<TopicPartition, i64>,
869        timeout: Duration,
870    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, Option<OffsetAndTimestamp>>, Error>> {
871        Box::pin(Consumer::offsets_for_times_with_timeout(self, timestamps_to_search, timeout))
872    }
873
874    fn beginning_offsets<'a>(
875        &'a mut self,
876        partitions: &'a [TopicPartition],
877    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, i64>, Error>> {
878        Box::pin(Consumer::beginning_offsets(self, partitions))
879    }
880
881    fn beginning_offsets_with_timeout<'a>(
882        &'a mut self,
883        partitions: &'a [TopicPartition],
884        timeout: Duration,
885    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, i64>, Error>> {
886        Box::pin(Consumer::beginning_offsets_with_timeout(self, partitions, timeout))
887    }
888
889    fn end_offsets<'a>(
890        &'a mut self,
891        partitions: &'a [TopicPartition],
892    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, i64>, Error>> {
893        Box::pin(Consumer::end_offsets(self, partitions))
894    }
895
896    fn end_offsets_with_timeout<'a>(
897        &'a mut self,
898        partitions: &'a [TopicPartition],
899        timeout: Duration,
900    ) -> BoxFuture<'a, Result<HashMap<TopicPartition, i64>, Error>> {
901        Box::pin(Consumer::end_offsets_with_timeout(self, partitions, timeout))
902    }
903
904    fn pause<'a>(&'a mut self, partitions: &'a [TopicPartition]) -> BoxFuture<'a, Result<(), Error>> {
905        Box::pin(Consumer::pause(self, partitions))
906    }
907
908    fn resume<'a>(&'a mut self, partitions: &'a [TopicPartition]) -> BoxFuture<'a, Result<(), Error>> {
909        Box::pin(Consumer::resume(self, partitions))
910    }
911
912    fn enforce_rebalance<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>> {
913        Box::pin(Consumer::enforce_rebalance(self))
914    }
915
916    fn enforce_rebalance_with_reason<'a>(&'a mut self, reason: &'a str) -> BoxFuture<'a, Result<(), Error>> {
917        Box::pin(Consumer::enforce_rebalance_with_reason(self, reason))
918    }
919
920    fn close<'a>(&'a mut self) -> BoxFuture<'a, Result<(), Error>> {
921        Box::pin(Consumer::close(self))
922    }
923
924    fn close_with_options<'a>(&'a mut self, options: CloseOptions) -> BoxFuture<'a, Result<(), Error>> {
925        Box::pin(Consumer::close_with_options(self, options))
926    }
927
928    fn wakeup(&self) {
929        Consumer::wakeup(self)
930    }
931
932    fn handle(&self) -> ConsumerHandle {
933        Consumer::handle(self)
934    }
935}
936
937// On the trait object rather than on `Box<dyn DynConsumer>`: a boxed consumer
938// that implemented `Consumer` would also pick up the blanket `DynConsumer` impl,
939// and a method call on the box would then box every future twice.
940//
941// Methods taking a borrowed argument besides `self` are `async fn` rather than
942// returning the boxed future directly: that future lives for the shorter of the
943// two input lifetimes, which the trait's opaque return type cannot name.
944impl<K, V> Consumer<K, V> for dyn DynConsumer<K, V>
945where
946    K: Send + 'static,
947    V: Send + 'static,
948{
949    fn assignment(&mut self) -> HashSet<TopicPartition> {
950        DynConsumer::assignment(self)
951    }
952
953    fn subscription(&mut self) -> HashSet<String> {
954        DynConsumer::subscription(self)
955    }
956
957    fn paused(&mut self) -> HashSet<TopicPartition> {
958        DynConsumer::paused(self)
959    }
960
961    fn group_metadata(&mut self) -> Arc<dyn ConsumerGroupMetadata> {
962        DynConsumer::group_metadata(self)
963    }
964
965    async fn current_lag(&mut self, topic_partition: &TopicPartition) -> Result<Option<i64>, Error> {
966        DynConsumer::current_lag(self, topic_partition).await
967    }
968
969    fn metrics(&self) -> MetricsView {
970        DynConsumer::metrics(self)
971    }
972
973    fn subscribe_with_topics(&mut self, topics: Vec<String>) -> impl Future<Output = Result<(), Error>> + Send {
974        DynConsumer::subscribe_with_topics(self, topics)
975    }
976
977    fn subscribe_with_topics_listener(
978        &mut self,
979        topics: Vec<String>,
980        listener: Arc<dyn ConsumerRebalanceListener>,
981    ) -> impl Future<Output = Result<(), Error>> + Send {
982        DynConsumer::subscribe_with_topics_listener(self, topics, listener)
983    }
984
985    fn subscribe_with_pattern(
986        &mut self,
987        pattern: SubscriptionPattern,
988    ) -> impl Future<Output = Result<(), Error>> + Send {
989        DynConsumer::subscribe_with_pattern(self, pattern)
990    }
991
992    fn subscribe_with_pattern_listener(
993        &mut self,
994        pattern: SubscriptionPattern,
995        listener: Arc<dyn ConsumerRebalanceListener>,
996    ) -> impl Future<Output = Result<(), Error>> + Send {
997        DynConsumer::subscribe_with_pattern_listener(self, pattern, listener)
998    }
999
1000    fn assign(&mut self, partitions: Vec<TopicPartition>) -> impl Future<Output = Result<(), Error>> + Send {
1001        DynConsumer::assign(self, partitions)
1002    }
1003
1004    fn unsubscribe(&mut self) -> impl Future<Output = Result<(), Error>> + Send {
1005        DynConsumer::unsubscribe(self)
1006    }
1007
1008    fn poll(&mut self, timeout: Duration) -> impl Future<Output = Result<ConsumerRecords<K, V>, Error>> + Send {
1009        DynConsumer::poll(self, timeout)
1010    }
1011
1012    fn commit_sync(&mut self) -> impl Future<Output = Result<(), Error>> + Send {
1013        DynConsumer::commit_sync(self)
1014    }
1015
1016    fn commit_sync_with_timeout(&mut self, timeout: Duration) -> impl Future<Output = Result<(), Error>> + Send {
1017        DynConsumer::commit_sync_with_timeout(self, timeout)
1018    }
1019
1020    fn commit_sync_with_offsets(
1021        &mut self,
1022        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
1023    ) -> impl Future<Output = Result<(), Error>> + Send {
1024        DynConsumer::commit_sync_with_offsets(self, offsets)
1025    }
1026
1027    fn commit_sync_with_offsets_timeout(
1028        &mut self,
1029        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
1030        timeout: Duration,
1031    ) -> impl Future<Output = Result<(), Error>> + Send {
1032        DynConsumer::commit_sync_with_offsets_timeout(self, offsets, timeout)
1033    }
1034
1035    fn commit_async(&mut self) -> impl Future<Output = Result<(), Error>> + Send {
1036        DynConsumer::commit_async(self)
1037    }
1038
1039    fn commit_async_with_callback(
1040        &mut self,
1041        callback: Arc<dyn OffsetCommitCallback>,
1042    ) -> impl Future<Output = Result<(), Error>> + Send {
1043        DynConsumer::commit_async_with_callback(self, callback)
1044    }
1045
1046    fn commit_async_with_offsets_callback(
1047        &mut self,
1048        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
1049        callback: Arc<dyn OffsetCommitCallback>,
1050    ) -> impl Future<Output = Result<(), Error>> + Send {
1051        DynConsumer::commit_async_with_offsets_callback(self, offsets, callback)
1052    }
1053
1054    fn seek_with_offset(
1055        &mut self,
1056        partition: TopicPartition,
1057        offset: i64,
1058    ) -> impl Future<Output = Result<(), Error>> + Send {
1059        DynConsumer::seek_with_offset(self, partition, offset)
1060    }
1061
1062    fn seek_with_offset_and_metadata(
1063        &mut self,
1064        partition: TopicPartition,
1065        offset_and_metadata: OffsetAndMetadata,
1066    ) -> impl Future<Output = Result<(), Error>> + Send {
1067        DynConsumer::seek_with_offset_and_metadata(self, partition, offset_and_metadata)
1068    }
1069
1070    async fn seek_to_beginning(&mut self, partitions: &[TopicPartition]) -> Result<(), Error> {
1071        DynConsumer::seek_to_beginning(self, partitions).await
1072    }
1073
1074    async fn seek_to_end(&mut self, partitions: &[TopicPartition]) -> Result<(), Error> {
1075        DynConsumer::seek_to_end(self, partitions).await
1076    }
1077
1078    async fn position(&mut self, partition: &TopicPartition) -> Result<i64, Error> {
1079        DynConsumer::position(self, partition).await
1080    }
1081
1082    async fn position_with_timeout(&mut self, partition: &TopicPartition, timeout: Duration) -> Result<i64, Error> {
1083        DynConsumer::position_with_timeout(self, partition, timeout).await
1084    }
1085
1086    async fn committed(
1087        &mut self,
1088        partitions: &[TopicPartition],
1089    ) -> Result<HashMap<TopicPartition, Option<OffsetAndMetadata>>, Error> {
1090        DynConsumer::committed(self, partitions).await
1091    }
1092
1093    async fn committed_with_timeout(
1094        &mut self,
1095        partitions: &[TopicPartition],
1096        timeout: Duration,
1097    ) -> Result<HashMap<TopicPartition, Option<OffsetAndMetadata>>, Error> {
1098        DynConsumer::committed_with_timeout(self, partitions, timeout).await
1099    }
1100
1101    async fn partitions_for(&mut self, topic: &str) -> Result<Vec<PartitionInfo>, Error> {
1102        DynConsumer::partitions_for(self, topic).await
1103    }
1104
1105    async fn partitions_for_with_timeout(
1106        &mut self,
1107        topic: &str,
1108        timeout: Duration,
1109    ) -> Result<Vec<PartitionInfo>, Error> {
1110        DynConsumer::partitions_for_with_timeout(self, topic, timeout).await
1111    }
1112
1113    fn list_topics(&mut self) -> impl Future<Output = Result<HashMap<String, Vec<PartitionInfo>>, Error>> + Send {
1114        DynConsumer::list_topics(self)
1115    }
1116
1117    fn list_topics_with_timeout(
1118        &mut self,
1119        timeout: Duration,
1120    ) -> impl Future<Output = Result<HashMap<String, Vec<PartitionInfo>>, Error>> + Send {
1121        DynConsumer::list_topics_with_timeout(self, timeout)
1122    }
1123
1124    fn offsets_for_times(
1125        &mut self,
1126        timestamps_to_search: HashMap<TopicPartition, i64>,
1127    ) -> impl Future<Output = Result<HashMap<TopicPartition, Option<OffsetAndTimestamp>>, Error>> + Send {
1128        DynConsumer::offsets_for_times(self, timestamps_to_search)
1129    }
1130
1131    fn offsets_for_times_with_timeout(
1132        &mut self,
1133        timestamps_to_search: HashMap<TopicPartition, i64>,
1134        timeout: Duration,
1135    ) -> impl Future<Output = Result<HashMap<TopicPartition, Option<OffsetAndTimestamp>>, Error>> + Send {
1136        DynConsumer::offsets_for_times_with_timeout(self, timestamps_to_search, timeout)
1137    }
1138
1139    async fn beginning_offsets(
1140        &mut self,
1141        partitions: &[TopicPartition],
1142    ) -> Result<HashMap<TopicPartition, i64>, Error> {
1143        DynConsumer::beginning_offsets(self, partitions).await
1144    }
1145
1146    async fn beginning_offsets_with_timeout(
1147        &mut self,
1148        partitions: &[TopicPartition],
1149        timeout: Duration,
1150    ) -> Result<HashMap<TopicPartition, i64>, Error> {
1151        DynConsumer::beginning_offsets_with_timeout(self, partitions, timeout).await
1152    }
1153
1154    async fn end_offsets(&mut self, partitions: &[TopicPartition]) -> Result<HashMap<TopicPartition, i64>, Error> {
1155        DynConsumer::end_offsets(self, partitions).await
1156    }
1157
1158    async fn end_offsets_with_timeout(
1159        &mut self,
1160        partitions: &[TopicPartition],
1161        timeout: Duration,
1162    ) -> Result<HashMap<TopicPartition, i64>, Error> {
1163        DynConsumer::end_offsets_with_timeout(self, partitions, timeout).await
1164    }
1165
1166    async fn pause(&mut self, partitions: &[TopicPartition]) -> Result<(), Error> {
1167        DynConsumer::pause(self, partitions).await
1168    }
1169
1170    async fn resume(&mut self, partitions: &[TopicPartition]) -> Result<(), Error> {
1171        DynConsumer::resume(self, partitions).await
1172    }
1173
1174    fn enforce_rebalance(&mut self) -> impl Future<Output = Result<(), Error>> + Send {
1175        DynConsumer::enforce_rebalance(self)
1176    }
1177
1178    async fn enforce_rebalance_with_reason(&mut self, reason: &str) -> Result<(), Error> {
1179        DynConsumer::enforce_rebalance_with_reason(self, reason).await
1180    }
1181
1182    fn close(&mut self) -> impl Future<Output = Result<(), Error>> + Send {
1183        DynConsumer::close(self)
1184    }
1185
1186    fn close_with_options(&mut self, options: CloseOptions) -> impl Future<Output = Result<(), Error>> + Send {
1187        DynConsumer::close_with_options(self, options)
1188    }
1189
1190    fn wakeup(&self) {
1191        DynConsumer::wakeup(self)
1192    }
1193
1194    fn handle(&self) -> ConsumerHandle {
1195        DynConsumer::handle(self)
1196    }
1197}
1198
1199#[cfg(test)]
1200mod tests {
1201    use super::*;
1202    use crate::consumer::MockConsumer;
1203
1204    fn mock() -> MockConsumer<String, String> {
1205        MockConsumer::new("earliest").expect("earliest is a valid reset strategy")
1206    }
1207
1208    fn tp(partition: i32) -> TopicPartition {
1209        TopicPartition::new("topic", partition)
1210    }
1211
1212    async fn seek_and_position<C: Consumer<String, String> + ?Sized>(
1213        consumer: &mut C,
1214        partition: &TopicPartition,
1215    ) -> i64 {
1216        Consumer::seek_to_beginning(consumer, std::slice::from_ref(partition))
1217            .await
1218            .expect("seek_to_beginning");
1219        Consumer::position(consumer, partition).await.expect("position")
1220    }
1221
1222    /// A boxed consumer keeps the `Send + !Sync` shape of the consumers it
1223    /// holds (`consumer-threading.md` §2).
1224    #[test]
1225    fn dyn_consumer_is_send_and_not_sync() {
1226        use crate::test_thread_safety::{is_send, is_sync};
1227        assert!(is_send!(dyn DynConsumer<String, String>));
1228        assert!(!is_sync!(dyn DynConsumer<String, String>));
1229        assert!(is_send!(Box<dyn DynConsumer<String, String>>));
1230        assert!(!is_sync!(Box<dyn DynConsumer<String, String>>));
1231    }
1232
1233    #[tokio::test]
1234    async fn test_different_consumers_in_one_vec() {
1235        let mut first = mock();
1236        let mut second = mock();
1237        {
1238            let mut consumers: Vec<&mut dyn DynConsumer<String, String>> = vec![&mut first, &mut second];
1239            for (index, consumer) in consumers.iter_mut().enumerate() {
1240                let partition = i32::try_from(index).expect("two consumers");
1241                DynConsumer::assign(&mut **consumer, vec![tp(partition)]).await.expect("assign");
1242            }
1243        }
1244
1245        assert_eq!(HashSet::from([tp(0)]), Consumer::assignment(&mut first));
1246        assert_eq!(HashSet::from([tp(1)]), Consumer::assignment(&mut second));
1247    }
1248
1249    #[tokio::test]
1250    async fn test_boxed_consumers_in_one_vec() {
1251        let mut consumers: Vec<Box<dyn DynConsumer<String, String>>> = vec![Box::new(mock()), Box::new(mock())];
1252
1253        for consumer in &mut consumers {
1254            DynConsumer::assign(&mut **consumer, vec![tp(0)]).await.expect("assign");
1255            assert_eq!(HashSet::from([tp(0)]), DynConsumer::assignment(&mut **consumer));
1256            DynConsumer::close(&mut **consumer).await.expect("close");
1257        }
1258    }
1259
1260    #[tokio::test]
1261    async fn test_dyn_consumer_is_a_consumer() {
1262        let mut inner = mock();
1263        Consumer::assign(&mut inner, vec![tp(0)]).await.expect("assign");
1264        inner.update_beginning_offsets(HashMap::from([(tp(0), 5)]));
1265        let mut boxed: Box<dyn DynConsumer<String, String>> = Box::new(inner);
1266
1267        // Through the `Consumer` impl on `dyn DynConsumer`, including the
1268        // methods whose borrowed argument makes them `async fn`.
1269        assert_eq!(5, seek_and_position(&mut *boxed, &tp(0)).await);
1270        assert_eq!(HashSet::from([tp(0)]), Consumer::assignment(&mut *boxed));
1271    }
1272
1273    #[tokio::test]
1274    async fn test_errors_pass_through_unchanged() {
1275        let mut direct = mock();
1276        let mut through_dyn = mock();
1277
1278        let expected = Consumer::position(&mut direct, &tp(0))
1279            .await
1280            .expect_err("position of an unassigned partition");
1281        let as_dyn: &mut dyn DynConsumer<String, String> = &mut through_dyn;
1282        let error = DynConsumer::position(as_dyn, &tp(0))
1283            .await
1284            .expect_err("same call through DynConsumer");
1285        assert_eq!(expected.to_string(), error.to_string());
1286    }
1287
1288    #[tokio::test]
1289    async fn test_sync_methods_reach_the_consumer() {
1290        let mut inner = mock();
1291        {
1292            let as_dyn: &mut dyn DynConsumer<String, String> = &mut inner;
1293            DynConsumer::wakeup(as_dyn);
1294            let error = DynConsumer::poll(as_dyn, Duration::ZERO)
1295                .await
1296                .expect_err("a woken-up mock fails the next poll");
1297            assert!(matches!(error, Error::Wakeup(_)), "{error:?}");
1298            DynConsumer::close(as_dyn).await.expect("close");
1299        }
1300        assert!(inner.closed(), "close through DynConsumer closes the mock");
1301    }
1302
1303    #[tokio::test]
1304    async fn test_dyn_consumer_futures_can_be_spawned() {
1305        let mut inner = mock();
1306        inner.update_beginning_offsets(HashMap::from([(tp(0), 0)]));
1307        let mut boxed: Box<dyn DynConsumer<String, String>> = Box::new(inner);
1308        let task = tokio::spawn(async move {
1309            DynConsumer::assign(&mut *boxed, vec![tp(0)]).await?;
1310            DynConsumer::poll(&mut *boxed, Duration::ZERO)
1311                .await
1312                .map(|records| records.count())
1313        });
1314        assert_eq!(0, task.await.expect("join").expect("poll"));
1315    }
1316}