pub struct Promise<T> { /* private fields */ }Expand description
Result of a queued request or reply.
Requests return Promise<Message>; their wait selects the expected response
type or takes the message directly. Replies return Promise<()>; their wait
observes local writing and flushing, not peer receipt or processing.
Dropping a promise leaves the request or reply running with its original deadline. A timeout does not mean the peer stopped working. Completed results remain available after the session closes.
A buffered response counts toward the session’s inbound byte limit until
wait() or drop. It stays encoded until wait() decodes it. Late answers and
answers with no matching request are discarded. Answers whose promises were
dropped are also discarded. Their payloads are never decoded.
Each promise returns its result once:
use darkbio_wire::protocol::schema::DeviceInfoResponse;
use darkbio_wire::protocol::{Message, Promise};
fn take_twice(promise: Promise<Message>) {
let _ = promise.wait::<DeviceInfoResponse>();
let _ = promise.wait::<DeviceInfoResponse>();
}Implementations§
Source§impl Promise<Message>
impl Promise<Message>
Sourcepub fn wait<T>(self) -> Result<T, Error>
pub fn wait<T>(self) -> Result<T, Error>
Blocks for completion, then decodes the response. The response must be accepted before the request’s original deadline. Decoding is outside that deadline. An accepted response remains available after the deadline or closure.
Taking the response removes its bytes from the inbound byte count before
decoding it. Invalid protobuf returns Error::Malformed and closes its
original session.
Selects the expected response type at this call, either through inference
or wait::<Response>(). Message extraction checks the content variant and
returns Error::UnexpectedResponse on mismatch. The Message enum can
also be taken directly for application pattern matching.
Source§impl<T> Promise<T>
impl<T> Promise<T>
Sourcepub fn notify<E: Copy + Send + 'static>(&mut self, sender: Sender<E>, event: E)
pub fn notify<E: Copy + Send + 'static>(&mut self, sender: Sender<E>, event: E)
Sends event through the unbounded channel once a terminal result is ready.
Requests notify on a response or error; replies notify on local write/flush
completion or error. Notification does not imply success or peer receipt.
The result is published before the event, so wait() can then extract it
without waiting for completion. Response decoding still happens in wait().
Registering or receiving a notification neither decodes the response nor
releases its retained bytes. They remain charged until wait() or drop.
Deadlines are unchanged, and registration does not service expiry.
An already-completed promise sends immediately on the registering thread, even after its session is gone. Otherwise the thread settling the operation sends the event. A disconnected notification receiver discards the event without affecting the result. Copy tokens cannot run application destructors on a protocol worker; keep any associated payload on the consumer’s side.
Dropping the promise clears an unsent notification without cancelling the operation. An event already sent can outlive its promise.
§Panics
Panics if notification was already registered on this promise.