semantic_operators/
blocking.rs1use crate::{Answers, Error, ErrorKind, Provider, Request, Result, ask};
3pub struct Blocking<P: Provider> {
5 provider: P,
6 runtime: Option<tokio::runtime::Runtime>,
7}
8impl<P: Provider> Blocking<P> {
9 pub fn new(provider: P) -> Result<Self> {
14 let runtime = tokio::runtime::Builder::new_multi_thread()
15 .enable_all()
16 .build()
17 .map_err(|_| Error::new(ErrorKind::Runtime, "unable to create blocking runtime"))?;
18 Ok(Self {
19 provider,
20 runtime: Some(runtime),
21 })
22 }
23 pub fn ask(&self, request: &Request) -> Result<Answers> {
30 if tokio::runtime::Handle::try_current().is_ok() {
31 return Err(Error::new(
32 ErrorKind::Runtime,
33 "blocking facade cannot run inside a Tokio runtime",
34 ));
35 }
36 self.runtime
37 .as_ref()
38 .ok_or_else(|| Error::new(ErrorKind::Runtime, "runtime is shut down"))?
39 .block_on(ask(&self.provider, request))
40 }
41}
42impl<P: Provider> Drop for Blocking<P> {
43 fn drop(&mut self) {
44 if let Some(runtime) = self.runtime.take() {
45 runtime.shutdown_background();
46 }
47 }
48}