initial commit
This commit is contained in:
7
exercises/07_threads/13_without_channels/Cargo.toml
Normal file
7
exercises/07_threads/13_without_channels/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "without_channels"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
ticket_fields = { path = "../../../helpers/ticket_fields" }
|
||||
23
exercises/07_threads/13_without_channels/src/data.rs
Normal file
23
exercises/07_threads/13_without_channels/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,
|
||||
}
|
||||
7
exercises/07_threads/13_without_channels/src/lib.rs
Normal file
7
exercises/07_threads/13_without_channels/src/lib.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
// TODO: You don't actually have to change anything in the library itself!
|
||||
// We mostly had to **remove** code (the client type, the launch function, the command enum)
|
||||
// that's no longer necessary.
|
||||
// Fix the `todo!()` in the testing code and see how the new design can be used.
|
||||
|
||||
pub mod data;
|
||||
pub mod store;
|
||||
40
exercises/07_threads/13_without_channels/src/store.rs
Normal file
40
exercises/07_threads/13_without_channels/src/store.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use crate::data::{Status, Ticket, TicketDraft};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct TicketId(u64);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TicketStore {
|
||||
tickets: BTreeMap<TicketId, Arc<RwLock<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,
|
||||
};
|
||||
let ticket = Arc::new(RwLock::new(ticket));
|
||||
self.tickets.insert(id, ticket);
|
||||
id
|
||||
}
|
||||
|
||||
pub fn get(&self, id: TicketId) -> Option<Arc<RwLock<Ticket>>> {
|
||||
self.tickets.get(&id).cloned()
|
||||
}
|
||||
}
|
||||
40
exercises/07_threads/13_without_channels/tests/check.rs
Normal file
40
exercises/07_threads/13_without_channels/tests/check.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::thread::spawn;
|
||||
|
||||
use ticket_fields::test_helpers::{ticket_description, ticket_title};
|
||||
use without_channels::data::TicketDraft;
|
||||
use without_channels::store::TicketStore;
|
||||
|
||||
#[test]
|
||||
fn works() {
|
||||
let store = todo!();
|
||||
|
||||
let store1 = store.clone();
|
||||
let client1 = spawn(move || {
|
||||
let draft = TicketDraft {
|
||||
title: ticket_title(),
|
||||
description: ticket_description(),
|
||||
};
|
||||
store1.write().unwrap().add_ticket(draft)
|
||||
});
|
||||
|
||||
let store2 = store.clone();
|
||||
let client2 = spawn(move || {
|
||||
let draft = TicketDraft {
|
||||
title: ticket_title(),
|
||||
description: ticket_description(),
|
||||
};
|
||||
store2.write().unwrap().add_ticket(draft)
|
||||
});
|
||||
|
||||
let ticket_id1 = client1.join().unwrap();
|
||||
let ticket_id2 = client2.join().unwrap();
|
||||
|
||||
let reader = store.read().unwrap();
|
||||
|
||||
let ticket1 = reader.get(ticket_id1).unwrap();
|
||||
assert_eq!(ticket_id1, ticket1.read().unwrap().id);
|
||||
|
||||
let ticket2 = reader.get(ticket_id2).unwrap();
|
||||
assert_eq!(ticket_id2, ticket2.read().unwrap().id);
|
||||
}
|
||||
Reference in New Issue
Block a user