Skip to content

Commit

Permalink
Move body impl, use box dyn error type
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Jul 11, 2024
1 parent 25f9213 commit c840978
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions opentelemetry-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,39 +108,12 @@ pub mod hyper {
use http_body_util::{BodyExt, Full};
use hyper::body::{Body as HttpBody, Frame};
use hyper_util::client::legacy::{connect::Connect, Client};
use std::convert::Infallible;
use std::fmt::Debug;
use std::pin::Pin;
use std::task::{self, Poll};
use std::time::Duration;
use tokio::time;

pub struct Body(Full<Bytes>);

impl HttpBody for Body {
type Data = Bytes;
type Error = Infallible;

#[inline]
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
let inner_body = unsafe { self.map_unchecked_mut(|b| &mut b.0) };
inner_body.poll_frame(cx)
}

#[inline]
fn is_end_stream(&self) -> bool {
self.0.is_end_stream()
}

#[inline]
fn size_hint(&self) -> hyper::body::SizeHint {
self.0.size_hint()
}
}

#[derive(Debug, Clone)]
pub struct HyperClient<C> {
inner: Client<C, Body>,
Expand Down Expand Up @@ -194,6 +167,32 @@ pub mod hyper {
Ok(http_response.error_for_status()?)
}
}

pub struct Body(Full<Bytes>);

impl HttpBody for Body {
type Data = Bytes;
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[inline]
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
let inner_body = unsafe { self.map_unchecked_mut(|b| &mut b.0) };
inner_body.poll_frame(cx)
}

#[inline]
fn is_end_stream(&self) -> bool {
self.0.is_end_stream()
}

#[inline]
fn size_hint(&self) -> hyper::body::SizeHint {
self.0.size_hint()
}
}
}

/// Methods to make working with responses from the [`HttpClient`] trait easier.
Expand Down

0 comments on commit c840978

Please sign in to comment.