surrealdb/api/method/
use_db.rs1use crate::api::conn::Method;
2use crate::api::conn::Param;
3use crate::api::Connection;
4use crate::api::Result;
5use crate::method::OnceLockExt;
6use crate::opt::WaitFor;
7use crate::sql::Value;
8use crate::Surreal;
9use std::borrow::Cow;
10use std::future::Future;
11use std::future::IntoFuture;
12use std::pin::Pin;
13
14#[derive(Debug)]
15#[must_use = "futures do nothing unless you `.await` or poll them"]
16pub struct UseDb<'r, C: Connection> {
17 pub(super) client: Cow<'r, Surreal<C>>,
18 pub(super) ns: Value,
19 pub(super) db: String,
20}
21
22impl<C> UseDb<'_, C>
23where
24 C: Connection,
25{
26 pub fn into_owned(self) -> UseDb<'static, C> {
28 UseDb {
29 client: Cow::Owned(self.client.into_owned()),
30 ..self
31 }
32 }
33}
34
35impl<'r, Client> IntoFuture for UseDb<'r, Client>
36where
37 Client: Connection,
38{
39 type Output = Result<()>;
40 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + Sync + 'r>>;
41
42 fn into_future(self) -> Self::IntoFuture {
43 Box::pin(async move {
44 let mut conn = Client::new(Method::Use);
45 conn.execute_unit(
46 self.client.router.extract()?,
47 Param::new(vec![self.ns, self.db.into()]),
48 )
49 .await?;
50 self.client.waiter.0.send(Some(WaitFor::Database)).ok();
51 Ok(())
52 })
53 }
54}