stalin-sort 1.0.2

An implementation of stalin sort in rust.
Documentation
// GPLv3-or-later
/*
 * lib.rs - the mentioned implementation of stalin sort
 * Copyright (C) 2025 skythedragon <skythedragon@sdf.org> under the GPLv3-or-later liscence
*/


#![deny(unsafe_op_in_unsafe_fn)]

pub trait StalinSortable {
    /**
    This function is marked unsafe, because well, would you feel safe if Stalin came to sort your
    list?
    */
    unsafe fn stalin_sort(&self) -> Self;
    /**
    This function is marked unsafe, because well, would you feel safe if Stalin came to sort your
    list? The GTG part stands for 'Got the gulag', meaning it also returns the discarded elements
    that were sent to the gulag
    */
    unsafe fn stalin_sort_gtg(&self) -> (Self, Self)
    where
        Self: Sized;
}

impl<T> StalinSortable for Vec<T> where T : Clone + PartialOrd {
    unsafe  fn stalin_sort(&self) -> Self {
        let mut result : Vec<T> = Vec::with_capacity(self.len());

        if self.len() == 0 {
            return result;
        }

        let mut max : T = self.first().unwrap().clone();

        self.into_iter().for_each(|x| {
            if *x > max {
                result.push(x.clone());
                max = x.clone();
            }
        });

        result
    }

    unsafe fn stalin_sort_gtg(&self) -> (Self, Self) {

        let mut result : Vec<T> = Vec::with_capacity(self.len());

        let mut gulag : Vec<T> = Vec::with_capacity(self.len());

        if self.len() == 0 {
            return (result, gulag);
        }

        let mut max : T = self.first().unwrap().clone();

        self.into_iter().for_each(|x| {
            if *x >= max {
                result.push(x.clone());
                max = x.clone();
            } else { gulag.push(x.clone()); }
        });

        (result, gulag)
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stalin_sort() {
        unsafe {
            assert_eq!(vec![1, 2, 3].stalin_sort(), vec![1, 2, 3]);
            assert_eq!(vec![1,2,1,4,5].stalin_sort(), vec![1,2,4,5]);
        }
    }
}