From 4ef41d86187ab86f6c833d333ce1f60dd7e6db39 Mon Sep 17 00:00:00 2001 From: Nikhil Benesch Date: Wed, 30 Jun 2021 01:01:44 -0400 Subject: [PATCH] Implement Extend and FromIterator for ClientConfig --- .github/workflows/ci.yml | 2 +- Cargo.lock | 5 +++-- changelog.md | 7 +++++++ rdkafka.suppressions | 4 ++-- src/config.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/message.rs | 4 ++-- 6 files changed, 53 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c1e7927..55da4b3b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ on: branches: [master] env: - rust_version: 1.45.0 + rust_version: 1.53.0 jobs: lint: diff --git a/Cargo.lock b/Cargo.lock index 3df612ff..25a6dae8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -697,11 +697,12 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "lexical-core" -version = "0.6.2" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7043aa5c05dd34fb73b47acb8c3708eac428de4545ea3682ed2f11293ebd890" +checksum = "233853dfa6b87c7c00eb46a205802069263ab27e16b6bdd1b08ddf91a855e30c" dependencies = [ "arrayvec", + "bitflags", "cfg-if 0.1.10", "rustc_version", "ryu", diff --git a/changelog.md b/changelog.md index 355c794f..4910df32 100644 --- a/changelog.md +++ b/changelog.md @@ -15,8 +15,15 @@ See also the [rdkafka-sys changelog](rdkafka-sys/changelog.md). * Provide a mutable accessor (`Message::payload_mut`) for a message's payload ([#95]). +* Implement `std::iter::Extend<(String, String)>` and + `std::iter::FromIterator<(String, String)` for `ClientConfig` ([#367]). + + Thanks, [@djKooks]. + [#95]: https://github.com/fede1024/rust-rdkafka/issues/95 [#360]: https://github.com/fede1024/rust-rdkafka/issues/360 +[#367]: https://github.com/fede1024/rust-rdkafka/issues/367 +[@djKooks]: https://github.com/djKooks ## 0.26.0 (2021-03-16) diff --git a/rdkafka.suppressions b/rdkafka.suppressions index d2c4d9c0..ed248e58 100644 --- a/rdkafka.suppressions +++ b/rdkafka.suppressions @@ -6,7 +6,7 @@ Memcheck:Param statx(file_name) - fun:syscall + fun:statx fun:statx fun:_ZN3std3sys4unix2fs9try_statx* ... @@ -15,7 +15,7 @@ Memcheck:Param statx(buf) - fun:syscall + fun:statx fun:statx fun:_ZN3std3sys4unix2fs9try_statx* ... diff --git a/src/config.rs b/src/config.rs index 2d173249..2375f888 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,6 +24,7 @@ use std::collections::HashMap; use std::ffi::{CStr, CString}; +use std::iter::FromIterator; use std::mem; use std::os::raw::c_char; use std::ptr; @@ -273,6 +274,26 @@ impl ClientConfig { } } +impl FromIterator<(String, String)> for ClientConfig { + fn from_iter(iter: I) -> ClientConfig + where + I: IntoIterator, + { + let mut config = ClientConfig::new(); + config.extend(iter); + config + } +} + +impl Extend<(String, String)> for ClientConfig { + fn extend(&mut self, iter: I) + where + I: IntoIterator, + { + self.conf_map.extend(iter) + } +} + /// Return the log level fn log_level_from_global_config() -> RDKafkaLogLevel { if log_enabled!(target: "librdkafka", Level::Debug) { @@ -298,3 +319,20 @@ pub trait FromClientConfigAndContext: Sized { /// Creates a client from a client configuration and a client context. fn from_config_and_context(_: &ClientConfig, _: C) -> KafkaResult; } + +#[cfg(test)] +mod tests { + use super::ClientConfig; + + #[test] + fn test_client_config_set_map() { + let mut config: ClientConfig = vec![("a".into(), "1".into()), ("b".into(), "1".into())] + .into_iter() + .collect(); + config.extend([("b".into(), "2".into()), ("c".into(), "3".into())]); + + assert_eq!(config.get("a").unwrap(), "1"); + assert_eq!(config.get("b").unwrap(), "2"); + assert_eq!(config.get("c").unwrap(), "3"); + } +} diff --git a/src/message.rs b/src/message.rs index 469a2849..234402ac 100644 --- a/src/message.rs +++ b/src/message.rs @@ -27,8 +27,8 @@ pub enum Timestamp { impl Timestamp { /// Convert the timestamp to milliseconds since epoch. - pub fn to_millis(&self) -> Option { - match *self { + pub fn to_millis(self) -> Option { + match self { Timestamp::NotAvailable | Timestamp::CreateTime(-1) | Timestamp::LogAppendTime(-1) => { None }