From f0fe96ac7843f64278f1158c6ed9f08c6913b079 Mon Sep 17 00:00:00 2001 From: Fahad Zubair Date: Tue, 30 May 2023 15:00:15 +0100 Subject: [PATCH] Fix tiny_map from_iter where one operation was being dropped (#2733) 15th operation was being dropped when iterator was being read into a TinyMap The 15th operation was being dropped when the if statement was caught because the operation had been popped of the iterator but hadn't been added to the vec of operations before the two iterators were being chained and collected into a Added unit tests that reproduced the issue and verified that the issue is fixed - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or runtime crates - [ ] I have updated `CHANGELOG.next.toml` if I made changes to the AWS SDK, generated SDK code, or SDK runtime crates ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ Co-authored-by: Thor Bjorgvinsson <64786777+Thor-Bjorgvinsson@users.noreply.github.com> --- CHANGELOG.next.toml | 2 +- .../src/routing/tiny_map.rs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index fc4c4c2578..0afcaae4a3 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -9,4 +9,4 @@ # message = "Fix typos in module documentation for generated crates" # references = ["smithy-rs#920"] # meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client | server | all"} -# author = "rcoh" \ No newline at end of file +# author = "rcoh" diff --git a/rust-runtime/aws-smithy-http-server/src/routing/tiny_map.rs b/rust-runtime/aws-smithy-http-server/src/routing/tiny_map.rs index 236da27797..7011515641 100644 --- a/rust-runtime/aws-smithy-http-server/src/routing/tiny_map.rs +++ b/rust-runtime/aws-smithy-http-server/src/routing/tiny_map.rs @@ -78,13 +78,13 @@ where // Populate the `Vec` while let Some((index, pair)) = iter.next() { + vec.push(pair); + // If overflow `CUTOFF` then return a `HashMap` instead if index == CUTOFF { let inner = TinyMapInner::HashMap(vec.into_iter().chain(iter.map(|(_, pair)| pair)).collect()); return TinyMap { inner }; } - - vec.push(pair); } TinyMap { @@ -158,19 +158,25 @@ mod tests { #[test] fn get_small_success() { let tiny_map: TinyMap<_, _, CUTOFF> = SMALL_VALUES.into_iter().collect(); - assert_eq!(tiny_map.get("a"), Some(&0)) + SMALL_VALUES.into_iter().for_each(|(op, val)| { + assert_eq!(tiny_map.get(op), Some(&val)); + }); } #[test] fn get_medium_success() { let tiny_map: TinyMap<_, _, CUTOFF> = MEDIUM_VALUES.into_iter().collect(); - assert_eq!(tiny_map.get("d"), Some(&3)) + MEDIUM_VALUES.into_iter().for_each(|(op, val)| { + assert_eq!(tiny_map.get(op), Some(&val)); + }); } #[test] fn get_large_success() { let tiny_map: TinyMap<_, _, CUTOFF> = LARGE_VALUES.into_iter().collect(); - assert_eq!(tiny_map.get("h"), Some(&7)) + LARGE_VALUES.into_iter().for_each(|(op, val)| { + assert_eq!(tiny_map.get(op), Some(&val)); + }); } #[test]