I don't know why this code is not ok
use std::thread::{self, JoinHandle};
use std::sync::mpsc::{self, Sender, Receiver};
use std::sync::{Arc, Mutex};
struct Worker;
impl Worker {
fn new<T: FnOnce() + Send + 'static>(receiver: Arc<Mutex<Receiver<Box<T>>>>) -> Worker {
Worker
}
}
fn main() {
let (tx, rx) = mpsc::channel();
let rx: Arc<Mutex<Receiver<Box<FnOnce() + Send + 'static>>>> = Arc::new(Mutex::new(rx));
Worker::new(rx);
}
The compiler told me
error[E0277]: the trait bound `std::ops::FnOnce() + std::marker::Send + 'static: std::marker::Sized` is not satisfied
--> src/main.rs:16:5
|
16 | Worker::new(rx);
| ^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `std::ops::FnOnce() + std::marker::Send + 'static`
|
= note: `std::ops::FnOnce() + std::marker::Send + 'static` does not have a constant size known at compile-time
= note: required by `Worker::new`
but when I use type alias like this, compilation is passed. Can anyone tell me the reason? I'm very confused ... In my opinion, the two methods is same, the type alias "Job" is just a shorten method.
use std::thread::JoinHandle;
use std::thread;
use std::sync::mpsc;
use std::sync::mpsc::{Sender, Receiver};
use std::sync::{Arc, Mutex};
struct Worker;
type Job = Box<FnOnce() + Send + 'static>;
impl Worker {
fn new(receiver: Arc<Mutex<Receiver<Job>>>)->Worker {
Worker
}
}
fn main(){
let (tx, rx) = mpsc::channel();
let rx = Arc::new(Mutex::new(rx));
Worker::new(rx);
}