Correctly halts
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
use std::any::Any;
|
||||
use std::cell::RefCell;
|
||||
use std::future::Future;
|
||||
use std::marker::PhantomData;
|
||||
@@ -41,19 +40,19 @@ fn get_id(message: &[u8]) -> (u64, &[u8]) {
|
||||
}
|
||||
|
||||
pub trait ReqHandlish {
|
||||
fn defer_drop(&self, val: impl Any + 'static)
|
||||
fn defer(&self, cb: impl Future<Output = ()> + 'static)
|
||||
where Self: Sized {
|
||||
self.defer_drop_objsafe(Box::new(val));
|
||||
self.defer_objsafe(Box::pin(cb));
|
||||
}
|
||||
fn defer_drop_objsafe(&self, val: Box<dyn Any>);
|
||||
fn defer_objsafe(&self, val: Pin<Box<dyn Future<Output = ()>>>);
|
||||
}
|
||||
impl ReqHandlish for &'_ dyn ReqHandlish {
|
||||
fn defer_drop_objsafe(&self, val: Box<dyn Any>) { (**self).defer_drop_objsafe(val) }
|
||||
fn defer_objsafe(&self, val: Pin<Box<dyn Future<Output = ()>>>) { (**self).defer_objsafe(val) }
|
||||
}
|
||||
|
||||
#[derive(destructure)]
|
||||
pub struct RequestHandle<'a, MS: MsgSet> {
|
||||
defer_drop: RefCell<Vec<Box<dyn Any>>>,
|
||||
defer: RefCell<Vec<Pin<Box<dyn Future<Output = ()>>>>>,
|
||||
fulfilled: AtomicBool,
|
||||
id: u64,
|
||||
_reqlt: PhantomData<&'a mut ()>,
|
||||
@@ -61,13 +60,7 @@ pub struct RequestHandle<'a, MS: MsgSet> {
|
||||
}
|
||||
impl<'a, MS: MsgSet + 'static> RequestHandle<'a, MS> {
|
||||
fn new(parent: ReqNot<MS>, id: u64) -> Self {
|
||||
Self {
|
||||
defer_drop: RefCell::default(),
|
||||
fulfilled: false.into(),
|
||||
_reqlt: PhantomData,
|
||||
parent,
|
||||
id,
|
||||
}
|
||||
Self { defer: RefCell::default(), fulfilled: false.into(), _reqlt: PhantomData, parent, id }
|
||||
}
|
||||
pub fn reqnot(&self) -> ReqNot<MS> { self.parent.clone() }
|
||||
pub async fn handle<U: Request>(&self, _: &U, rep: &U::Response) -> Receipt<'a> {
|
||||
@@ -83,11 +76,17 @@ impl<'a, MS: MsgSet + 'static> RequestHandle<'a, MS> {
|
||||
response.encode(Pin::new(&mut buf)).await;
|
||||
let mut send = clone_box(&*self.reqnot().0.lock().await.send);
|
||||
(send)(&buf, self.parent.clone()).await;
|
||||
let deferred = mem::take(&mut *self.defer.borrow_mut());
|
||||
for item in deferred {
|
||||
item.await
|
||||
}
|
||||
Receipt(PhantomData)
|
||||
}
|
||||
}
|
||||
impl<MS: MsgSet> ReqHandlish for RequestHandle<'_, MS> {
|
||||
fn defer_drop_objsafe(&self, val: Box<dyn Any>) { self.defer_drop.borrow_mut().push(val); }
|
||||
fn defer_objsafe(&self, val: Pin<Box<dyn Future<Output = ()>>>) {
|
||||
self.defer.borrow_mut().push(val)
|
||||
}
|
||||
}
|
||||
impl<MS: MsgSet> Drop for RequestHandle<'_, MS> {
|
||||
fn drop(&mut self) {
|
||||
@@ -236,7 +235,10 @@ impl<This: DynRequester + ?Sized> Requester for This {
|
||||
async fn request<R: Request + Into<Self::Transfer>>(&self, data: R) -> R::Response {
|
||||
let req = format!("{data:?}");
|
||||
let rep = R::Response::decode(Pin::new(&mut &self.raw_request(data.into()).await[..])).await;
|
||||
writeln!(self.logger(), "Request {req} got response {rep:?}");
|
||||
let req_str = req.to_string();
|
||||
if !req_str.starts_with("AtomPrint") && !req_str.starts_with("ExtAtomPrint") {
|
||||
writeln!(self.logger(), "Request {req} got response {rep:?}");
|
||||
}
|
||||
rep
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user