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>

source

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.

source

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§

source§

impl<T, E> From<SetCtx<T, RCtxState, E>> for ReplyContext<T, E>

source§

fn from(sctx: SetCtx<T, RCtxState, E>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T, E> !RefUnwindSafe for ReplyContext<T, E>

§

impl<T, E> Send for ReplyContext<T, E>
where E: Send, T: Send,

§

impl<T, E> Sync for ReplyContext<T, E>
where E: Send, T: Send,

§

impl<T, E> Unpin for ReplyContext<T, E>

§

impl<T, E> !UnwindSafe for ReplyContext<T, E>

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>,

§

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>,

§

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.