Struct reqchan::RequestContract
[−]
[src]
pub struct RequestContract<T> { /* fields omitted */ }
This is the contract returned by a successful Requester::try_request()
.
It represents the caller's exclusive access to the requesting side of
the channel. The user can either try to get a datum from the responding side
or attempt to cancel the request. To prevent data loss, RequestContract
will panic if the user has not received a datum or cancelled the request.
Methods
impl<T> RequestContract<T>
[src]
fn try_receive(&mut self) -> Result<T, TryReceiveError>
[src]
This method attempts to receive a datum from one or more responder(s).
Warning
It returns Err(TryReceiveError::Done)
if the user called it
after either receiving a datum or cancelling the request.
Example
extern crate reqchan as chan; let (requester, responder) = chan::channel::<u32>(); let mut request_contract = requester.try_request().unwrap(); // The responder has not responded yet. match request_contract.try_receive() { Err(chan::TryReceiveError::Empty) => { println!("No Data yet!"); }, _ => unreachable!(), } responder.try_respond().unwrap().send(6); // The responder has responded now. match request_contract.try_receive() { Ok(num) => { println!("Number: {}", num); }, _ => unreachable!(), } // We need to issue another request to receive more data. match request_contract.try_receive() { Err(chan::TryReceiveError::Done) => { println!("We already received data!"); }, _ => unreachable!(), }
fn try_cancel(&mut self) -> bool
[src]
This method attempts to cancel a request. This is useful for implementing a timeout.
Return
true
- Cancelled requestfalse
-Responder
started processing request first
Warning
It also returns false
if the user called it after
either receiving a datum or cancelling the request.
Example
extern crate reqchan as chan; let (requester, responder) = chan::channel::<u32>(); { let mut contract = requester.try_request().unwrap(); // We can cancel the request since `responder` has not // yet responded to it. assert_eq!(contract.try_cancel(), true); // Both contracts go out of scope here } { let mut request_contract = requester.try_request().unwrap(); responder.try_respond().unwrap().send(7); // It is too late to cancel the request! if !request_contract.try_cancel() { println!("Number: {}", request_contract.try_receive().unwrap()); } // Both contracts go out of scope here }