I am new to rust and still thinking in C++ terms (I think)... I have a repo https://github.com/CaribouJohn/todo-api which will contain a simple REST API for Todos Tasks etc. The topic of the code isn't important but the code structure and how I get it to work. I have already learned quite a lot in getting it to the stage it is currently at : See the readme or todo-api | This code is my learning of Rust and it’s intricacies - I wanted to do a fairly well defined easy project that allowed me to create something nearer a real project. It is likely to contain some front end (Angular) before it is done. for the notes I am making while trying things out.
Q. I want to ask how I can structure this so that the struct TaskList
does more of the work and the Rocket
handlers for the API requests can deal with the requests....
outline of code (See repo for actual)
type TaskHashMap = HashMap<u32,Task>;
pub struct TaskList {
pub tasks : Arc<RwLock<TaskHashMap>>,
}
impl TaskList {
pub fn new() -> TaskList {
TaskList {
tasks : Arc::new(RwLock::new(TaskHashMap::new())),
}
}
...
}
//in main functions
#[post("/tasks", data = "<t>")]
fn add_task(state: State<tasks::TaskList>, t: Json<Task>) -> String {
let tl = self.tasks.clone();
let mut my_tasks = tl.read().expect("RWLOCK poisoned");
//we should get the request body here and read in the json body
let o = tasks::Task::new(...);
//insert the object
let r = match my_tasks.insert(new_id, o) {
...
};
...
}
fn main() {
let task_db= tasks::TaskList::new();
rocket::ignite()
.mount("/", routes![get_tasks,add_task,get_task_by_id])
.manage(task_db)
.launch();
}
Basically I have to do all the work in the handlers, and I wanted to have methods implemented for the struct that would do the relevent operations (lock correctly, return a vec of elements etc... add a new one to the list etc....)
Am I thinking too much in a C++ way - I feel like I have missed something fundemental.
E.G. I tried removing the Arc as the state in Rocker seems to be thread safe but :
impl TaskList {
...
pub fn getAll(self) -> String{
let my_tasks = self.tasks.read().expect("Should have allowed read");
let my_vec: Vec<&Task> = my_tasks.iter().map(|(_,v)| v.clone()).collect();
let s = serde_json::to_string(&my_vec).unwrap();
return s.clone();
}
}
When I call it from the handler
error[E0507]: cannot move out of dereference of `rocket::State<'_, tasks::TaskList>`
--> src/main.rs:15:5
|
15 | state.getAll()
| ^^^^^ move occurs because value has type `tasks::TaskList`, which does not implement the `Copy` trait
TIA
--John