Client

Struct Client 

Source
pub struct Client<S: Service>(/* private fields */);
Expand description

A client to the service S using the local message type M and the remote message type R.

R is typically a serializable enum with a case for each possible message type. It can be thought of as the definition of the protocol.

M is typically an enum with a case for each possible message type, where each case is a WithChannels struct that extends the inner protocol message with a local tx and rx channel as well as a tracing span to allow for keeping tracing context across async boundaries.

In some cases, M and R can be enums for a subset of the protocol. E.g. if you have a subsystem that only handles a part of the messages.

The service type S provides a scope for the protocol messages. It exists so you can use the same message with multiple services.

Implementations§

Source§

impl<S: Service> Client<S>

Source

pub fn quinn(endpoint: Endpoint, addr: SocketAddr) -> Self

Available on crate feature rpc only.

Create a new client to a remote service using the given quinn endpoint and a socket addr of the remote service.

Source

pub fn boxed(remote: impl RemoteConnection) -> Self

Available on crate feature rpc only.

Create a new client from a rpc::RemoteConnection trait object. This is used from crates that want to provide other transports than quinn, such as the iroh transport.

Source

pub fn local(tx: impl Into<Sender<S::Message>>) -> Self

Creates a new client from a tokio::sync::mpsc::Sender.

Source

pub fn as_local(&self) -> Option<LocalSender<S>>

Get the local sender. This is useful if you don’t care about remote requests.

Source

pub fn request( &self, ) -> impl Future<Output = Result<Request<LocalSender<S>, RemoteSender<S>>, RequestError>> + 'static

Start a request by creating a sender that can be used to send the initial message to the local or remote service.

In the local case, this is just a clone which has almost zero overhead. Creating a local sender can not fail.

In the remote case, this involves lazily creating a connection to the remote side and then creating a new stream on the underlying quinn or iroh connection.

In both cases, the returned sender is fully self contained.

Source

pub fn rpc<Req, Res>( &self, msg: Req, ) -> impl Future<Output = Result<Res>> + Send + 'static
where S: From<Req>, S::Message: From<WithChannels<Req, S>>, Req: Channels<S, Tx = Sender<Res>, Rx = NoReceiver>, Res: RpcMessage,

Performs a request for which the server returns a oneshot receiver.

Source

pub fn server_streaming<Req, Res>( &self, msg: Req, local_response_cap: usize, ) -> impl Future<Output = Result<Receiver<Res>>> + Send + 'static
where S: From<Req>, S::Message: From<WithChannels<Req, S>>, Req: Channels<S, Tx = Sender<Res>, Rx = NoReceiver>, Res: RpcMessage,

Performs a request for which the server returns a mpsc receiver.

Source

pub fn client_streaming<Req, Update, Res>( &self, msg: Req, local_update_cap: usize, ) -> impl Future<Output = Result<(Sender<Update>, Receiver<Res>)>>
where S: From<Req>, S::Message: From<WithChannels<Req, S>>, Req: Channels<S, Tx = Sender<Res>, Rx = Receiver<Update>>, Update: RpcMessage, Res: RpcMessage,

Performs a request for which the client can send updates.

Source

pub fn bidi_streaming<Req, Update, Res>( &self, msg: Req, local_update_cap: usize, local_response_cap: usize, ) -> impl Future<Output = Result<(Sender<Update>, Receiver<Res>)>> + Send + 'static
where S: From<Req>, S::Message: From<WithChannels<Req, S>>, Req: Channels<S, Tx = Sender<Res>, Rx = Receiver<Update>>, Update: RpcMessage, Res: RpcMessage,

Performs a request for which the client can send updates, and the server returns a mpsc receiver.

Source

pub fn notify<Req>( &self, msg: Req, ) -> impl Future<Output = Result<()>> + Send + 'static
where S: From<Req>, S::Message: From<WithChannels<Req, S>>, Req: Channels<S, Tx = NoSender, Rx = NoReceiver>,

Performs a request for which the server returns nothing.

The returned future completes once the message is sent.

Source

pub fn notify_0rtt<Req>( &self, msg: Req, ) -> impl Future<Output = Result<()>> + Send + 'static
where S: From<Req>, S::Message: From<WithChannels<Req, S>>, Req: Channels<S, Tx = NoSender, Rx = NoReceiver>,

Performs a request for which the server returns nothing.

The returned future completes once the message is sent.

Compared to Self::notify, this variant takes a future that returns true if 0rtt has been accepted. If not, the data is sent again via the same remote channel. For local requests, the future is ignored.

Source

pub fn rpc_0rtt<Req, Res>( &self, msg: Req, ) -> impl Future<Output = Result<Res>> + Send + 'static
where S: From<Req>, S::Message: From<WithChannels<Req, S>>, Req: Channels<S, Tx = Sender<Res>, Rx = NoReceiver>, Res: RpcMessage,

Performs a request for which the server returns a oneshot receiver.

Compared to Self::rpc, this variant takes a future that returns true if 0rtt has been accepted. If not, the data is sent again via the same remote channel. For local requests, the future is ignored.

Source

pub fn server_streaming_0rtt<Req, Res>( &self, msg: Req, local_response_cap: usize, ) -> impl Future<Output = Result<Receiver<Res>>> + Send + 'static
where S: From<Req>, S::Message: From<WithChannels<Req, S>>, Req: Channels<S, Tx = Sender<Res>, Rx = NoReceiver>, Res: RpcMessage,

Performs a request for which the server returns a mpsc receiver.

Compared to Self::server_streaming, this variant takes a future that returns true if 0rtt has been accepted. If not, the data is sent again via the same remote channel. For local requests, the future is ignored.

Trait Implementations§

Source§

impl<S: Service> Clone for Client<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + Service> Debug for Client<S>
where S::Message: Debug,

Source§

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

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

impl<S: Service> From<LocalSender<S>> for Client<S>

Source§

fn from(tx: LocalSender<S>) -> Self

Converts to this type from the input type.
Source§

impl<S: Service> From<Sender<<S as Service>::Message>> for Client<S>

Source§

fn from(tx: Sender<S::Message>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<S> Freeze for Client<S>

§

impl<S> !RefUnwindSafe for Client<S>

§

impl<S> Send for Client<S>

§

impl<S> Sync for Client<S>

§

impl<S> Unpin for Client<S>
where S: Unpin,

§

impl<S> !UnwindSafe for Client<S>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more