sui_http/body.rs
1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::BoxError;
5use bytes::Bytes;
6use http_body_util::BodyExt;
7
8pub type BoxBody = http_body_util::combinators::UnsyncBoxBody<Bytes, BoxError>;
9
10pub fn boxed<B>(body: B) -> BoxBody
11where
12 B: http_body::Body<Data = Bytes> + Send + 'static,
13 B::Error: Into<BoxError>,
14{
15 try_downcast(body).unwrap_or_else(|body| body.map_err(Into::into).boxed_unsync())
16}
17
18pub(crate) fn try_downcast<T, K>(k: K) -> Result<T, K>
19where
20 T: 'static,
21 K: Send + 'static,
22{
23 let mut k = Some(k);
24 if let Some(k) = <dyn std::any::Any>::downcast_mut::<Option<T>>(&mut k) {
25 Ok(k.take().unwrap())
26 } else {
27 Err(k.unwrap())
28 }
29}