pub struct SideA<Req, Resp> { /* private fields */ }Expand description
Side A endpoint (request sender, response receiver) - can be cloned
A 方的 channel 端点(请求发送方,响应接收方)- 可以克隆
Implementations§
Source§impl<Req, Resp> SideA<Req, Resp>
impl<Req, Resp> SideA<Req, Resp>
Sourcepub async fn request(&self, req: Req) -> Result<Resp, ChannelError>
pub async fn request(&self, req: Req) -> Result<Resp, ChannelError>
Send a request and wait for response
This method will:
- Push request to the queue
- Wait for side B to process and respond
- Return the response
发送请求并等待响应
这个方法会:
- 将请求推入队列
- 等待 B 方处理并响应
- 返回响应
§Returns
Ok(response): Received response from side BErr(ChannelError::Closed): Side B has been closed
§Example
let (side_a, side_b) = channel::<String, i32>();
tokio::spawn(async move {
while let Ok(guard) = side_b.recv_request().await {
let len = guard.request().len() as i32;
guard.reply(len);
}
});
let response = side_a.request("Hello".to_string()).await;
assert_eq!(response, Ok(5));Sourcepub fn try_request(
&self,
req: Req,
) -> Result<impl Future<Output = Result<Resp, ChannelError>>, ChannelError>
pub fn try_request( &self, req: Req, ) -> Result<impl Future<Output = Result<Resp, ChannelError>>, ChannelError>
Try to send a request without waiting for response
Returns a future that will resolve to the response.
尝试发送请求但不等待响应
返回一个 future,将解析为响应。
Trait Implementations§
Auto Trait Implementations§
impl<Req, Resp> Freeze for SideA<Req, Resp>
impl<Req, Resp> !RefUnwindSafe for SideA<Req, Resp>
impl<Req, Resp> Send for SideA<Req, Resp>
impl<Req, Resp> Sync for SideA<Req, Resp>
impl<Req, Resp> Unpin for SideA<Req, Resp>
impl<Req, Resp> !UnwindSafe for SideA<Req, Resp>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more