-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
pub(crate) mod body { | ||
use quote::ToTokens; | ||
|
||
use crate::derive::*; | ||
|
||
pub(crate) const NAME: &[&str] = &["http_body1::Body"]; | ||
|
||
pub(crate) fn derive(cx: &Context, data: &Data) -> Result<TokenStream> { | ||
cx.needs_pin_projection(); | ||
|
||
let ident = &data.ident; | ||
let pin = quote!(::core::pin::Pin); | ||
let trait_: syn::Path = parse_quote!(::http_body::Body); | ||
let mut impl_ = EnumImpl::from_trait(data, trait_.clone(), None, parse_quote! { | ||
trait Body { | ||
type Data; | ||
type Error; | ||
#[inline] | ||
fn is_end_stream(&self) -> bool; | ||
#[inline] | ||
fn size_hint(&self) -> ::http_body::SizeHint; | ||
} | ||
}) | ||
.build_impl(); | ||
|
||
let poll_frame = data | ||
.variant_idents() | ||
.map(|v| quote!(#ident::#v(x) => #trait_::poll_frame(#pin::new_unchecked(x), cx))); | ||
impl_.items.push(parse_quote! { | ||
fn poll_frame( | ||
self: #pin<&mut Self>, | ||
cx: &mut ::core::task::Context<'_>, | ||
) -> ::core::task::Poll< | ||
::core::option::Option< | ||
::core::result::Result<::http_body::Frame<Self::Data>, Self::Error>, | ||
>, | ||
> { | ||
unsafe { | ||
match self.get_unchecked_mut() { #(#poll_frame,)* } | ||
} | ||
} | ||
}); | ||
|
||
Ok(impl_.into_token_stream()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
extern crate http_body1_crate as http_body; | ||
use auto_enums::enum_derive; | ||
enum Enum<A, B> { | ||
A(A), | ||
B(B), | ||
} | ||
impl<A, B> ::http_body::Body for Enum<A, B> | ||
where | ||
A: ::http_body::Body, | ||
B: ::http_body::Body< | ||
Data = <A as ::http_body::Body>::Data, | ||
Error = <A as ::http_body::Body>::Error, | ||
>, | ||
{ | ||
type Data = <A as ::http_body::Body>::Data; | ||
type Error = <A as ::http_body::Body>::Error; | ||
#[inline] | ||
fn is_end_stream(&self) -> bool { | ||
match self { | ||
Enum::A(x) => ::http_body::Body::is_end_stream(x), | ||
Enum::B(x) => ::http_body::Body::is_end_stream(x), | ||
} | ||
} | ||
#[inline] | ||
fn size_hint(&self) -> ::http_body::SizeHint { | ||
match self { | ||
Enum::A(x) => ::http_body::Body::size_hint(x), | ||
Enum::B(x) => ::http_body::Body::size_hint(x), | ||
} | ||
} | ||
fn poll_frame( | ||
self: ::core::pin::Pin<&mut Self>, | ||
cx: &mut ::core::task::Context<'_>, | ||
) -> ::core::task::Poll< | ||
::core::option::Option< | ||
::core::result::Result<::http_body::Frame<Self::Data>, Self::Error>, | ||
>, | ||
> { | ||
unsafe { | ||
match self.get_unchecked_mut() { | ||
Enum::A(x) => { | ||
::http_body::Body::poll_frame(::core::pin::Pin::new_unchecked(x), cx) | ||
} | ||
Enum::B(x) => { | ||
::http_body::Body::poll_frame(::core::pin::Pin::new_unchecked(x), cx) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
impl<A, B> ::core::marker::Unpin for Enum<A, B> | ||
where | ||
A: ::core::marker::Unpin, | ||
B: ::core::marker::Unpin, | ||
{} | ||
const _: () = { | ||
trait MustNotImplDrop {} | ||
#[allow(clippy::drop_bounds, drop_bounds)] | ||
impl<T: ::core::ops::Drop> MustNotImplDrop for T {} | ||
impl<A, B> MustNotImplDrop for Enum<A, B> {} | ||
}; | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
extern crate http_body1_crate as http_body; | ||
|
||
use auto_enums::enum_derive; | ||
|
||
#[enum_derive(http_body1::Body)] | ||
enum Enum<A, B> { | ||
A(A), | ||
B(B), | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
extern crate http_body1_crate as http_body; | ||
|
||
use auto_enums::enum_derive; | ||
|
||
#[enum_derive(http_body1::Body)] | ||
enum HttpBody1<A, B> { | ||
A(A), | ||
B(B), | ||
} | ||
|
||
fn main() {} |