Struct Client

Source
pub struct Client<S, R, E>(/* private fields */);
Expand description

Representation of a clonable client object that can issue requests to Server objects.

Each instantiation of a Client object is itself an isolated client with regards to the server context. By cloning a client a new independent client is created. (“Independent” here meaning that it is still tied to the same server object, but the new client can be passed to a separate thread and can independently make calls to the server).

Implementations§

Source§

impl<S, R, E> Client<S, R, E>
where R: 'static + Send, E: 'static + Send,

Source

pub fn req(&self, out: S) -> Result<R, Error<E>>

Send a message to the server, wait for a reply, and return the reply.

A complete round-trip (the message is delivered to the server, and the server sends a reply) must complete before this function returns success.

§Return

On success the function will return Ok(msg).

§Errors

If the linked server object has been released, or is released while the message is in the server’s queue, Error::ServerDisappeared will be returned.

If the server never replied to the message and the reply context was dropped Error::NoReply will be returned.

If an application specific error occurs it will be returned as a Error::App.

§Implementation details

This method is currently reentrant: It is safe to use share a single Client among multiple threads. This may change in the future; it’s recommended to not rely on this. The recommended way to send messages to a server from multiple threads is to clone the Client and assign one separate Client to each thread.

Source

pub fn req_async(&self, out: S) -> Result<WaitReply<R, E>, Error<E>>
where S: Send,

Issue a request, immediately returning a context that is used to wait for the server’s reply.

The _async naming is slightly misleading – this method isn’t an async in a language/Future sense, but rather it doesn’t block and wait for a reply before returning. Instead it returns a WaitReply object that is used to wait for the reply.

This can be useful (in place of req() or areq() methods) if the caller knows that the server will take some time to respond to the request and the caller has other tasks it can perform in the meantime.

use std::thread;

use ump::channel;

let (server, client) = channel::<String, String, ()>();

let server_thread = thread::spawn(move || {
  // Wait for data to arrive from a client
  println!("Server waiting for message ..");
  let (data, mut rctx) = server.wait().unwrap();

  println!("Server received: '{}'", data);

  // Long processing of data from client

  // Reply to client
  let reply = format!("Hello, {}!", data);
  println!("Server replying '{}'", reply);
  rctx.reply(reply);

  println!("Server done");
});

let msg = String::from("Client");
println!("Client sending '{}'", msg);
let wrctx = client.req_async(msg).unwrap();

// .. perform some operation while server is processing the request ..

let reply = wrctx.wait().unwrap();
println!("Client received reply '{}'", reply);
println!("Client done");

server_thread.join().unwrap();
§Errors

If the linked server object has been released, or is released while the message is in the server’s queue, Error::ServerDisappeared will be returned.

Source

pub fn send(&self, out: S) -> Result<R, Error<E>>

👎Deprecated since 0.10.2: Use req() instead.
Source

pub async fn areq(&self, out: S) -> Result<R, Error<E>>
where S: Send,

Same as Client::req() but for use in async contexts.

Source

pub async fn asend(&self, out: S) -> Result<R, Error<E>>
where S: Send,

👎Deprecated since 0.10.2: Use areq() instead.
Source

pub fn weak(&self) -> Weak<S, R, E>

Create a weak Client reference.

Trait Implementations§

Source§

impl<S, R, E> Clone for Client<S, R, E>

Source§

fn clone(&self) -> Self

Clone a client.

When a client is cloned the new object will be linked to the same server, but in all other respects the clone is a completely independent client.

This means that a cloned client can be passed to a new thread/task and make new independent calls to the server without any risk of collision between clone and the original client object.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<S, R, E> Freeze for Client<S, R, E>

§

impl<S, R, E> !RefUnwindSafe for Client<S, R, E>

§

impl<S, R, E> Send for Client<S, R, E>
where S: Send, R: Send, E: Send,

§

impl<S, R, E> Sync for Client<S, R, E>
where S: Send, R: Send, E: Send,

§

impl<S, R, E> Unpin for Client<S, R, E>

§

impl<S, R, E> !UnwindSafe for Client<S, R, 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.