===================the error message: ===================
src\main.rs:176:12: 180:7 error: the trait core::marker::Sync is not implemented for the type core::cell::UnsafeCell<std::sync::mpsc::Flavor<i32>> [E0277]
src\main.rs:176 router.add_route("restart".to_string(),|_: &mut Request|{
src\main.rs:177 tx.send(1).unwrap();
src\main.rs:178 println!("a restart requestt");
src\main.rs:179 Ok(Response::with((status::Ok, "ok")))
src\main.rs:180 });
src\main.rs:176:12: 180:7 help: run rustc --explain E0277 to see a detailed explanation
src\main.rs:176:12: 180:7 note: core::cell::UnsafeCell<std::sync::mpsc::Flavor<i32>> cannot be shared between threads safely
src\main.rs:176:12: 180:7 note: required because it appears within the type std::sync::mpsc::Sender<i32>
src\main.rs:176:12: 180:7 note: required because it appears within the type [closure@src\main.rs:176:44: 180:6 tx:&std::sync::mpsc::Sender<i32>]
error: aborting due to previous error
The move keyword will make sure that the environment, including the Sender, is moved into the closure instead of just referenced. Then it shouldn't require Sync.
Never mind, I think I might know what's up. The handler can be shared among multiple threads, so the sender has to be Sync. One way to work around it is to Mutex lock it, but there may be a better solution.
i use Mutex lock solves the problem.
but, have a new problem:
let (tx,rx)= channel();
let sh = std::sync::Arc::new(std::sync::Mutex::new(tx));
let rh = std::sync::Arc::new(std::sync::Mutex::new(rx));
router.add_route("shutdown".to_string(),move|_: &mut Request|{
&sh.lock().unwrap().send("a shutdown requestt").unwrap();
Ok(Response::with((status::Ok, "ok")))
});
router.add_route("restart".to_string(), move|_: &mut Request|{
&sh.lock().unwrap().send("a restart request").unwrap();
Ok(Response::with((status::Ok, "ok")))
});
===================the error message: ===================
src\main.rs:176:10: 176:12 error: capture of moved value: sh [E0382]
src\main.rs:176 &sh.lock().unwrap().send("a restart request").unwrap();
^~
src\main.rs:176:10: 176:12 help: run rustc --explain E0382 to see a detailed explanation
src\main.rs:171:45: 174:6 note: sh moved into closure environment here because it has type alloc::arc::Arc<std::sync::mutex::Mutex<std::sync::mpsc::Sender<&'static str>>>, which is non-copyable
src\main.rs:171 router.add_route("shutdown".to_string(),move|_: &mut Request|{
src\main.rs:172 &sh.lock().unwrap().send("a shutdown requestt").unwrap();
src\main.rs:173 Ok(Response::with((status::Ok, "ok")))
src\main.rs:174 });
src\main.rs:171:45: 174:6 help: perhaps you meant to use clone()?
error: aborting due to previous error