Crate mpsc_requests

source ·
Expand description

mpsc_requests is a small library built on top of std::sync::mpsc 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 mpsc_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

A object expected tois a request which is received from the Responder poll method
Requester has a connection to a Responder which it can send requests to
A Responder listens to requests of a specific type and responds back to the Requester

Enums

Errors which can occur when a Responder handles a request

Functions

Create a Requester and a Responder with a channel between them