MCPcopy Create free account
hub / github.com/bytecodealliance/wstd / contents

Method contents

src/http/body.rs:149–175  ·  view source on GitHub ↗

Collect the entire contents of this `Body`, and expose them as a byte slice. This async fn will be pending until the entire `Body` is copied into memory, or an error occurs.

(&mut self)

Source from the content-addressed store, hash-verified

147 /// byte slice. This async fn will be pending until the entire `Body` is
148 /// copied into memory, or an error occurs.
149 pub async fn contents(&mut self) -> Result<&[u8], Error> {
150 match &mut self.0 {
151 BodyInner::Complete { data, .. } => Ok(&*data),
152 inner => {
153 let mut prev = BodyInner::Complete {
154 data: Bytes::new(),
155 trailers: None,
156 };
157 std::mem::swap(inner, &mut prev);
158 let boxed_body = match prev {
159 BodyInner::Incoming(i) => i.into_http_body().boxed_unsync(),
160 BodyInner::Boxed(b) => b,
161 BodyInner::Complete { .. } => unreachable!(),
162 };
163 let collected = boxed_body.collect().await?;
164 let trailers = collected.trailers().cloned();
165 *inner = BodyInner::Complete {
166 data: collected.to_bytes(),
167 trailers,
168 };
169 Ok(match inner {
170 BodyInner::Complete { data, .. } => &*data,
171 _ => unreachable!(),
172 })
173 }
174 }
175 }
176 /// Collect the entire contents of this `Body`, and expose it as `Bytes`.
177 /// This async fn will be pending until the entire `Body` is
178 /// copied into memory, or an error occurs.

Callers 4

bytes_contentsMethod · 0.80
str_contentsMethod · 0.80
mainFunction · 0.80
listFunction · 0.80

Calls 1

into_http_bodyMethod · 0.80

Tested by

no test coverage detected