From ef07c88707128f9b82d4e1a597cf0db8e0037367 Mon Sep 17 00:00:00 2001 From: Andrew Jewell <107044381+ajewellamz@users.noreply.github.com> Date: Wed, 16 Oct 2024 17:47:39 -0400 Subject: [PATCH] chore: add Into conversions for Blob to [u8] (#3877) ## Motivation and Context Often I have a &[u8] or a Vec, but the sdk function takes a Blob. It would be convenient to pass the my_vec instead of aws_smithy_types::Blob::new(my_vec) ## Description In blob.rs I added Into> for Blob Into for Vec Into for &[u8] ## Testing Added a test to blob.rs to test the various scenarios. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ --- rust-runtime/aws-smithy-types/Cargo.toml | 2 +- rust-runtime/aws-smithy-types/src/blob.rs | 37 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/rust-runtime/aws-smithy-types/Cargo.toml b/rust-runtime/aws-smithy-types/Cargo.toml index 7b52b8b6d8..3fd9186d0f 100644 --- a/rust-runtime/aws-smithy-types/Cargo.toml +++ b/rust-runtime/aws-smithy-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aws-smithy-types" -version = "1.2.7" +version = "1.2.8" authors = [ "AWS Rust SDK Team ", "Russell Cohen ", diff --git a/rust-runtime/aws-smithy-types/src/blob.rs b/rust-runtime/aws-smithy-types/src/blob.rs index 7f76a45df3..f6ba16fda4 100644 --- a/rust-runtime/aws-smithy-types/src/blob.rs +++ b/rust-runtime/aws-smithy-types/src/blob.rs @@ -31,6 +31,24 @@ impl AsRef<[u8]> for Blob { } } +impl From> for Blob { + fn from(value: Vec) -> Self { + Blob::new(value) + } +} + +impl From for Vec { + fn from(value: Blob) -> Self { + value.into_inner() + } +} + +impl From<&[u8]> for Blob { + fn from(value: &[u8]) -> Self { + Blob::new(value) + } +} + #[cfg(all(aws_sdk_unstable, feature = "serde-serialize"))] mod serde_serialize { use super::*; @@ -103,6 +121,25 @@ mod serde_deserialize { } #[cfg(test)] +mod test { + use crate::Blob; + + #[test] + fn blob_conversion() { + let my_bytes: &[u8] = &[1u8, 2u8, 3u8]; + let my_vec = vec![1u8, 2u8, 3u8]; + let orig_vec = my_vec.clone(); + + let blob1: Blob = my_bytes.into(); + let vec1: Vec = blob1.into(); + assert_eq!(orig_vec, vec1); + + let blob2: Blob = my_vec.into(); + let vec2: Vec = blob2.into(); + assert_eq!(orig_vec, vec2); + } +} + #[cfg(all( aws_sdk_unstable, feature = "serde-serialize",