Skip to content

Commit

Permalink
chore: add Into conversions for Blob to [u8] (#3877)
Browse files Browse the repository at this point in the history
## Motivation and Context
Often I have a &[u8] or a Vec<u8>, 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<Vec<u8>> for Blob
Into<Blob> for Vec<u8>
Into<Blob> 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._
  • Loading branch information
ajewellamz authored Oct 16, 2024
1 parent 8e8101a commit ef07c88
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rust-runtime/aws-smithy-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aws-smithy-types"
version = "1.2.7"
version = "1.2.8"
authors = [
"AWS Rust SDK Team <aws-sdk-rust@amazon.com>",
"Russell Cohen <rcoh@amazon.com>",
Expand Down
37 changes: 37 additions & 0 deletions rust-runtime/aws-smithy-types/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ impl AsRef<[u8]> for Blob {
}
}

impl From<Vec<u8>> for Blob {
fn from(value: Vec<u8>) -> Self {
Blob::new(value)
}
}

impl From<Blob> for Vec<u8> {
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::*;
Expand Down Expand Up @@ -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<u8> = blob1.into();
assert_eq!(orig_vec, vec1);

let blob2: Blob = my_vec.into();
let vec2: Vec<u8> = blob2.into();
assert_eq!(orig_vec, vec2);
}
}

#[cfg(all(
aws_sdk_unstable,
feature = "serde-serialize",
Expand Down

0 comments on commit ef07c88

Please sign in to comment.