Skip to content

Commit 0d927f0

Browse files
authored
feat(util/full): introduce Full::into_inner() (#155)
fixes #154. this commit introduces a `http_body_util::Full::into_inner()` method. this allows callers to consume a `Full<D>`, and reacquire the wrapped value. this matches the style of [many interfaces][into-inner-docs] offered by various types provided in `core` and `std` such as `Box<T>`, `Arc<T>`, `Mutex<T>`, as well as [many interfaces][tokio-into-inner-docs] offered by various types in `tokio` such as `BufWriter<T>`, `OnceCell<T>`, and so forth. this pattern is also mirrored in other `http_body_util` types including `Either<L, R>`. ```rust use http_body_util::Full; const DATA: &[u8] = b"hello"; let full = Full::new(DATA); assert_eq!(full.into_inner(), Some("hello").map(str::as_bytes)); ``` NB: an `Full::inner()` method is not provided here. other types in `std` and `tokio` do not expose such a method. idiomatically, once a chunk is placed into a `Full<T>` it should be thought of as a `Body` rather than a `T`, and `inner()` would make this a leaky abstraction. [into-inner-docs]: https://doc.rust-lang.org/stable/std/?search=into_inner [tokio-into-inner-docs]: https://docs.rs/tokio/latest/tokio/?search=into_inner Signed-off-by: katelyn martin <[email protected]>
1 parent 740614b commit 0d927f0

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

http-body-util/src/full.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ where
2929
}
3030
}
3131

32+
impl<D> Full<D> {
33+
/// Consumes this `Full`, returning the wrapped value.
34+
///
35+
/// If this body has already been polled, this returns `None`.
36+
///
37+
/// # Examples
38+
///
39+
/// ```
40+
/// # use http_body_util::Full;
41+
/// const DATA: &[u8] = b"hello";
42+
/// let full = Full::new(DATA);
43+
/// assert_eq!(full.into_inner(), Some("hello").map(str::as_bytes));
44+
/// ```
45+
pub fn into_inner(self) -> Option<D> {
46+
self.data
47+
}
48+
}
49+
3250
impl<D> Body for Full<D>
3351
where
3452
D: Buf,
@@ -144,4 +162,27 @@ mod tests {
144162
assert!(Full::<&[u8]>::default().frame().await.is_none());
145163
assert!(Full::new(&b""[..]).frame().await.is_none());
146164
}
165+
166+
#[tokio::test]
167+
async fn full_into_inner_returns_none_before_poll() {
168+
const DATA: &[u8] = b"hello";
169+
let full = Full::new(DATA);
170+
assert_eq!(
171+
full.into_inner(),
172+
Some(str::as_bytes("hello")),
173+
"`Full::into_inner()` returns `Some(_)` before poll"
174+
);
175+
}
176+
177+
#[tokio::test]
178+
async fn full_into_inner_returns_none_after_poll() {
179+
const DATA: &[u8] = b"hello";
180+
let mut full = Full::new(DATA);
181+
full.frame().await.expect("a result").expect("a frame");
182+
assert_eq!(
183+
full.into_inner(),
184+
None,
185+
"`Full::into_inner()` returns `None` after poll"
186+
);
187+
}
147188
}

0 commit comments

Comments
 (0)