    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31

    //Implementing our Threadpool

    pub struct ThreadPool;

    impl ThreadPool {
        ///Creates a new ThreadPool
        ///
        ///Panics if the number of threads is 0.
        ///
        /// # Panics
        ///
        /// The `new` function will panic if the size is zero.
        pub fn new(size: usize) -> ThreadPool {
            assert!(size > 0);
            ThreadPool
        }

        //This function executes the closure (F) it receives
        //It is modeled after thread::execute.
        //<F> since F is a generic
        pub fn execute<F>(&self, f: F)
        where
            //Trait binds : F is an FnOnce(), it is executed one time.
            //It has the Send trait : used to transfer the closure from on thread to another. (wtf, Rust ?)
            //'static : we don't know how long the thread will execute.
            F: FnOnce() + Send + 'static,
            {
                //...
            }
    }