Refactoring take 2

I think I am looking at this the wrong way round, and there is a better way to make these calls.

I looked again at the examples I could find and everything points at my not needing the Arc (For Rocket) because the state struct is threadsafe. When I remove the Arc I got

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

I asked a question on https://users.rust-lang.org/t/multithreaded-rest-api/48163 because I felt like I was randomly trying things and not understanding wth I was doing…

The getAll function was defined like this

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();	
	}

}

The answer to my question did solve the compile issue and I suddenly realised I was thinking in the wrong way about things. I’m not sure why I had this mental block but the function signature is the key part of the misunderstanding.


class test {
	public: 
		test(...)

	public:
		Foo( Bar x ) {}
		Foo( Bar& x ) {}
		Foo( Bar* x ) {}
}

If I call any of the above with the wrong parameter it will fail to compile. If I pass by value then I know it will not affect the value I pass and I can carry on using x after that function. In Rust I pass ownership if I do what looks to be a pass by value , but the function signature is the key

//ignoring pointers because they are different
let x: Bar = ...; //create on stack
let y = &x; //borrow (similar to ref)

Foo myFoo = Foo::new();
Foo::fooFunc(x) //borrow x - self not involved
myFoo.barFunc() //borrow self
myFoo.bazFunc() //owns self 

...
impl Foo {
	new() //create Foo
	fooFunc(b: &Bar) //ignores self 
	barFunc(&self) //borrow self - behaviour I wanted
	bazFunc(self) //take ownership - "move" error above
}


Back to index page of the project is about getting the initial rust API running.