Skip to content

Commit c1dac1a

Browse files
committed
Docs
1 parent 42083a2 commit c1dac1a

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/lib.rs

+43-2
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,22 @@ impl<I: Channels<S>, S: Service> Deref for WithChannels<I, S> {
506506
}
507507
}
508508

509+
/// A client to the service `S` using the local message type `M` and the remote
510+
/// message type `R`.
511+
///
512+
/// `R` is typically a serializable enum with a case for each possible message
513+
/// type. It can be thought of as the definition of the protocol.
514+
///
515+
/// `M` is typically an enum with a case for each possible message type, where
516+
/// each case is a `WithChannels` struct that extends the inner protocol message
517+
/// with a local tx and rx channel as well as a tracing span to allow for
518+
/// keeping tracing context across async boundaries.
519+
///
520+
/// In some cases, `M` and `R` can be enums for a subset of the protocol. E.g.
521+
/// if you have a subsystem that only handles a part of the messages.
522+
///
523+
/// The service type `S` provides a scope for the protocol messages. It exists
524+
/// so you can use the same message with multiple services.
509525
#[derive(Debug)]
510526
pub struct Client<M, R, S>(ClientInner<M>, PhantomData<(R, S)>);
511527

@@ -528,11 +544,16 @@ impl<M, R, S> From<tokio::sync::mpsc::Sender<M>> for Client<M, R, S> {
528544
}
529545

530546
impl<M, R, S> Client<M, R, S> {
547+
/// Create a new client to a remote service using the given quinn `endpoint`
548+
/// and a socket `addr` of the remote service.
531549
#[cfg(feature = "rpc")]
532550
pub fn quinn(endpoint: quinn::Endpoint, addr: std::net::SocketAddr) -> Self {
533551
Self::boxed(rpc::QuinnRemoteConnection::new(endpoint, addr))
534552
}
535553

554+
/// Create a new client from a `rpc::RemoteConnection` trait object.
555+
/// This is used from crates that want to provide other transports than quinn,
556+
/// such as the iroh transport.
536557
#[cfg(feature = "rpc")]
537558
pub fn boxed(remote: impl rpc::RemoteConnection) -> Self {
538559
Self(ClientInner::Remote(Box::new(remote)), PhantomData)
@@ -725,12 +746,17 @@ pub mod rpc {
725746
BoxedFuture, RequestError, RpcMessage,
726747
};
727748

749+
/// Error that can occur when writing the initial message when doing a
750+
/// cross-process RPC.
728751
#[derive(Debug, thiserror::Error)]
729752
pub enum WriteError {
730-
#[error("error serializing: {0}")]
731-
Io(#[from] io::Error),
753+
/// Error writing to the stream with quinn
732754
#[error("error writing to stream: {0}")]
733755
Quinn(#[from] quinn::WriteError),
756+
/// Generic IO error, e.g. when serializing the message or when using
757+
/// other transports.
758+
#[error("error serializing: {0}")]
759+
Io(#[from] io::Error),
734760
}
735761

736762
impl From<WriteError> for io::Error {
@@ -742,9 +768,21 @@ pub mod rpc {
742768
}
743769
}
744770

771+
/// Trait to abstract over a client connection to a remote service.
772+
///
773+
/// This isn't really that much abstracted, since the result of open_bi must
774+
/// still be a quinn::SendStream and quinn::RecvStream. This is just so we
775+
/// can have different connection implementations for normal quinn connections,
776+
/// iroh connections, and possibly quinn connections with disabled encryption
777+
/// for performance.
778+
///
779+
/// This is done as a trait instead of an enum, so we don't need an iroh
780+
/// dependency in the main crate.
745781
pub trait RemoteConnection: Send + Sync + Debug + 'static {
782+
/// Boxed clone so the trait is dynable.
746783
fn clone_boxed(&self) -> Box<dyn RemoteConnection>;
747784

785+
/// Open a bidirectional stream to the remote service.
748786
fn open_bi(
749787
&self,
750788
) -> BoxedFuture<std::result::Result<(quinn::SendStream, quinn::RecvStream), RequestError>>;
@@ -1058,9 +1096,12 @@ pub mod rpc {
10581096
}
10591097
}
10601098

1099+
/// A request to a service. This can be either local or remote.
10611100
#[derive(Debug)]
10621101
pub enum Request<L, R> {
1102+
/// Local request
10631103
Local(L),
1104+
/// Remote request
10641105
Remote(R),
10651106
}
10661107

src/util.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Utilities
2+
//!
3+
//! This module contains utilities to read and write varints, as well as
4+
//! functions to set up quinn endpoints for local rpc and testing.
15
#[cfg(feature = "test")]
26
#[cfg_attr(quicrpc_docsrs, doc(cfg(feature = "test")))]
37
mod quinn_setup_utils {
@@ -261,7 +265,11 @@ mod varint_util {
261265
Ok(())
262266
}
263267

268+
/// Provides a fn to read a varint from an AsyncRead source.
264269
pub trait AsyncReadVarintExt: AsyncRead + Unpin {
270+
/// Reads a u64 varint from an AsyncRead source, using the Postcard/LEB128 format.
271+
///
272+
/// If the stream is at the end, this returns `Ok(None)`.
265273
fn read_varint_u64(&mut self) -> impl Future<Output = io::Result<Option<u64>>>;
266274
}
267275

@@ -271,9 +279,13 @@ mod varint_util {
271279
}
272280
}
273281

282+
/// Provides a fn to write a varint to an [`io::Write`] target, as well as a
283+
/// helper to write a length-prefixed value.
274284
pub trait WriteVarintExt: std::io::Write {
285+
/// Write a varint
275286
#[allow(dead_code)]
276287
fn write_varint_u64(&mut self, value: u64) -> io::Result<usize>;
288+
/// Write a value with a varint enoded length prefix.
277289
fn write_length_prefixed<T: Serialize>(&mut self, value: T) -> io::Result<()>;
278290
}
279291

0 commit comments

Comments
 (0)