Remove `FromRequest::Config` which should not be exposed as type

This commit is contained in:
Arniu 2021-05-24 20:04:41 +08:00
parent b1de196509
commit fe6f0aee6d
15 changed files with 21 additions and 49 deletions

View File

@ -10,6 +10,7 @@
Alternatively, explicitly require trailing slashes: `NormalizePath::new(TrailingSlash::Always)`.
* The `type Config` of `FromRequest` was removed.
## 3.0.0

View File

@ -59,7 +59,6 @@ impl AsRef<Path> for PathBufWrap {
impl FromRequest for PathBufWrap {
type Error = UriSegmentError;
type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ready(req.match_info().path().parse())

View File

@ -33,7 +33,6 @@ use crate::server::Multipart;
impl FromRequest for Multipart {
type Error = Error;
type Future = Ready<Result<Multipart, Error>>;
type Config = ();
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {

View File

@ -114,7 +114,6 @@ where
}
impl<T: ?Sized + 'static> FromRequest for Data<T> {
type Config = ();
type Error = Error;
type Future = Ready<Result<Self, Error>>;

View File

@ -15,9 +15,6 @@ use crate::{dev::Payload, Error, HttpRequest};
///
/// Types that implement this trait can be used with `Route` handlers.
pub trait FromRequest: Sized {
/// Configuration for this extractor.
type Config: Default + 'static;
/// The associated error which can be returned.
type Error: Into<Error>;
@ -33,14 +30,6 @@ pub trait FromRequest: Sized {
fn extract(req: &HttpRequest) -> Self::Future {
Self::from_request(req, &mut Payload::None)
}
/// Create and configure config instance.
fn configure<F>(f: F) -> Self::Config
where
F: FnOnce(Self::Config) -> Self::Config,
{
f(Self::Config::default())
}
}
/// Optionally extract a field from the request
@ -64,7 +53,6 @@ pub trait FromRequest: Sized {
/// impl FromRequest for Thing {
/// type Error = Error;
/// type Future = Ready<Result<Self, Self::Error>>;
/// type Config = ();
///
/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
/// if rand::random() {
@ -99,7 +87,6 @@ where
{
type Error = Error;
type Future = FromRequestOptFuture<T::Future>;
type Config = T::Config;
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
@ -156,7 +143,6 @@ where
/// impl FromRequest for Thing {
/// type Error = Error;
/// type Future = Ready<Result<Thing, Error>>;
/// type Config = ();
///
/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
/// if rand::random() {
@ -189,7 +175,6 @@ where
{
type Error = Error;
type Future = FromRequestResFuture<T::Future>;
type Config = T::Config;
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
@ -222,7 +207,6 @@ where
impl FromRequest for () {
type Error = Error;
type Future = Ready<Result<(), Error>>;
type Config = ();
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
ready(Ok(()))
@ -262,7 +246,6 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
{
type Error = Error;
type Future = $fut_type<$($T),+>;
type Config = ($($T::Config),+);
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
$fut_type {

View File

@ -375,7 +375,6 @@ impl Drop for HttpRequest {
/// }
/// ```
impl FromRequest for HttpRequest {
type Config = ();
type Error = Error;
type Future = Ready<Result<Self, Error>>;

View File

@ -65,7 +65,6 @@ impl<T: Clone + 'static> Deref for ReqData<T> {
}
impl<T: Clone + 'static> FromRequest for ReqData<T> {
type Config = ();
type Error = Error;
type Future = Ready<Result<Self, Error>>;

View File

@ -173,7 +173,7 @@ where
/// Resource data overrides data registered by `App::data()` method.
///
/// ```
/// use actix_web::{web, App, FromRequest};
/// use actix_web::{web, App};
///
/// /// extract text data from request
/// async fn index(body: String) -> String {
@ -184,9 +184,7 @@ where
/// let app = App::new().service(
/// web::resource("/index.html")
/// // limit size of the payload
/// .data(String::configure(|cfg| {
/// cfg.limit(4096)
/// }))
/// .data(web::PayloadConfig::new(4096))
/// .route(
/// web::get()
/// // register handler

View File

@ -187,7 +187,6 @@ where
{
type Error = EitherExtractError<L::Error, R::Error>;
type Future = EitherExtractFut<L, R>;
type Config = ();
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
EitherExtractFut {

View File

@ -121,20 +121,12 @@ impl<T> FromRequest for Form<T>
where
T: DeserializeOwned + 'static,
{
type Config = FormConfig;
type Error = Error;
type Future = FormExtractFut<T>;
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let (limit, err_handler) = req
.app_data::<Self::Config>()
.or_else(|| {
req.app_data::<web::Data<Self::Config>>()
.map(|d| d.as_ref())
})
.map(|c| (c.limit, c.err_handler.clone()))
.unwrap_or((16384, None));
let FormConfig { limit, err_handler } = FormConfig::from_req(req).clone();
FormExtractFut {
fut: UrlEncoded::new(req, payload).limit(limit),
@ -236,14 +228,25 @@ impl FormConfig {
self.err_handler = Some(Rc::new(f));
self
}
/// Extract payload config from app data. Check both `T` and `Data<T>`, in that order, and fall
/// back to the default payload config.
fn from_req(req: &HttpRequest) -> &Self {
req.app_data::<Self>()
.or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref()))
.unwrap_or(&DEFAULT_CONFIG)
}
}
/// Allow shared refs used as default.
const DEFAULT_CONFIG: FormConfig = FormConfig {
limit: 16_384, // 2^14 bytes (~16kB)
err_handler: None,
};
impl Default for FormConfig {
fn default() -> Self {
FormConfig {
limit: 16_384, // 2^14 bytes (~16kB)
err_handler: None,
}
DEFAULT_CONFIG.clone()
}
}

View File

@ -62,7 +62,6 @@ where
{
type Error = ParseError;
type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {

View File

@ -139,7 +139,6 @@ where
{
type Error = Error;
type Future = JsonExtractFut<T>;
type Config = JsonConfig;
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {

View File

@ -98,12 +98,11 @@ where
{
type Error = Error;
type Future = Ready<Result<Self, Self::Error>>;
type Config = PathConfig;
#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let error_handler = req
.app_data::<Self::Config>()
.app_data::<PathConfig>()
.map(|c| c.ehandler.clone())
.unwrap_or(None);

View File

@ -62,7 +62,6 @@ impl Stream for Payload {
/// See [here](#usage) for example of usage as an extractor.
impl FromRequest for Payload {
type Config = PayloadConfig;
type Error = Error;
type Future = Ready<Result<Payload, Error>>;
@ -89,7 +88,6 @@ impl FromRequest for Payload {
/// }
/// ```
impl FromRequest for Bytes {
type Config = PayloadConfig;
type Error = Error;
type Future = Either<BytesExtractFut, Ready<Result<Bytes, Error>>>;
@ -138,7 +136,6 @@ impl<'a> Future for BytesExtractFut {
/// format!("Body {}!", text)
/// }
impl FromRequest for String {
type Config = PayloadConfig;
type Error = Error;
type Future = Either<StringExtractFut, Ready<Result<String, Error>>>;

View File

@ -113,12 +113,11 @@ where
{
type Error = Error;
type Future = Ready<Result<Self, Error>>;
type Config = QueryConfig;
#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let error_handler = req
.app_data::<Self::Config>()
.app_data::<QueryConfig>()
.map(|c| c.err_handler.clone())
.unwrap_or(None);