surrealdb/api/method/
set.rs

1use crate::api::conn::Command;
2use crate::api::method::BoxFuture;
3use crate::api::Connection;
4use crate::api::Result;
5use crate::method::OnceLockExt;
6use crate::Surreal;
7use std::borrow::Cow;
8use std::future::IntoFuture;
9use surrealdb_core::sql::Value as CoreValue;
10
11/// A set future
12#[derive(Debug)]
13#[must_use = "futures do nothing unless you `.await` or poll them"]
14pub struct Set<'r, C: Connection> {
15	pub(super) client: Cow<'r, Surreal<C>>,
16	pub(super) key: String,
17	pub(super) value: Result<CoreValue>,
18}
19
20impl<C> Set<'_, C>
21where
22	C: Connection,
23{
24	/// Converts to an owned type which can easily be moved to a different thread
25	pub fn into_owned(self) -> Set<'static, C> {
26		Set {
27			client: Cow::Owned(self.client.into_owned()),
28			..self
29		}
30	}
31}
32
33impl<'r, Client> IntoFuture for Set<'r, Client>
34where
35	Client: Connection,
36{
37	type Output = Result<()>;
38	type IntoFuture = BoxFuture<'r, Self::Output>;
39
40	fn into_future(self) -> Self::IntoFuture {
41		Box::pin(async move {
42			let router = self.client.router.extract()?;
43			router
44				.execute_unit(Command::Set {
45					key: self.key,
46					value: self.value?,
47				})
48				.await
49		})
50	}
51}