pub struct MsgReader<'s, T: FromPointerReader<'s>> { /* private fields */ }
Expand description
A utility type to read capnproto message types
Implementations§
Source§impl<'s, T: FromPointerReader<'s>> MsgReader<'s, T>
impl<'s, T: FromPointerReader<'s>> MsgReader<'s, T>
Sourcepub fn get_root(&'s self) -> Result<T>
pub fn get_root(&'s self) -> Result<T>
Get the root object from this reader, if it exists
This function returns a reference to the inner reader for you. Because the way this trait is implemented, the parent can’t go out of scope.
To get access to the fields of a type, you need to type-cast
it as a T::Reader
, so to read a service
type (such as the
one provided by this sdk crate), you would cast it as
service::Reader
.
use qrpc_sdk::{parser::MsgReader, types::service};
let msg = MsgReader::new(buf)?;
let r: service::Reader = msg.get_root()?;
println!("DESC: {}", r.get_description()?);
Some types will be capability sets, encoded in an unnamed union. Because this is a very common pattern, here is an example usage of how to implement matching for the types defined in this crate.
use qrpc_sdk::{parser::MsgReader, error::RpcError, rpc::capabilities::{Reader, Which}};
// Get the `reader` by calling `builders::parse_rpc_msg(...)`
let r: Reader = reader.get_root().unwrap();
match r.which() {
Ok(Which::Register(Ok(reg))) => handle_register(reg),
Ok(Which::Unregister(Ok(unreg))) => handle_unregister(unreg),
Ok(Which::Upgrade(Ok(upgr))) => handle_upgrade(upgr),
_ => eprintln!("Invalid variant/ decode!"),
}
use qrpc_sdk::rpc::{register, unregister, upgrade};
fn handle_register(_: register::Reader) { /* ... */}
fn handle_unregister(_: unregister::Reader) { /* ... */}
fn handle_upgrade(_: upgrade::Reader) { /* ... */}
The above code can be found in the qrpc-broker crate. Your own service code will differ, but this should give you a good idea how to start!