Skip to content

Commit

Permalink
Bug fix for awslabs#749
Browse files Browse the repository at this point in the history
  • Loading branch information
spencergilbert committed Jun 22, 2023
1 parent 647fc04 commit 3d6aefb
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions sdk/aws-smithy-http/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pin_project! {
// In the event of retry, this function will be called to generate a new body. See
// [`try_clone()`](SdkBody::try_clone)
rebuild: Option<Arc<dyn (Fn() -> Inner) + Send + Sync>>,
bytes_contents: Option<Bytes>
}
}

Expand Down Expand Up @@ -90,6 +91,7 @@ impl SdkBody {
Self {
inner: Inner::Dyn { inner: body },
rebuild: None,
bytes_contents: None,
}
}

Expand All @@ -106,20 +108,23 @@ impl SdkBody {
SdkBody {
inner: initial.inner,
rebuild: Some(Arc::new(move || f().inner)),
bytes_contents: initial.bytes_contents,
}
}

pub fn taken() -> Self {
Self {
inner: Inner::Taken,
rebuild: None,
bytes_contents: None,
}
}

pub fn empty() -> Self {
Self {
inner: Inner::Once { inner: None },
rebuild: Some(Arc::new(|| Inner::Once { inner: None })),
bytes_contents: None,
}
}

Expand Down Expand Up @@ -150,10 +155,9 @@ impl SdkBody {
/// If this SdkBody is NOT streaming, this will return the byte slab
/// If this SdkBody is streaming, this will return `None`
pub fn bytes(&self) -> Option<&[u8]> {
match &self.inner {
Inner::Once { inner: Some(b) } => Some(b),
Inner::Once { inner: None } => Some(&[]),
_ => None,
match &self.bytes_contents {
Some(b) => Some(b),
None => None,
}
}

Expand All @@ -163,6 +167,7 @@ impl SdkBody {
Self {
inner: next,
rebuild: self.rebuild.clone(),
bytes_contents: self.bytes_contents.clone(),
}
})
}
Expand All @@ -178,6 +183,18 @@ impl SdkBody {
f(self)
}
}

pub fn map_immutable(self, f: impl Fn(SdkBody) -> SdkBody + Sync + Send + 'static) -> SdkBody {
// `Bytes` has an `Arc` inside, so cloning is cheap.
let contents = self.bytes_contents.clone();
let mut out = if self.rebuild.is_some() {
SdkBody::retryable(move || f(self.try_clone().unwrap()))
} else {
f(self)
};
out.bytes_contents = contents;
out
}
}

impl From<&str> for SdkBody {
Expand All @@ -188,13 +205,15 @@ impl From<&str> for SdkBody {

impl From<Bytes> for SdkBody {
fn from(bytes: Bytes) -> Self {
let b = bytes.clone();
SdkBody {
inner: Inner::Once {
inner: Some(bytes.clone()),
},
rebuild: Some(Arc::new(move || Inner::Once {
inner: Some(bytes.clone()),
})),
bytes_contents: Some(b),
}
}
}
Expand All @@ -204,6 +223,7 @@ impl From<hyper::Body> for SdkBody {
SdkBody {
inner: Inner::Streaming { inner: body },
rebuild: None,
bytes_contents: None,
}
}
}
Expand Down

0 comments on commit 3d6aefb

Please sign in to comment.