fn toposort_visit<T>(
    index: usize,
    items: &mut [MaybeUninit<T>],
    state: &mut [State],
    result: &mut Vec<T>,
    is_edge_from: &dyn Fn(&T, &T) -> bool,
) -> bool {
    match state[index] {
        State::Seen => true,
        State::Visiting => false, // Cycle.
        State::Unseen => {
            // Indicate that the item is being visited to detect cycles and
            // ensure no recursive call to `toposort_visit` can move
            // `items[index]` out of `items`.
            state[index] = State::Visiting;

            // SAFETY: items are only consumed (and therefore uninitialized)
            // when their corresponding state is set to `Seen`. Here, the state
            // is `Unseen`.
            let value = unsafe { &*items[index].as_ptr() };

            // Iterate over all unseen items. If `items[index]` depends on
            // `items[i]`, we recursively call `toposort_visit` to add that
            // dependency to `result` before `items[index]` itself.
            let mut i = 0;

            while let Some(i_state) = state.get(i) {
                if i != index && *i_state != State::Seen {
                    // SAFETY: the `Unseen` and `Visiting` states indicate that
                    // the item has not been moved out of the array yet.
                    let i_value = unsafe { &*items[i].as_ptr() };

                    if is_edge_from(value, i_value) {
                        if !toposort_visit(i, items, state, result, is_edge_from) {
                            // Cycle encountered.
                            return false;
                        }
                    }
                }

                i += 1;
            }

            // SAFETY: item at `index` was `Unseen` and is guaranteed to still
            // have a value in `items[index]`.
            result.push(unsafe {
                std::mem::replace(&mut items[index], MaybeUninit::uninit()).assume_init()
            });
            state[index] = State::Seen;

            true
        }
    }
}
/// Recursively visits the node at the given index; returns `true` if the
/// visit completes successfully, or `false` if a cycle was detected.
Recursively visits the node at the given index; returns `true` if the
visit completes successfully, or `false` if a cycle was detected.
Recursively visits the node at the given index ; returns ` true ` if the visit completes successfully or ` false ` if a cycle was detected .
fn toposort_visit<T>(
    index: usize,
    items: &mut [MaybeUninit<T>],
    state: &mut [State],
    result: &mut Vec<T>,
    is_edge_from: &dyn Fn(&T, &T) -> bool,
) -> bool {
    match state[index] {
        State::Seen => true,
        State::Visiting => false, // Cycle.
        State::Unseen => {
            // Indicate that the item is being visited to detect cycles and
            // ensure no recursive call to `toposort_visit` can move
            // `items[index]` out of `items`.
            state[index] = State::Visiting;
            // SAFETY: items are only consumed (and therefore uninitialized)
            // when their corresponding state is set to `Seen`. Here, the state
            // is `Unseen`.
            let value = unsafe { &*items[index].as_ptr() };
            // Iterate over all unseen items. If `items[index]` depends on
            // `items[i]`, we recursively call `toposort_visit` to add that
            // dependency to `result` before `items[index]` itself.
            let mut i = 0;
            while let Some(i_state) = state.get(i) {
                if i != index && *i_state != State::Seen {
                    // SAFETY: the `Unseen` and `Visiting` states indicate that
                    // the item has not been moved out of the array yet.
                    let i_value = unsafe { &*items[i].as_ptr() };
                    if is_edge_from(value, i_value) {
                        if !toposort_visit(i, items, state, result, is_edge_from) {
                            // Cycle encountered.
                            return false;
                        }
                    }
                }
                i += 1;
            }
            // SAFETY: item at `index` was `Unseen` and is guaranteed to still
            // have a value in `items[index]`.
            result.push(unsafe {
                std::mem::replace(&mut items[index], MaybeUninit::uninit()).assume_init()
            });
            state[index] = State::Seen;
            true
        }
    }
}
fn toposort_visit < T > ( index : usize , items : & mut [ MaybeUninit < T > ] , state : & mut [ State ] , result : & mut Vec < T > , is_edge_from : & dyn Fn ( & T , & T ) -> bool , ) -> bool { match state [ index ] { State :: Seen => true , State :: Visiting => false , // Cycle. State :: Unseen => { // Indicate that the item is being visited to detect cycles and // ensure no recursive call to `toposort_visit` can move // `items[index]` out of `items`. state [ index ] = State :: Visiting ; // SAFETY: items are only consumed (and therefore uninitialized) // when their corresponding state is set to `Seen`. Here, the state // is `Unseen`. let value = unsafe { & * items [ index ] . as_ptr ( ) } ; // Iterate over all unseen items. If `items[index]` depends on // `items[i]`, we recursively call `toposort_visit` to add that // dependency to `result` before `items[index]` itself. let mut i = 0 ; while let Some ( i_state ) = state . get ( i ) { if i != index && * i_state != State :: Seen { // SAFETY: the `Unseen` and `Visiting` states indicate that // the item has not been moved out of the array yet. let i_value = unsafe { & * items [ i ] . as_ptr ( ) } ; if is_edge_from ( value , i_value ) { if ! toposort_visit ( i , items , state , result , is_edge_from ) { // Cycle encountered. return false ; } } } i += 1 ; } // SAFETY: item at `index` was `Unseen` and is guaranteed to still // have a value in `items[index]`. result . push ( unsafe { std :: mem :: replace ( & mut items [ index ] , MaybeUninit :: uninit ( ) ) . assume_init ( ) } ) ; state [ index ] = State :: Seen ; true } } }
Recursively visits the node at the given index; returns `true` if the
visit completes successfully, or `false` if a cycle was detected.
Recursively visits the node at the given index ; returns ` true ` if the visit completes successfully or ` false ` if a cycle was detected .
index
usize
items
&mut [MaybeUninit<T>]
state
&mut [State]
result
&mut Vec<T>
is_edge_from
&dyn Fn(&T, &T) -> bool
index
usize
items
&mut [MaybeUninit<T>]
state
&mut [State]
result
&mut Vec<T>
is_edge_from
&dyn Fn(&T, &T) -> bool