1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-01 14:01:37 +00:00

Implement Deserialize and Default for actix_web::Data (#3109)

* Implement Default and Deserialize for Data

* FMT

* Change Log

* tweak changelog

* chore: whitespace

---------

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Wyatt Herkamp 2023-08-27 18:47:05 -04:00 committed by GitHub
parent 55c15f5bbf
commit cbf5e948db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -9,6 +9,8 @@
- Add `web::Payload::to_bytes[_limited]()` helper methods. - Add `web::Payload::to_bytes[_limited]()` helper methods.
- Add missing constructors on `HttpResponse` for several status codes. - Add missing constructors on `HttpResponse` for several status codes.
- Add `http::header::ContentLength` typed header. - Add `http::header::ContentLength` typed header.
- Implement `Default` for `web::Data`.
- Implement `serde::Deserialize` for `web::Data`.
### Changed ### Changed

View file

@ -3,7 +3,7 @@ use std::{any::type_name, ops::Deref, sync::Arc};
use actix_http::Extensions; use actix_http::Extensions;
use actix_utils::future::{err, ok, Ready}; use actix_utils::future::{err, ok, Ready};
use futures_core::future::LocalBoxFuture; use futures_core::future::LocalBoxFuture;
use serde::Serialize; use serde::{de, Serialize};
use crate::{dev::Payload, error, Error, FromRequest, HttpRequest}; use crate::{dev::Payload, error, Error, FromRequest, HttpRequest};
@ -128,6 +128,12 @@ impl<T: ?Sized> From<Arc<T>> for Data<T> {
} }
} }
impl<T: Default> Default for Data<T> {
fn default() -> Self {
Data::new(T::default())
}
}
impl<T> Serialize for Data<T> impl<T> Serialize for Data<T>
where where
T: Serialize, T: Serialize,
@ -139,6 +145,17 @@ where
self.0.serialize(serializer) self.0.serialize(serializer)
} }
} }
impl<'de, T> de::Deserialize<'de> for Data<T>
where
T: de::Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
Ok(Data::new(T::deserialize(deserializer)?))
}
}
impl<T: ?Sized + 'static> FromRequest for Data<T> { impl<T: ?Sized + 'static> FromRequest for Data<T> {
type Error = Error; type Error = Error;