initial commit
This commit is contained in:
8
exercises/07_threads/11_locks/Cargo.toml
Normal file
8
exercises/07_threads/11_locks/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "locks"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
thiserror = "1.0.60"
|
||||
ticket_fields = { path = "../../../helpers/ticket_fields" }
|
||||
23
exercises/07_threads/11_locks/src/data.rs
Normal file
23
exercises/07_threads/11_locks/src/data.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::store::TicketId;
|
||||
use ticket_fields::{TicketDescription, TicketTitle};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Ticket {
|
||||
pub id: TicketId,
|
||||
pub title: TicketTitle,
|
||||
pub description: TicketDescription,
|
||||
pub status: Status,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TicketDraft {
|
||||
pub title: TicketTitle,
|
||||
pub description: TicketDescription,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
|
||||
pub enum Status {
|
||||
ToDo,
|
||||
InProgress,
|
||||
Done,
|
||||
}
|
||||
88
exercises/07_threads/11_locks/src/lib.rs
Normal file
88
exercises/07_threads/11_locks/src/lib.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
// TODO: Fill in the missing methods for `TicketStore`.
|
||||
// Notice how we no longer need a separate update command: `Get` now returns a handle to the ticket
|
||||
// which allows the caller to both modify and read the ticket.
|
||||
use std::sync::mpsc::{sync_channel, Receiver, SyncSender, TrySendError};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::data::{Ticket, TicketDraft};
|
||||
use crate::store::{TicketId, TicketStore};
|
||||
|
||||
pub mod data;
|
||||
pub mod store;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TicketStoreClient {
|
||||
sender: SyncSender<Command>,
|
||||
}
|
||||
|
||||
impl TicketStoreClient {
|
||||
pub fn insert(&self, draft: TicketDraft) -> Result<TicketId, OverloadedError> {
|
||||
let (response_sender, response_receiver) = sync_channel(1);
|
||||
self.sender
|
||||
.try_send(Command::Insert {
|
||||
draft,
|
||||
response_channel: response_sender,
|
||||
})
|
||||
.map_err(|_| OverloadedError)?;
|
||||
Ok(response_receiver.recv().unwrap())
|
||||
}
|
||||
|
||||
pub fn get(&self, id: TicketId) -> Result<Option<Arc<Mutex<Ticket>>>, OverloadedError> {
|
||||
let (response_sender, response_receiver) = sync_channel(1);
|
||||
self.sender
|
||||
.try_send(Command::Get {
|
||||
id,
|
||||
response_channel: response_sender,
|
||||
})
|
||||
.map_err(|_| OverloadedError)?;
|
||||
Ok(response_receiver.recv().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("The store is overloaded")]
|
||||
pub struct OverloadedError;
|
||||
|
||||
pub fn launch(capacity: usize) -> TicketStoreClient {
|
||||
let (sender, receiver) = sync_channel(capacity);
|
||||
std::thread::spawn(move || server(receiver));
|
||||
TicketStoreClient { sender }
|
||||
}
|
||||
|
||||
enum Command {
|
||||
Insert {
|
||||
draft: TicketDraft,
|
||||
response_channel: SyncSender<TicketId>,
|
||||
},
|
||||
Get {
|
||||
id: TicketId,
|
||||
response_channel: SyncSender<Option<Arc<Mutex<Ticket>>>>,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn server(receiver: Receiver<Command>) {
|
||||
let mut store = TicketStore::new();
|
||||
loop {
|
||||
match receiver.recv() {
|
||||
Ok(Command::Insert {
|
||||
draft,
|
||||
response_channel,
|
||||
}) => {
|
||||
let id = store.add_ticket(draft);
|
||||
let _ = response_channel.send(id);
|
||||
}
|
||||
Ok(Command::Get {
|
||||
id,
|
||||
response_channel,
|
||||
}) => {
|
||||
let ticket = store.get(id);
|
||||
let _ = response_channel.send(ticket);
|
||||
}
|
||||
Err(_) => {
|
||||
// There are no more senders, so we can safely break
|
||||
// and shut down the server.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
40
exercises/07_threads/11_locks/src/store.rs
Normal file
40
exercises/07_threads/11_locks/src/store.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use crate::data::{Status, Ticket, TicketDraft};
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct TicketId(u64);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TicketStore {
|
||||
tickets: BTreeMap<TicketId, Arc<Mutex<Ticket>>>,
|
||||
counter: u64,
|
||||
}
|
||||
|
||||
impl TicketStore {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tickets: BTreeMap::new(),
|
||||
counter: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_ticket(&mut self, ticket: TicketDraft) -> TicketId {
|
||||
let id = TicketId(self.counter);
|
||||
self.counter += 1;
|
||||
let ticket = Ticket {
|
||||
id,
|
||||
title: ticket.title,
|
||||
description: ticket.description,
|
||||
status: Status::ToDo,
|
||||
};
|
||||
todo!();
|
||||
id
|
||||
}
|
||||
|
||||
// The `get` method should return a handle to the ticket
|
||||
// which allows the caller to either read or modify the ticket.
|
||||
pub fn get(&self, id: TicketId) -> Option<todo!()> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
31
exercises/07_threads/11_locks/tests/check.rs
Normal file
31
exercises/07_threads/11_locks/tests/check.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use locks::data::{Status, TicketDraft};
|
||||
use locks::launch;
|
||||
use ticket_fields::test_helpers::{ticket_description, ticket_title};
|
||||
|
||||
#[test]
|
||||
fn works() {
|
||||
let client = launch(5);
|
||||
let draft = TicketDraft {
|
||||
title: ticket_title(),
|
||||
description: ticket_description(),
|
||||
};
|
||||
let ticket_id = client.insert(draft.clone()).unwrap();
|
||||
|
||||
let ticket = client.get(ticket_id).unwrap().unwrap();
|
||||
{
|
||||
let mut ticket = ticket.lock().unwrap();
|
||||
assert_eq!(ticket_id, ticket.id);
|
||||
assert_eq!(ticket.status, Status::ToDo);
|
||||
assert_eq!(ticket.title, draft.title);
|
||||
assert_eq!(ticket.description, draft.description);
|
||||
|
||||
ticket.status = Status::InProgress;
|
||||
}
|
||||
|
||||
let ticket = client.get(ticket_id).unwrap().unwrap();
|
||||
{
|
||||
let ticket = ticket.lock().unwrap();
|
||||
assert_eq!(ticket_id, ticket.id);
|
||||
assert_eq!(ticket.status, Status::InProgress);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user