parent
993821ab77
commit
8f554b7e5b
@ -1,7 +1,21 @@
|
||||
use std::sync::Arc;
|
||||
use crate::simple::HandlingError;
|
||||
|
||||
pub type PgSharedPool = Arc<sqlx::Pool<sqlx::Postgres>>;
|
||||
// An application wide shared pool of postgres database connections
|
||||
#[derive(Clone)]
|
||||
pub struct PgSharedPool(sqlx::Pool<sqlx::Postgres>);
|
||||
|
||||
pub fn make_shared(conn: sqlx::Pool<sqlx::Postgres>) -> PgSharedPool {
|
||||
Arc::new(conn)
|
||||
impl PgSharedPool {
|
||||
pub fn new(conn: sqlx::Pool<sqlx::Postgres>) -> Self {
|
||||
Self(conn)
|
||||
}
|
||||
|
||||
pub async fn begin<'a>(
|
||||
&self,
|
||||
) -> Result<Box<sqlx::Transaction<'a, sqlx::Postgres>>, HandlingError> {
|
||||
self.0
|
||||
.begin()
|
||||
.await
|
||||
.map(Box::new)
|
||||
.map_err(HandlingError::DBError)
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
use std::any;
|
||||
use std::boxed::Box;
|
||||
use std::vec::Vec;
|
||||
|
||||
pub struct SharedContainer {
|
||||
items: Vec<Box<dyn any::Any + Send + Sync + 'static>>,
|
||||
}
|
||||
|
||||
impl SharedContainer {
|
||||
pub fn find<E>(&self) -> Option<&E>
|
||||
where
|
||||
E: any::Any + 'static,
|
||||
{
|
||||
for x in self.items.iter() {
|
||||
if let Some(val) = x.downcast_ref::<E>() {
|
||||
return Some(val);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn add<E>(&mut self, val: E)
|
||||
where
|
||||
E: any::Any + Send + Sync + 'static,
|
||||
{
|
||||
self.items.push(Box::new(val))
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self { items: Vec::new() }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue