stalin-sort 1.0.0

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
*/


#![deny(unsafe_op_in_unsafe_fn)]

pub trait StalinSortable {
    //returns stalin sorted vector
    unsafe fn stalin_sort(&self) -> Self;
    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]);
        }
    }
}