Skip to content

Commit ef07c88

Browse files
authored
chore: add Into conversions for Blob to [u8] (#3877)
## 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._
1 parent 8e8101a commit ef07c88

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

rust-runtime/aws-smithy-types/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "aws-smithy-types"
3-
version = "1.2.7"
3+
version = "1.2.8"
44
authors = [
55
"AWS Rust SDK Team <[email protected]>",
66
"Russell Cohen <[email protected]>",

rust-runtime/aws-smithy-types/src/blob.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ impl AsRef<[u8]> for Blob {
3131
}
3232
}
3333

34+
impl From<Vec<u8>> for Blob {
35+
fn from(value: Vec<u8>) -> Self {
36+
Blob::new(value)
37+
}
38+
}
39+
40+
impl From<Blob> for Vec<u8> {
41+
fn from(value: Blob) -> Self {
42+
value.into_inner()
43+
}
44+
}
45+
46+
impl From<&[u8]> for Blob {
47+
fn from(value: &[u8]) -> Self {
48+
Blob::new(value)
49+
}
50+
}
51+
3452
#[cfg(all(aws_sdk_unstable, feature = "serde-serialize"))]
3553
mod serde_serialize {
3654
use super::*;
@@ -103,6 +121,25 @@ mod serde_deserialize {
103121
}
104122

105123
#[cfg(test)]
124+
mod test {
125+
use crate::Blob;
126+
127+
#[test]
128+
fn blob_conversion() {
129+
let my_bytes: &[u8] = &[1u8, 2u8, 3u8];
130+
let my_vec = vec![1u8, 2u8, 3u8];
131+
let orig_vec = my_vec.clone();
132+
133+
let blob1: Blob = my_bytes.into();
134+
let vec1: Vec<u8> = blob1.into();
135+
assert_eq!(orig_vec, vec1);
136+
137+
let blob2: Blob = my_vec.into();
138+
let vec2: Vec<u8> = blob2.into();
139+
assert_eq!(orig_vec, vec2);
140+
}
141+
}
142+
106143
#[cfg(all(
107144
aws_sdk_unstable,
108145
feature = "serde-serialize",

0 commit comments

Comments
 (0)