SideB

Struct SideB 

Source
pub struct SideB<Req, Resp> { /* private fields */ }
Expand description

Side B endpoint (request receiver, response sender) - single instance

B 方的 channel 端点(请求接收方,响应发送方)- 单实例

Implementations§

Source§

impl<Req, Resp> SideB<Req, Resp>

Source

pub async fn recv_request( &self, ) -> Result<RequestGuard<Req, Resp>, ChannelError>
where Req: Send, Resp: Send,

Wait for and receive next request, returning a guard that must be replied to

The returned RequestGuard enforces that you must call reply() on it. If you drop the guard without calling reply(), it will panic.

等待并接收下一个请求,返回一个必须回复的 guard

返回的 RequestGuard 强制你必须调用 reply()。 如果你在没有调用 reply() 的情况下丢弃 guard,会 panic。

§Returns
  • Ok(RequestGuard): Received request from a side A
  • Err(ChannelError::Closed): All side A instances have 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));
Source

pub async fn handle_request<F>(&self, handler: F) -> Result<(), ChannelError>
where Req: Send, Resp: Send, F: FnOnce(&Req) -> Resp,

Convenient method to handle request and send response

This method will:

  1. Wait for and receive request
  2. Call the handler function
  3. Send the response via the guard

处理请求并发送响应的便捷方法

这个方法会:

  1. 等待并接收请求
  2. 调用处理函数
  3. 通过 guard 发送响应
§Example
let (side_a, side_b) = channel::<String, i32>();
 
tokio::spawn(async move {
    while side_b.handle_request(|req| req.len() as i32).await.is_ok() {
        // Continue handling
    }
});
 
let response = side_a.request("Hello".to_string()).await;
assert_eq!(response, Ok(5));
Source

pub async fn handle_request_async<F, Fut>( &self, handler: F, ) -> Result<(), ChannelError>
where Req: Send, Resp: Send, F: FnOnce(Req) -> Fut, Fut: Future<Output = Resp>,

Convenient async method to handle request and send response

Similar to handle_request, but supports async handler functions. Note: The handler takes ownership of the request to avoid lifetime issues.

处理请求并发送响应的异步便捷方法

handle_request 类似,但支持异步处理函数。 注意:处理函数会获取请求的所有权以避免生命周期问题。

§Example
let (side_a, side_b) = channel::<String, String>();
 
tokio::spawn(async move {
    while side_b.handle_request_async(|req| async move {
        // Async processing - req is owned
        req.to_uppercase()
    }).await.is_ok() {
        // Continue handling
    }
});
 
let response = side_a.request("hello".to_string()).await;
assert_eq!(response, Ok("HELLO".to_string()));

Trait Implementations§

Source§

impl<Req, Resp> Debug for SideB<Req, Resp>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Req, Resp> Drop for SideB<Req, Resp>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<Req, Resp> Freeze for SideB<Req, Resp>

§

impl<Req, Resp> !RefUnwindSafe for SideB<Req, Resp>

§

impl<Req, Resp> Send for SideB<Req, Resp>
where Req: Send, Resp: Send,

§

impl<Req, Resp> Sync for SideB<Req, Resp>
where Req: Send, Resp: Send,

§

impl<Req, Resp> Unpin for SideB<Req, Resp>

§

impl<Req, Resp> !UnwindSafe for SideB<Req, Resp>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.