Skip to content

Commit 9a482bf

Browse files
SteveLauCdwrensha
authored andcommitted
docs: document capnrpc/src/lib.rs
1 parent a29f92b commit 9a482bf

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

capnp-rpc/src/lib.rs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub mod rpc_capnp;
8181
/// [rpc-twoparty.capnp](https://github.com/capnproto/capnproto/blob/master/c%2B%2B/src/capnp/rpc-twoparty.capnp).
8282
pub mod rpc_twoparty_capnp;
8383

84-
/// Like `try!()`, but for functions that return a `Promise<T, E>` rather than a `Result<T, E>`.
84+
/// Like [`try!()`], but for functions that return a [`Promise<T, E>`] rather than a [`Result<T, E>`].
8585
///
8686
/// Unwraps a `Result<T, E>`. In the case of an error `Err(e)`, immediately returns from the
8787
/// enclosing function with `Promise::err(e)`.
@@ -110,8 +110,15 @@ pub mod twoparty;
110110

111111
use capnp::message;
112112

113+
/// A message to be sent by a [`VatNetwork`].
113114
pub trait OutgoingMessage {
115+
/// Gets the message body, which the caller may fill in any way it wants.
116+
///
117+
/// The standard RPC implementation initializes it as a Message as defined
118+
/// in `schema/rpc.capnp`.
114119
fn get_body(&mut self) -> ::capnp::Result<::capnp::any_pointer::Builder>;
120+
121+
/// Same as `get_body()`, but returns the corresponding reader type.
115122
fn get_body_as_reader(&self) -> ::capnp::Result<::capnp::any_pointer::Reader>;
116123

117124
/// Sends the message. Returns a promise for the message that resolves once the send has completed.
@@ -123,44 +130,73 @@ pub trait OutgoingMessage {
123130
Rc<message::Builder<message::HeapAllocator>>,
124131
);
125132

133+
/// Takes the inner message out of `self`.
126134
fn take(self: Box<Self>) -> ::capnp::message::Builder<::capnp::message::HeapAllocator>;
127135
}
128136

137+
/// A message received from a [`VatNetwork`].
129138
pub trait IncomingMessage {
139+
/// Gets the message body, to be interpreted by the caller.
140+
///
141+
/// The standard RPC implementation interprets it as a Message as defined
142+
/// in `schema/rpc.capnp`.
130143
fn get_body(&self) -> ::capnp::Result<::capnp::any_pointer::Reader>;
131144
}
132145

146+
/// A two-way RPC connection.
147+
///
148+
/// A connection can be created by [`VatNetwork::connect()`].
133149
pub trait Connection<VatId> {
150+
/// Returns the connected vat's authenticated VatId. It is the VatNetwork's
151+
/// responsibility to authenticate this, so that the caller can be assured
152+
/// that they are really talking to the identified vat and not an imposter.
134153
fn get_peer_vat_id(&self) -> VatId;
154+
155+
/// Allocates a new message to be sent on this connection.
156+
///
157+
/// If `first_segment_word_size` is non-zero, it should be treated as a
158+
/// hint suggesting how large to make the first segment. This is entirely
159+
/// a hint and the connection may adjust it up or down. If it is zero,
160+
/// the connection should choose the size itself.
135161
fn new_outgoing_message(&mut self, first_segment_word_size: u32) -> Box<dyn OutgoingMessage>;
136162

137163
/// Waits for a message to be received and returns it. If the read stream cleanly terminates,
138164
/// returns None. If any other problem occurs, returns an Error.
139165
fn receive_incoming_message(&mut self) -> Promise<Option<Box<dyn IncomingMessage>>, Error>;
140166

141-
// Waits until all outgoing messages have been sent, then shuts down the outgoing stream. The
142-
// returned promise resolves after shutdown is complete.
167+
/// Waits until all outgoing messages have been sent, then shuts down the outgoing stream. The
168+
/// returned promise resolves after shutdown is complete.
143169
fn shutdown(&mut self, result: ::capnp::Result<()>) -> Promise<(), Error>;
144170
}
145171

172+
/// Network facility between vats, it determines how to form connections between
173+
/// vats.
174+
///
175+
/// ## Vat
176+
///
177+
/// Cap'n Proto RPC operates between vats, where a "vat" is some sort of host of
178+
/// objects. Typically one Cap'n Proto process (in the Unix sense) is one vat.
146179
pub trait VatNetwork<VatId> {
147-
/// Returns None if `hostId` refers to the local vat.
180+
/// Connects to `host_id`.
181+
///
182+
/// Returns None if `host_id` refers to the local vat.
148183
fn connect(&mut self, host_id: VatId) -> Option<Box<dyn Connection<VatId>>>;
149184

150185
/// Waits for the next incoming connection and return it.
151186
fn accept(&mut self) -> Promise<Box<dyn Connection<VatId>>, ::capnp::Error>;
152187

188+
/// A promise that cannot be resolved until the shutdown.
153189
fn drive_until_shutdown(&mut self) -> Promise<(), Error>;
154190
}
155191

156192
/// A portal to objects available on the network.
157193
///
158-
/// The RPC implemententation sits on top of an implementation of `VatNetwork`, which
194+
/// The RPC implementation sits on top of an implementation of [`VatNetwork`], which
159195
/// determines how to form connections between vats. The RPC implementation determines
160196
/// how to use such connections to manage object references and make method calls.
161197
///
162198
/// At the moment, this is all rather more general than it needs to be, because the only
163-
/// implementation of `VatNetwork` is `twoparty::VatNetwork`. However, eventually we
199+
/// implementation of `VatNetwork` is [`twoparty::VatNetwork`]. However, eventually we
164200
/// will need to have more sophisticated `VatNetwork` implementations, in order to support
165201
/// [level 3](https://capnproto.org/rpc.html#protocol-features) features.
166202
///
@@ -228,7 +264,8 @@ impl<VatId> RpcSystem<VatId> {
228264
result
229265
}
230266

231-
/// Connects to the given vat and returns its bootstrap interface.
267+
/// Connects to the given vat and returns its bootstrap interface, returns
268+
/// a client that can be used to invoke the bootstrap interface.
232269
pub fn bootstrap<T>(&mut self, vat_id: VatId) -> T
233270
where
234271
T: ::capnp::capability::FromClientHook,

capnp-rpc/src/task_set.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ where
114114
}
115115
}
116116

117+
/// For a specific kind of task, `TaskReaper` defines the procedure that should
118+
/// be invoked when it succeeds or fails.
117119
pub trait TaskReaper<E>
118120
where
119121
E: 'static,

capnp-rpc/src/twoparty.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
// THE SOFTWARE.
2121

22-
//! An implementation of `VatNetwork` for the common case of a client-server connection.
22+
//! An implementation of [`VatNetwork`](crate::VatNetwork) for the common case
23+
//! of a client-server connection.
2324
2425
use capnp::capability::Promise;
2526
use capnp::message::ReaderOptions;
@@ -205,6 +206,7 @@ where
205206
side: crate::rpc_twoparty_capnp::Side,
206207
}
207208

209+
/// A two-party vat `VatNetwork` implementation.
208210
impl<T> VatNetwork<T>
209211
where
210212
T: AsyncRead + Unpin,

0 commit comments

Comments
 (0)