mirror of
https://github.com/actix/actix-web.git
synced 2024-11-29 04:51:13 +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:
parent
55c15f5bbf
commit
cbf5e948db
2 changed files with 20 additions and 1 deletions
|
@ -9,6 +9,8 @@
|
|||
- Add `web::Payload::to_bytes[_limited]()` helper methods.
|
||||
- Add missing constructors on `HttpResponse` for several status codes.
|
||||
- Add `http::header::ContentLength` typed header.
|
||||
- Implement `Default` for `web::Data`.
|
||||
- Implement `serde::Deserialize` for `web::Data`.
|
||||
|
||||
### Changed
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::{any::type_name, ops::Deref, sync::Arc};
|
|||
use actix_http::Extensions;
|
||||
use actix_utils::future::{err, ok, Ready};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
use serde::Serialize;
|
||||
use serde::{de, Serialize};
|
||||
|
||||
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>
|
||||
where
|
||||
T: Serialize,
|
||||
|
@ -139,6 +145,17 @@ where
|
|||
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> {
|
||||
type Error = Error;
|
||||
|
|
Loading…
Reference in a new issue