1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-23 16:21:06 +01: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 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

View File

@ -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;