100 exercises to learn Rust
This commit is contained in:
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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user