This repository has been archived on 2025-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
rustex/exercises/07_threads/05_channels/src/lib.rs

37 lines
958 B
Rust

use std::sync::mpsc::{Receiver, Sender};
use data::TicketDraft;
use store::TicketStore;
pub mod data;
pub mod store;
pub enum Command {
Insert(TicketDraft),
}
// Start the system by spawning the server thread.
// It returns a `Sender` instance which can then be used
// by one or more clients to interact with the server.
pub fn launch() -> Sender<Command> {
let (sender, receiver) = std::sync::mpsc::channel();
std::thread::spawn(move || server(receiver));
sender
}
// TODO: The server task should **never** stop.
// Enter a loop: wait for a command to show up in
// the channel, then execute it, then start waiting
// for the next command.
pub fn server(receiver: Receiver<Command>) {
let mut tickStor = TicketStore::new();
'aLoop: loop {
let command = receiver.recv().ok().unwrap();
match command {
Command::Insert(a) => {
tickStor.add_ticket(a);
}
}
}
}