[][src]Crate crossbeam_requests

mpsc_requests rewritted for crossbeam, written by @stjepang (https://github.com/crossbeam-rs/crossbeam/issues/353#issuecomment-484013974)

mpsc_requests is a small library built on top of crossbeam-channel but with the addition of the consumer responding with a message to the producer. Since the producer no longer only produces and the consumer no longer only consumes, the Producer is renamed to Requester and the Consumer is renamed to Responder.

mpsc_requests is small and lean by only building on top of the rust standard library

A perfect use-case for this library is single-threaded databases which need to be accessed from multiple threads (such as SQLite)

Examples

For more examples, see the examples directory

For even more examples see the tests in the tests directory

Simple echo example

use std::thread;
use crossbeam_requests::channel;

type RequestType = String;
type ResponseType = String;
let (responder, requester) = channel::<RequestType, ResponseType>();
thread::spawn(move || {
    responder.poll_loop(|mut req| {
        req.respond(req.body().clone());
    });
});
let msg = String::from("Hello");
let res = requester.request(msg.clone());
assert_eq!(res, msg);

Structs

Request

A object expected tois a request which is received from the Responder poll method

Requester

Requester has a connection to a Responder which it can send requests to

Responder

A Responder listens to requests of a specific type and responds back to the Requester

Enums

RequestError

Errors which can occur when a Responder handles a request

Functions

channel

Create a Requester and a Responder with a channel between them