pub fn range(&self, mut rng: Range<usize>, head_start: Option<Duration>) -> Result<(), usize> {
        let (res, mut state_lck) = if rng.is_empty() {
            let state_lck = self.state_lck();
            (Err(state_lck.0), state_lck)
        } else {
            let l = rng.next().expect("check rng is not empty");
            let h = 1 + rng.last().unwrap_or(l);
            let state_lck = self.cv.wait_while(self.state_lck(), |&mut (n, _)| n < l);
            let state_lck = Self::into_guard(state_lck);

            // check the state
            let res = match *state_lck {
                (n, _) if l <= n && n < h => Ok(()),
                (n, _) if n >= h => Err(n),
                _ => unreachable!("passed waypoint before schedule"),
            };
            (res, state_lck)
        };

        // update state
        let (ref mut n, ref mut target_time) = *state_lck;
        *n += 1;
        let now = Instant::now();
        let target_time_this = *target_time;
        *target_time = match (*target_time, head_start) {
            (Some(t), Some(dt)) => Some(std::cmp::max(now, t) + dt),
            (Some(t), None) if now < t => Some(t),
            (Some(_), None) => None,
            (None, Some(dt)) => Some(now + dt),
            (None, None) => None,
        };

        // drop lock before sleeping
        drop(state_lck);

        match target_time_this {
            Some(t) if now < t => std::thread::sleep(t - now),
            _ => {}
        }

        self.cv.notify_all();

        res
    }
/// Allow a waypoint to be passed if the current number is within the range (inclusive lower
/// bound, exclusive upper bound).  This can be used to have multiple threads pass a waypoint
/// concurrently rather than any particular thread being advantaged.  Argument `head_start`
/// represents the minimum amount of time between calling this method and the next waypoint
/// being allowed to pass.  The `Result` is an `Err` if a another waypoint previously use the
/// same waypoint number.
Allow a waypoint to be passed if the current number is within the range (inclusive lower
bound, exclusive upper bound).  This can be used to have multiple threads pass a waypoint
concurrently rather than any particular thread being advantaged.  Argument `head_start`
represents the minimum amount of time between calling this method and the next waypoint
being allowed to pass.  The `Result` is an `Err` if a another waypoint previously use the
same waypoint number.
Allow a waypoint to be passed if the current number is within the range ( inclusive lower bound exclusive upper bound ) . This can be used to have multiple threads pass a waypoint concurrently rather than any particular thread being advantaged . Argument ` head_start ` represents the minimum amount of time between calling this method and the next waypoint being allowed to pass . The ` Result ` is an ` Err ` if a another waypoint previously use the same waypoint number .
pub fn range(&self, mut rng: Range<usize>, head_start: Option<Duration>) -> Result<(), usize> {
        let (res, mut state_lck) = if rng.is_empty() {
            let state_lck = self.state_lck();
            (Err(state_lck.0), state_lck)
        } else {
            let l = rng.next().expect("check rng is not empty");
            let h = 1 + rng.last().unwrap_or(l);
            let state_lck = self.cv.wait_while(self.state_lck(), |&mut (n, _)| n < l);
            let state_lck = Self::into_guard(state_lck);
            // check the state
            let res = match *state_lck {
                (n, _) if l <= n && n < h => Ok(()),
                (n, _) if n >= h => Err(n),
                _ => unreachable!("passed waypoint before schedule"),
            };
            (res, state_lck)
        };
        // update state
        let (ref mut n, ref mut target_time) = *state_lck;
        *n += 1;
        let now = Instant::now();
        let target_time_this = *target_time;
        *target_time = match (*target_time, head_start) {
            (Some(t), Some(dt)) => Some(std::cmp::max(now, t) + dt),
            (Some(t), None) if now < t => Some(t),
            (Some(_), None) => None,
            (None, Some(dt)) => Some(now + dt),
            (None, None) => None,
        };
        // drop lock before sleeping
        drop(state_lck);
        match target_time_this {
            Some(t) if now < t => std::thread::sleep(t - now),
            _ => {}
        }
        self.cv.notify_all();
        res
    }
pub fn range ( & self , mut rng : Range < usize > , head_start : Option < Duration > ) -> Result < ( ) , usize > { let ( res , mut state_lck ) = if rng . is_empty ( ) { let state_lck = self . state_lck ( ) ; ( Err ( state_lck . 0 ) , state_lck ) } else { let l = rng . next ( ) . expect ( " " ) ; let h = 1 + rng . last ( ) . unwrap_or ( l ) ; let state_lck = self . cv . wait_while ( self . state_lck ( ) , | & mut ( n , _ ) | n < l ) ; let state_lck = Self :: into_guard ( state_lck ) ; // check the state let res = match * state_lck { ( n , _ ) if l <= n && n < h => Ok ( ( ) ) , ( n , _ ) if n >= h => Err ( n ) , _ => unreachable ! ( " " ) , } ; ( res , state_lck ) } ; // update state let ( ref mut n , ref mut target_time ) = * state_lck ; * n += 1 ; let now = Instant :: now ( ) ; let target_time_this = * target_time ; * target_time = match ( * target_time , head_start ) { ( Some ( t ) , Some ( dt ) ) => Some ( std :: cmp :: max ( now , t ) + dt ) , ( Some ( t ) , None ) if now < t => Some ( t ) , ( Some ( _ ) , None ) => None , ( None , Some ( dt ) ) => Some ( now + dt ) , ( None , None ) => None , } ; // drop lock before sleeping drop ( state_lck ) ; match target_time_this { Some ( t ) if now < t => std :: thread :: sleep ( t - now ) , _ => { } } self . cv . notify_all ( ) ; res }
Allow a waypoint to be passed if the current number is within the range (inclusive lower
bound, exclusive upper bound).
Allow a waypoint to be passed if the current number is within the range ( inclusive lower bound exclusive upper bound ) .
&self
rng
Range<usize>
head_start
Option<Duration>
&self
rng
Range<usize>
head_start
Option<Duration>