use std::sync::Arc;
use super::{
super::state::AssignedConsumerShardState, AssignedConsumerDelivery, AssignedConsumerRecords,
};
#[must_use = "dropping the batch returns its bounded delivery lease"]
pub struct AssignedConsumerBatch {
delivery: Option<AssignedConsumerDelivery>,
return_to: Arc<AssignedConsumerShardState>,
}
impl AssignedConsumerBatch {
pub(in crate::consumer::assigned_host) const fn new(
delivery: AssignedConsumerDelivery,
return_to: Arc<AssignedConsumerShardState>,
) -> Self {
Self {
delivery: Some(delivery),
return_to,
}
}
pub fn topic(&self) -> &str {
self.delivery().topic()
}
pub fn partition(&self) -> i32 {
self.delivery().partition()
}
pub fn checkpoint_next_offset(&self) -> i64 {
self.delivery().lease().next_offset().get()
}
pub fn records(&self) -> AssignedConsumerRecords<'_> {
AssignedConsumerRecords::new(self.delivery())
}
pub fn record_count(&self) -> usize {
self.records().count()
}
fn delivery(&self) -> &AssignedConsumerDelivery {
&self.delivery.as_slice()[0]
}
}
impl Drop for AssignedConsumerBatch {
fn drop(&mut self) {
if let Some(delivery) = self.delivery.take() {
self.return_to.return_assigned_delivery(delivery);
}
}
}
impl std::fmt::Debug for AssignedConsumerBatch {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("AssignedConsumerBatch")
.field("topic", &self.topic())
.field("partition", &self.partition())
.field("checkpoint_next_offset", &self.checkpoint_next_offset())
.field("record_count", &self.record_count())
.finish()
}
}