feat(rust): make handle async only
-use ockam_core::{Address, Message, Result};
-
-use crate::{block_future, Context};
+use crate::Context;
+use ockam_core::{async_trait, compat::boxed::Box};
+use ockam_core::{Address, AsyncTryClone, Message, Result};
/// Wrapper for `Context` and `Address`
pub struct Handle {
@@ -8,17 +8,12 @@ pub struct Handle {
address: Address,
}
-impl Clone for Handle {
- fn clone(&self) -> Self {
- block_future(&self.ctx.runtime(), async move {
- Handle {
- ctx: self
- .ctx
- .new_context(Address::random(0))
- .await
- .expect("new_context failed"),
+#[async_trait]
+impl AsyncTryClone for Handle {
+ async fn async_try_clone(&self) -> Result<Self> {
+ Ok(Handle {
+ ctx: self.ctx.new_context(Address::random(0)).await?,
address: self.address.clone(),
- }
})
}
}
@@ -30,43 +25,20 @@ impl Handle {
}
/// Asynchronously sends a message
- pub async fn async_cast<M: Message + Send + 'static>(&self, msg: M) -> Result<()> {
+ pub async fn cast<M: Message + Send + 'static>(&self, msg: M) -> Result<()> {
self.ctx.send(self.address.clone(), msg).await
}
- /// Sends a message that blocks current `Worker` without blocking the executor.
- pub fn cast<M: Message + Send + 'static>(&self, msg: M) -> Result<()> {
- block_future(
- &self.ctx.runtime(),
- async move { self.async_cast(msg).await },
- )
- }
-
/// Asynchronously sends and receiving a message using a new `Context`
- pub async fn async_call<I: Message + Send + 'static, O: Message + Send + 'static>(
+ pub async fn call<I: Message + Send + 'static, O: Message + Send + 'static>(
&self,
msg: I,
) -> Result<O> {
- let mut ctx = self
- .ctx
- .new_context(Address::random(0))
- .await
- .expect("new_context failed");
+ let mut ctx = self.ctx.new_context(Address::random(0)).await?;
ctx.send(self.address.clone(), msg).await?;
let msg = ctx.receive::<O>().await?;
Ok(msg.take().body())
}
-
- /// Send and receiving a message that blocks current `Worker` without blocking the executor.
- pub fn call<I: Message + Send + 'static, O: Message + Send + 'static>(
- &self,
- msg: I,
- ) -> Result<O> {
- block_future(
- &self.ctx.runtime(),
- async move { self.async_call(msg).await },
- )
- }
}
impl Handle {
