-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Full
and Empty
generic over the error type
#85
base: master
Are you sure you want to change the base?
Conversation
It is not uncommon to have a function that creatures a boxed body via `Full` or `Empty`: ```rust fn box_body_from_bytes(bytes: Bytes) -> UnsyncBoxBody<Bytes, Error> { Full::new(bytes) .map_err(|e| match e {}) .boxed_unsync() } ``` The `.map_err(|e| match e {})` dance is necessary because `Full` always uses `Infallible` as its error type, so we have to convert that into whatever error type we actually need. This will often be hyper's, tonic's, or axum's error type. However if we make `Full` generic over the error type: ```rust pub struct Full<D, E> { ... } ``` then Rust can just infer it and we avoid having to call `map_err`: ```rust fn box_body_from_bytes(bytes: Bytes) -> UnsyncBoxBody<Bytes, Error> { Full::new(bytes).boxed_unsync() } ``` I think this is quite a bit nicer. It is especially nice that we avoid having to type `match e {}` which is quite confusing unless you've see it before. The downside to this is that if the error type cannot be infered you have to explicitly set it when creating a `Full`: ```rust Full::<_, Error>::new(bytes) ``` That makes this a breaking change.
Full
's and Empty
's error type genericFull
and Empty
generic over the error type
http-body-util/src/empty.rs
Outdated
fmt, | ||
marker::PhantomData, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
/// A body that is always empty. | ||
pub struct Empty<D> { | ||
_marker: PhantomData<fn() -> D>, | ||
pub struct Empty<D, E> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably worth discussing: what if it still had a default? Empty<D, E = Infallible>
? It seems useful, but there's probably some cons to consider too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that makes sense!
I'm in favor of this; it'd make testing of functions that otherwise accept "production" types much more straighforward. With the addition of the default, would this be a breaking change? |
I think the default keeps it from being breaking. But it also means simply |
It is not uncommon to have a function that creatures a boxed body via
Full
orEmpty
:The
.map_err(|e| match e {})
dance is necessary becauseFull
always usesInfallible
as its error type, so we have to convert that into whatever error type we actually need. This will often be hyper's, tonic's, or axum's error type.However if we make
Full
generic over the error type:then Rust can just infer it and we avoid having to call
map_err
:I think this is quite a bit nicer. It is especially nice that we avoid having to type
match e {}
which is quite confusing unless you've see it before.The downside to this is that if the error type cannot be infered you have to explicitly set it when creating a
Full
:That makes this a breaking change.