pub fn add_slave(&self, mut slave: Slave) -> Result<(u64,JoinHandle<()>), Slave> {
        let state = self.state.lock().unwrap();
        if state.money < slave.value {
            Err(slave)
        } else { // simulate a transaction, taking between 0-1 days
            drop(state); // release this to let time pass in the runtime loop
            let state_clone = Arc::clone(&self.state);
            let duration = rand::thread_rng().gen_range(0..=1);
            Ok((duration, thread::spawn(move || {
                sleep_seconds!(duration);
                let mut state = state_clone.lock().unwrap();
                state.money -= slave.value;
                slave.owned = true;
                state.slaves.push_back(slave);
            })))
        }
    }
/// Create a thread to add a slave to the player's farm after a few seconds.
Create a thread to add a slave to the player's farm after a few seconds.
Create a thread to add a slave to the player ' s farm after a few seconds .
pub fn add_slave(&self, mut slave: Slave) -> Result<(u64,JoinHandle<()>), Slave> {
        let state = self.state.lock().unwrap();
        if state.money < slave.value {
            Err(slave)
        } else { // simulate a transaction, taking between 0-1 days
            drop(state); // release this to let time pass in the runtime loop
            let state_clone = Arc::clone(&self.state);
            let duration = rand::thread_rng().gen_range(0..=1);
            Ok((duration, thread::spawn(move || {
                sleep_seconds!(duration);
                let mut state = state_clone.lock().unwrap();
                state.money -= slave.value;
                slave.owned = true;
                state.slaves.push_back(slave);
            })))
        }
    }
pub fn add_slave ( & self , mut slave : Slave ) -> Result < ( u64 , JoinHandle < ( ) > ) , Slave > { let state = self . state . lock ( ) . unwrap ( ) ; if state . money < slave . value { Err ( slave ) } else { // simulate a transaction, taking between 0-1 days drop ( state ) ; // release this to let time pass in the runtime loop let state_clone = Arc :: clone ( & self . state ) ; let duration = rand :: thread_rng ( ) . gen_range ( 0 ..= 1 ) ; Ok ( ( duration , thread :: spawn ( move | | { sleep_seconds ! ( duration ) ; let mut state = state_clone . lock ( ) . unwrap ( ) ; state . money -= slave . value ; slave . owned = true ; state . slaves . push_back ( slave ) ; } ) ) ) } }
Create a thread to add a slave to the player's farm after a few seconds.
Create a thread to add a slave to the player ' s farm after a few seconds .
&self
slave
Slave
&self
slave
Slave