pub struct Call { /* private fields */ }std only.Expand description
One of our requests, awaiting its answer.
Ends in exactly one outcome: the peer’s result, or a CallError
(rejected, timed out, cancelled, evicted, connection closed). Dropping a
Call does not cancel the request; its answer is then discarded.
§Examples
use rpc_io::{CallError, Endpoint, Router};
let (tx, _rx) = std::sync::mpsc::channel::<String>();
let mut endpoint = Endpoint::new(tx, Router::new(), ());
let call = endpoint.peer().request("x", ())?;
// The peer answers through the endpoint:
endpoint.handle(br#"{"jsonrpc":"2.0","id":1,"result":[42]}"#)?;
assert_eq!(call.wait().map(|raw| raw.as_str().to_owned()), Ok("[42]".to_owned()));Implementations§
Source§impl Call
impl Call
Sourcepub const fn id(&self) -> u64
pub const fn id(&self) -> u64
The request’s id on the wire.
§Examples
let (tx, _rx) = std::sync::mpsc::channel::<String>();
let endpoint = rpc_io::Endpoint::new(tx, rpc_io::Router::new(), ()); // keep it: dropping an endpoint closes it
let peer = endpoint.peer();
assert_eq!(peer.request("a", ())?.id(), 1);
assert_eq!(peer.request("b", ())?.id(), 2);Sourcepub const fn deadline(&self) -> Option<Instant>
pub const fn deadline(&self) -> Option<Instant>
The deadline, if the request has one.
§Examples
let (tx, _rx) = std::sync::mpsc::channel::<String>();
let endpoint = rpc_io::Endpoint::new(tx, rpc_io::Router::new(), ()); // keep it: dropping an endpoint closes it
let peer = endpoint.peer();
assert!(peer.request("a", ())?.deadline().is_none());Sourcepub fn wait(self) -> Result<Raw<'static>, CallError>
pub fn wait(self) -> Result<Raw<'static>, CallError>
Blocks until the answer arrives or the request ends otherwise.
§Errors
The CallError the request ended with. On the endpoint’s own thread
(inside a handler) it returns CallError::WouldDeadlock at once
unless the outcome is already there: that thread is the one that would
read the answer.
§Examples
use rpc_io::{CallError, Endpoint, Router};
let (tx, _rx) = std::sync::mpsc::channel::<String>();
let mut endpoint = Endpoint::new(tx, Router::new(), ());
let call = endpoint.peer().request("x", ())?;
endpoint.close();
assert_eq!(call.wait(), Err(CallError::Closed));Sourcepub fn try_wait(self) -> Result<Result<Raw<'static>, CallError>, Call>
pub fn try_wait(self) -> Result<Result<Raw<'static>, CallError>, Call>
The outcome if it has arrived, or the call back to try again later. Never blocks, so a handler can poll a request it sent earlier.
§Errors
Err(self) while the request is still outstanding.
§Examples
use rpc_io::{Endpoint, Router};
let (tx, _rx) = std::sync::mpsc::channel::<String>();
let mut endpoint = Endpoint::new(tx, Router::new(), ());
let call = endpoint.peer().request("x", ())?;
let call = call.try_wait().err().unwrap(); // not answered yet
endpoint.handle(br#"{"jsonrpc":"2.0","id":1,"result":null}"#)?;
assert!(call.try_wait().is_ok_and(|outcome| outcome.is_ok()));Sourcepub fn cancel(self) -> bool
pub fn cancel(self) -> bool
Cancels the request: the peer is sent the cancel notification and the
outcome becomes CallError::Cancelled. Returns false if the
request had already ended.
§Examples
use rpc_io::{CallError, Endpoint, Router};
let (tx, rx) = std::sync::mpsc::channel::<String>();
let endpoint = Endpoint::new(tx, Router::new(), ()); // keep it: dropping an endpoint closes it
let peer = endpoint.peer();
let call = peer.request("slow", ())?;
assert!(call.cancel());
let sent: Vec<String> = rx.try_iter().collect();
assert_eq!(sent[1], r#"{"jsonrpc":"2.0","method":"$/cancelRequest","params":{"id":1}}"#);Sourcepub fn canceller(&self) -> Canceller
pub fn canceller(&self) -> Canceller
A handle that can cancel this request from elsewhere (another thread,
a later handler) while this Call waits.
§Examples
use rpc_io::{CallError, Endpoint, Router};
let (tx, _rx) = std::sync::mpsc::channel::<String>();
let endpoint = Endpoint::new(tx, Router::new(), ()); // keep it: dropping an endpoint closes it
let peer = endpoint.peer();
let call = peer.request("slow", ())?;
let canceller = call.canceller();
std::thread::spawn(move || canceller.cancel());
assert_eq!(call.wait(), Err(CallError::Cancelled));