From 012531334b2ec52389ef40e5e7f669be5229fba5 Mon Sep 17 00:00:00 2001 From: Konstantinos Demartinos Date: Wed, 16 Feb 2022 19:00:03 +0200 Subject: [PATCH] [#8] Implement IntoIterator for AddressParserResponse --- src/address.rs | 17 +++++++++++++---- tests/address.rs | 4 ++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/address.rs b/src/address.rs index 41131d9..6057775 100644 --- a/src/address.rs +++ b/src/address.rs @@ -22,6 +22,7 @@ //! ``` use std::collections::HashMap; use std::ffi::{CStr, CString, NulError}; +use std::vec::IntoIter; use std::slice::Iter; use crate::ffi; @@ -40,6 +41,16 @@ impl AddressParserResponse { } } +impl IntoIterator for AddressParserResponse { + type Item = (String, String); + type IntoIter = std::iter::Zip, IntoIter>; + + /// Iterates over `(label, token)` pairs by consuming the value. + fn into_iter(self) -> Self::IntoIter { + self.labels.into_iter().zip(self.tokens.into_iter()) + } +} + impl<'a> IntoIterator for &'a AddressParserResponse { type Item = (&'a String, &'a String); type IntoIter = std::iter::Zip, Iter<'a, String>>; @@ -290,10 +301,8 @@ impl From for ParsedAddress { /// Create a new `ParsedAddress` from an `AddressParserResponse`. fn from(response: AddressParserResponse) -> Self { let mut parsed_address = ParsedAddress::default(); - for (label, token) in &response { - parsed_address - .label_to_token - .insert(label.clone(), token.clone()); + for (label, token) in response { + parsed_address.label_to_token.insert(label, token); } parsed_address } diff --git a/tests/address.rs b/tests/address.rs index e7c5a51..cf913aa 100644 --- a/tests/address.rs +++ b/tests/address.rs @@ -5,9 +5,9 @@ use rustpostal::LibModules; fn assert_actual_eq_expected(address: &str, expected: Vec<(&str, &str)>) { let response = rustpostal::address::parse_address(address, None, None).unwrap(); - let actual: Vec<(&str, &str)> = response + let actual: Vec<(&str, &str)> = (&response) .into_iter() - .map(|(l, t)| (l.as_str(), t.as_str())) + .map(|(l, t)| (l.as_ref(), t.as_ref())) .collect(); assert_eq!(actual, expected); }