1use crate::block::Block;
2use crate::ctx::Context;
3use crate::dbs::{Options, Transaction};
4use crate::doc::CursorDoc;
5use crate::err::Error;
6use crate::value::Value;
7use revision::revisioned;
8use serde::{Deserialize, Serialize};
9use std::fmt;
10
11pub(crate) const TOKEN: &str = "$surrealdb::private::crate::Future";
12
13#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
14#[serde(rename = "$surrealdb::private::crate::Future")]
15#[revisioned(revision = 1)]
16pub struct Future(pub Block);
17
18impl From<Value> for Future {
19 fn from(v: Value) -> Self {
20 Future(Block::from(v))
21 }
22}
23
24impl Future {
25 pub(crate) async fn compute(
27 &self,
28 ctx: &Context<'_>,
29 opt: &Options,
30 txn: &Transaction,
31 doc: Option<&CursorDoc<'_>>,
32 ) -> Result<Value, Error> {
33 match opt.futures {
35 true => self.0.compute(ctx, opt, txn, doc).await?.ok(),
36 false => Ok(self.clone().into()),
37 }
38 }
39}
40
41impl fmt::Display for Future {
42 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43 write!(f, "<future> {}", self.0)
44 }
45}