orchestra_toolkit/session/api/
raw_msg.rs1use std::{
14 future::{Future, IntoFuture},
15 pin::Pin,
16};
17
18use crate::{hgtp::*, CallError, Session, SessionAsync, SessionTrait};
19
20use super::send_recv;
21
22pub struct RawMessage<T: SessionTrait> {
23 pub(crate) session: T,
24 pub(crate) msg: HGTPMessage,
25}
26impl SessionAsync {
27 #[must_use]
28 pub fn raw_message(&self, msg: HGTPMessage) -> RawMessage<Self> {
29 RawMessage {
30 session: self.clone(),
31 msg,
32 }
33 }
34}
35impl Session {
36 #[must_use]
37 pub fn raw_message(&self, msg: HGTPMessage) -> RawMessage<Self> {
38 RawMessage {
39 session: self.clone(),
40 msg,
41 }
42 }
43}
44impl IntoFuture for RawMessage<SessionAsync> {
45 type IntoFuture = Pin<Box<dyn Future<Output = Result<HGTPMessage, CallError>> + Send>>;
46 type Output = <Self::IntoFuture as Future>::Output;
47 fn into_future(self) -> Self::IntoFuture {
48 Box::pin(async move { self.call_async().await })
49 }
50}
51impl RawMessage<Session> {
52 pub fn call(self) -> Result<HGTPMessage, CallError> {
53 self.session.clone().run_async(|_| self.call_async())
54 }
55}
56impl<T> RawMessage<T> where T: SessionTrait {}
57
58impl<T: SessionTrait> RawMessage<T> {
59 async fn call_async(self) -> Result<HGTPMessage, CallError> {
60 let msg = send_recv(self.msg, self.session.get_socket_pool()).await?;
61 Ok(msg)
62 }
63}