Skip to content

Commit

Permalink
Add support for writing commit metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsabadie-datadog committed Sep 20, 2021
1 parent 4b7fe5e commit bbf67fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rdkafka-sys/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Utility functions.
use libc::c_char;

use crate::types::RDKafkaErrorCode;
use crate::types::RDKafkaErrorCode::*;
use crate::types::RDKafkaRespErr;
Expand Down Expand Up @@ -175,3 +177,7 @@ pub fn rd_kafka_resp_err_t_to_rdkafka_error(err: RDKafkaRespErr) -> RDKafkaError
RD_KAFKA_RESP_ERR_END_ALL => EndAll,
}
}

extern "C" {
pub fn rd_strndup(_: *const c_char, _: isize) -> *mut c_char;
}
12 changes: 12 additions & 0 deletions src/topic_partition_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ impl<'a> TopicPartitionListElem<'a> {
let slice: &[u8] = unsafe { ptr_to_slice(self.ptr.metadata, self.ptr.metadata_size) };
from_utf8(slice).expect("metadata is not UTF-8")
}

/// Set the metadata.
pub fn set_metadata(&mut self, metadata: impl AsRef<str>) {
let metadata = metadata.as_ref();
let c_metadata = CString::new(metadata).expect("metadat is not UTF-8");

unsafe {
self.ptr.metadata =
rdsys::helpers::rd_strndup(c_metadata.as_ptr(), metadata.len() as isize) as *mut _;
self.ptr.metadata_size = libc::strlen(self.ptr.metadata as *const _);
}
}
}

impl<'a> PartialEq for TopicPartitionListElem<'a> {
Expand Down

0 comments on commit bbf67fd

Please sign in to comment.