Struct ump_ng::ReplyContext
source · pub struct ReplyContext<T, E>(/* private fields */);
Expand description
Object used to reply to requests passed to the server.
Implementations§
source§impl<T, E> ReplyContext<T, E>
impl<T, E> ReplyContext<T, E>
sourcepub fn reply(self, data: T) -> Result<(), Error<E>>
pub fn reply(self, data: T) -> Result<(), Error<E>>
Send a reply back to originating client.
Example
use std::thread;
use ump_ng::{channel, MsgType};
let (server, client) = channel::<(), String, String, ()>();
let server_thread = thread::spawn(move || {
let MsgType::Request(data, rctx) = server.wait().unwrap() else {
panic!("Not Request type");
};
let reply = format!("Hello, {}!", data);
rctx.reply(reply).unwrap();
});
let msg = String::from("Client");
let reply = client.req(msg).unwrap();
assert_eq!(reply, "Hello, Client!");
server_thread.join().unwrap();
Semantics
This call is safe to make after the server context has been released.
sourcepub fn fail(self, err: E) -> Result<(), Error<E>>
pub fn fail(self, err: E) -> Result<(), Error<E>>
Return an error to originating client.
This will cause the calling client to return an error. The error passed
in the err
parameter will be wrapped in a Error::App(err)
.
Example
use std::thread;
use ump_ng::{channel, Error, MsgType};
#[derive(Debug, PartialEq)]
enum MyError {
SomeError(String)
}
let (server, client) = channel::<String, String, String, MyError>();
let server_thread = thread::spawn(move || {
let MsgType::Request(_, rctx) = server.wait().unwrap() else {
panic!("Unexpected message type");
};
rctx.fail(MyError::SomeError("failed".to_string())).unwrap();
});
let msg = String::from("Client");
let reply = client.req(msg);
match reply {
Err(Error::App(MyError::SomeError(s))) => {
assert_eq!(s, "failed");
}
_ => {
panic!("Unexpected return value");
}
}
server_thread.join().unwrap();
Semantics
This call is safe to make after the server context has been released.
Trait Implementations§
Auto Trait Implementations§
impl<T, E> !RefUnwindSafe for ReplyContext<T, E>
impl<T, E> Send for ReplyContext<T, E>
impl<T, E> Sync for ReplyContext<T, E>
impl<T, E> Unpin for ReplyContext<T, E>
impl<T, E> !UnwindSafe for ReplyContext<T, E>
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