use std::{ops::Deref, sync::Arc}; /// This type can be used to pass your own data into library functions and traits. It can be useful /// to pass around database connections or other context. #[derive(Debug)] pub struct Data(Arc); impl Data { /// Create new `Data` instance. pub fn new(state: T) -> Data { Data(Arc::new(state)) } /// Get reference to inner app data. pub fn get_ref(&self) -> &T { self.0.as_ref() } /// Convert to the internal Arc pub fn into_inner(self) -> Arc { self.0 } } impl Deref for Data { type Target = Arc; fn deref(&self) -> &Arc { &self.0 } } impl Clone for Data { fn clone(&self) -> Data { Data(self.0.clone()) } }