Skip to content

Commit

Permalink
Implement Extend and FromIterator for ClientConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
benesch committed Oct 16, 2021
1 parent 29f5ccf commit 4ef41d8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -7,7 +7,7 @@ on:
branches: [master]

env:
rust_version: 1.45.0
rust_version: 1.53.0

jobs:
lint:
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions changelog.md
Expand Up @@ -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

<a name="0.26.0"></a>
## 0.26.0 (2021-03-16)
Expand Down
4 changes: 2 additions & 2 deletions rdkafka.suppressions
Expand Up @@ -6,7 +6,7 @@
<statx1>
Memcheck:Param
statx(file_name)
fun:syscall
fun:statx
fun:statx
fun:_ZN3std3sys4unix2fs9try_statx*
...
Expand All @@ -15,7 +15,7 @@
<statx2>
Memcheck:Param
statx(buf)
fun:syscall
fun:statx
fun:statx
fun:_ZN3std3sys4unix2fs9try_statx*
...
Expand Down
38 changes: 38 additions & 0 deletions src/config.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -273,6 +274,26 @@ impl ClientConfig {
}
}

impl FromIterator<(String, String)> for ClientConfig {
fn from_iter<I>(iter: I) -> ClientConfig
where
I: IntoIterator<Item = (String, String)>,
{
let mut config = ClientConfig::new();
config.extend(iter);
config
}
}

impl Extend<(String, String)> for ClientConfig {
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = (String, String)>,
{
self.conf_map.extend(iter)
}
}

/// Return the log level
fn log_level_from_global_config() -> RDKafkaLogLevel {
if log_enabled!(target: "librdkafka", Level::Debug) {
Expand All @@ -298,3 +319,20 @@ pub trait FromClientConfigAndContext<C: ClientContext>: Sized {
/// Creates a client from a client configuration and a client context.
fn from_config_and_context(_: &ClientConfig, _: C) -> KafkaResult<Self>;
}

#[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");
}
}
4 changes: 2 additions & 2 deletions src/message.rs
Expand Up @@ -27,8 +27,8 @@ pub enum Timestamp {

impl Timestamp {
/// Convert the timestamp to milliseconds since epoch.
pub fn to_millis(&self) -> Option<i64> {
match *self {
pub fn to_millis(self) -> Option<i64> {
match self {
Timestamp::NotAvailable | Timestamp::CreateTime(-1) | Timestamp::LogAppendTime(-1) => {
None
}
Expand Down

0 comments on commit 4ef41d8

Please sign in to comment.