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