Skip to content

Commit

Permalink
enable extract ?Sized state with StateRef. (#909)
Browse files Browse the repository at this point in the history
* enable extract ?Sized state with StateRef.

* clippy fix.

* fix nightly build.
  • Loading branch information
fakeshadow authored Jan 24, 2024
1 parent f2d64c5 commit 4e1d4ff
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions examples/error-handle/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(trait_upcasting)]

//! example of error handling in xitca-web.
//! code must be compiled with nightly Rust.
Expand Down
10 changes: 5 additions & 5 deletions http/src/h2/proto/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ where
}
}
}
header => self.pseudo.from_header(
header => self.pseudo.parse(
header,
max_header_list_size,
&mut self.is_over_size,
Expand Down Expand Up @@ -606,7 +606,7 @@ where
pub trait _Pseudo {
fn into_iter(self) -> impl Iterator<Item = super::hpack::Header<Option<HeaderName>>> + Send;

fn from_header(
fn parse(
&mut self,
header: super::hpack::Header,
max_header_list_size: usize,
Expand Down Expand Up @@ -660,7 +660,7 @@ impl _Pseudo for Pseudo {
Iter(self)
}

fn from_header(
fn parse(
&mut self,
header: super::hpack::Header,
max_header_list_size: usize,
Expand Down Expand Up @@ -728,7 +728,7 @@ impl _Pseudo for ResponsePseudo {
core::iter::once_with(move || super::hpack::Header::Status(self.status))
}

fn from_header(
fn parse(
&mut self,
header: super::hpack::Header,
max_header_list_size: usize,
Expand Down Expand Up @@ -767,7 +767,7 @@ impl _Pseudo for () {
core::iter::empty()
}

fn from_header(&mut self, _: super::hpack::Header, _: usize, _: &mut bool, _: bool, _: &mut bool, _: &mut usize) {}
fn parse(&mut self, _: super::hpack::Header, _: usize, _: &mut bool, _: bool, _: &mut bool, _: &mut usize) {}

fn as_header_size(&self) -> usize {
0
Expand Down
2 changes: 2 additions & 0 deletions web/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# unreleased
## Add
- `StateRef` can used for extracting `?Sized` type from application state.

# 0.2.1
## Add
Expand Down
2 changes: 1 addition & 1 deletion web/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xitca-web"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
license = "Apache-2.0"
description = "an async web framework"
Expand Down
4 changes: 3 additions & 1 deletion web/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ use self::service_impl::ErrorService;
/// assert_eq!(res.status().as_u16(), 200);
///
/// // upcast and downcast to concrete error type again.
/// // *. trait upcast is a feature stabled in Rust 1.76
/// // *. trait upcast is a nightly feature.
/// // see https://github.com/rust-lang/rust/issues/65991 for detail
///
/// // let e = &*e as &dyn error::Error;
/// // assert!(e.downcast_ref::<Foo>().is_some());
/// }
Expand Down
6 changes: 4 additions & 2 deletions web/src/handler/types/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::{body::BodyStream, context::WebContext, error::Error, handler::FromRe

/// App state extractor.
/// S type must be the same with the type passed to App::with_xxx_state(S).
pub struct StateRef<'a, S>(pub &'a S);
pub struct StateRef<'a, S>(pub &'a S)
where
S: ?Sized;

impl<S: fmt::Debug> fmt::Debug for StateRef<'_, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -32,7 +34,7 @@ impl<'a, 'r, C, B, T> FromRequest<'a, WebContext<'r, C, B>> for StateRef<'a, T>
where
C: Borrow<T>,
B: BodyStream,
T: 'static,
T: ?Sized + 'static,
{
type Type<'b> = StateRef<'b, T>;
type Error = Error<C>;
Expand Down

0 comments on commit 4e1d4ff

Please sign in to comment.