use std::future::Future;
use std::pin::Pin;
use crate::msgbuf::MsgBuf;
use crate::rpc::Rpc;
use crate::session::SSlot;
use crate::type_alias::*;
pub(crate) type ReqHandlerFuture = Pin<Box<dyn Future<Output = MsgBuf> + 'static>>;
pub(crate) type ReqHandler = Box<dyn Fn(RequestHandle) -> ReqHandlerFuture + 'static>;
pub struct RequestHandle {
rpc: *const Rpc,
sslot: *mut SSlot,
}
impl RequestHandle {
#[inline(always)]
pub(crate) fn new<'a>(rpc: &'a Rpc, sslot: &'a mut SSlot) -> Self {
debug_assert!(!sslot.has_handle);
sslot.has_handle = true;
Self { rpc, sslot }
}
}
impl RequestHandle {
#[inline(always)]
pub fn rpc(&self) -> &Rpc {
unsafe { &*self.rpc }
}
#[inline(always)]
pub fn req_type(&self) -> ReqType {
unsafe { (*self.sslot).req_type }
}
#[inline(always)]
pub fn req_buf(&self) -> &MsgBuf {
unsafe { (*self.sslot).req_buf() }
}
#[inline(always)]
pub fn pre_resp_buf(&self) -> MsgBuf {
unsafe { (*self.sslot).pre_resp_msgbuf.clone_borrowed() }
}
}
impl Drop for RequestHandle {
fn drop(&mut self) {
let sslot = unsafe { &mut *self.sslot };
debug_assert!(sslot.has_handle);
sslot.has_handle = false;
}
}