pub struct ThinData<T>(pub T);
Expand description
Application data wrapper and extractor for cheaply-cloned types.
Similar to the Data
wrapper but for Clone
/Copy
types that are already an Arc
internally,
share state using some other means when cloned, or is otherwise static data that is very cheap
to clone.
Unlike Data
, this wrapper clones T
during extraction. Therefore, it is the user’s
responsibility to ensure that clones of T
do actually share the same state, otherwise state
may be unexpectedly different across multiple requests.
Note that if your type is literally an Arc<T>
then it’s recommended to use the
Data::from(arc)
conversion instead.
§Examples
use actix_web::{
web::{self, ThinData},
App, HttpResponse, Responder,
};
// Use the `ThinData<T>` extractor to access a database connection pool.
async fn index(ThinData(db_pool): ThinData<DbPool>) -> impl Responder {
// database action ...
HttpResponse::Ok()
}
let db_pool = DbPool::default();
App::new()
.app_data(ThinData(db_pool.clone()))
.service(web::resource("/").get(index))
Tuple Fields§
§0: T
Trait Implementations§
source§impl<T: Clone + 'static> FromRequest for ThinData<T>
impl<T: Clone + 'static> FromRequest for ThinData<T>
§type Future = Ready<Result<ThinData<T>, <ThinData<T> as FromRequest>::Error>>
type Future = Ready<Result<ThinData<T>, <ThinData<T> as FromRequest>::Error>>
Future that resolves to a
Self
. Read moresource§fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Create a
Self
from request parts asynchronously.Auto Trait Implementations§
impl<T> Freeze for ThinData<T>where
T: Freeze,
impl<T> RefUnwindSafe for ThinData<T>where
T: RefUnwindSafe,
impl<T> Send for ThinData<T>where
T: Send,
impl<T> Sync for ThinData<T>where
T: Sync,
impl<T> Unpin for ThinData<T>where
T: Unpin,
impl<T> UnwindSafe for ThinData<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)