Skip to main content

cartel_sqlite/engine/conn/
mod.rs

1use crate::{api, catalogs};
2use api::{exec, tx};
3use dope_fiber::abi;
4use dope_runtime::client;
5use phase::Phase;
6use std::{marker, mem, ptr};
7
8mod lease;
9use lease::Lease;
10mod phase;
11pub(crate) mod raw;
12
13/// An exclusive database capability bound to its installed application scope.
14///
15#[repr(transparent)]
16pub struct Connection<'app, 'local: 'app, 'd: 'local, I: catalogs::Catalog = catalogs::Empty> {
17    guard: Lease<'app, 'local, 'd, I>,
18}
19
20impl<'app, 'local: 'app, 'd: 'local, I: catalogs::Catalog> Connection<'app, 'local, 'd, I> {
21    const LAYOUT: () = {
22        let word = mem::size_of::<usize>();
23        assert!(mem::size_of::<Self>() == word);
24        assert!(mem::align_of::<Self>() == mem::align_of::<usize>());
25        assert!(mem::size_of::<Option<Self>>() == word);
26    };
27
28    const fn new(guard: Lease<'app, 'local, 'd, I>) -> Self {
29        Self { guard }
30    }
31
32    pub fn query(&mut self) -> exec::Query<'_, 'local, 'd, I> {
33        self.guard.state().query()
34    }
35
36    pub fn transaction(
37        &mut self,
38    ) -> impl abi::Fiber<'d, Output = Result<tx::Transaction<'_, 'local, 'd, I>, crate::Error>>
39    + use<'_, 'app, 'local, 'd, I> {
40        self.guard.state().transaction()
41    }
42}
43
44impl<'query, 'app: 'query, 'local: 'app, 'd: 'local, I: catalogs::Catalog>
45    api::IntoQuery<'query, 'local, 'd, I> for &'query mut Connection<'app, 'local, 'd, I>
46{
47    fn into_query(self) -> exec::Query<'query, 'local, 'd, I> {
48        self.query()
49    }
50}
51
52/// Installed database application owned by one executor generation.
53pub struct Installed<'local, 'd: 'local, I: catalogs::Catalog> {
54    phase: ptr::NonNull<Phase<'local, 'd, I>>,
55    ownership: marker::PhantomData<(Phase<'local, 'd, I>, marker::PhantomPinned)>,
56}
57
58impl<'local, 'd: 'local, I: catalogs::Catalog> client::Provider<'d> for Installed<'local, 'd, I> {
59    type Client<'app>
60        = Result<Connection<'app, 'local, 'd, I>, crate::Error>
61    where
62        Self: 'app,
63        'd: 'app;
64
65    fn provide<'app>(context: client::Context<'_, 'app, 'd, Self>) -> Self::Client<'app>
66    where
67        Self: 'app,
68        'd: 'app,
69    {
70        Lease::try_new(context).map(Connection::new)
71    }
72}
73
74const _: () = Connection::<catalogs::Empty>::LAYOUT;
75
76const _: () = {
77    assert!(mem::size_of::<Installed<'_, '_, catalogs::Empty>>() == mem::size_of::<usize>());
78    assert!(mem::align_of::<Installed<'_, '_, catalogs::Empty>>() == mem::align_of::<usize>());
79};