drumatech 0.1.3

a crate that has a little utility.
Documentation
use std::error::Error;
#[derive(Debug)]
pub struct Stack<T> {
    pub sp: usize,
    pub contents: Vec<T>,
}

impl<T> Stack<T> {
    /// the constructor is need tuple for initializing Stack
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate drumatech;
    /// use drumatech::structure;
    /// let mut stack = structure::Stack::new(vec![1,3,5,7,9]);
    /// ```
    pub fn new(contents: Vec<T>) -> Stack<T> {
        Stack {
            sp: contents.len() + 1,
            contents: contents,
        }
    }
    /// the constructor is need capacity for allocating memories for Stack\ndon't forget to
    /// annotate type
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate drumatech;
    /// use drumatech::structure;
    /// let mut stack : structure::Stack<u8> = structure::Stack::allocate(100); //
    /// ```
    pub fn allocate(cap: usize) -> Stack<T> {
        Stack {
            sp: 0,
            contents: Vec::with_capacity(cap),
        }
    }
    /// push the value on the top of stack.
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate drumatech;
    /// use drumatech::structure;
    /// let mut stack : structure::Stack<u8> = structure::Stack::allocate(100); //
    /// stack.push(3);
    /// assert_eq!(stack.contents[0],3);
    /// ```
    pub fn push(&mut self, element: T) {
        self.contents.push(element);
        self.sp += 1;
    }
    /// pop the value by the top of stack.
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate drumatech;
    /// use drumatech::structure;
    /// let mut stack : structure::Stack<u8> = structure::Stack::allocate(100); //
    /// stack.push(3);
    /// assert_eq!(stack.pop().unwrap(),3);
    /// ```
    pub fn pop(&mut self) -> Option<T> {
        if self.sp == 0 {
            println!("Error! the stack doesn't have enough to pop the value!");
            return None;
        }
        self.sp -= 1;
        self.contents.pop()
    }
    /// get the value by the top of stack without changing contents.
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate drumatech;
    /// use drumatech::structure;
    /// let mut stack : structure::Stack<u8> = structure::Stack::allocate(100); //
    /// stack.push(3);
    /// assert_eq!(*stack.top().unwrap(),3);
    /// assert_eq!(stack.pop().unwrap(),3);
    /// ```
    pub fn top(&self) -> Option<&T> {
        if self.sp == 0 {
            println!("Error! the stack is empty then coundn't get the contents!");
            return None;
        }
        Some(&self.contents[self.contents.len() - 1])
    }
}
impl<T: Clone> Stack<T> {
    /// get the value by the base of stack without changing contents.
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate drumatech;
    /// use drumatech::structure;
    /// let mut stack : structure::Stack<u8> = structure::Stack::allocate(100); //
    /// stack.push(3);
    /// stack.push(2);
    /// assert_eq!(stack.base(),3);
    /// assert_eq!(stack.pop().unwrap(),2);
    /// ```
    pub fn base(&self) -> T {
        if self.sp == 0 {
            eprintln!("Invalid access out of range");
        }
        self.contents[0].clone()
    }
}

pub struct ArrayStack<T> {
    size: usize,
    contents: Vec<T>,
}

impl<T: Clone> ArrayStack<T> {
    pub fn new(src: Vec<T>) -> Self {
        Self {
            size: src.len(),
            contents: src,
        }
    }
    /// get the value with specifying position.
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate drumatech;
    /// use drumatech::structure;
    /// let mut arys : structure::ArrayStack<u8> = structure::ArrayStack::new(vec![1,2]);
    /// assert_eq!(arys.get(1),2);
    /// assert_eq!(arys.get(0),1);
    /// ```
    pub fn get(&self, index: usize) -> T {
        if 0 > index || self.size < index {
            eprintln!("Invalid access out of range");
        }
        self.contents[index].clone()
    }
    /// set the value with specifying position.
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate drumatech;
    /// use drumatech::structure;
    /// let mut arys : structure::ArrayStack<u8> = structure::ArrayStack::new(vec![1,2]);
    /// assert_eq!(arys.set(1,10),2);
    /// assert_eq!(arys.get(1),10);
    /// ```
    pub fn set(&mut self, index: usize, x: T) -> T {
        if 0 > index || self.size < index {
            eprintln!("invalid access out of range");
        }
        let ret: T = self.get(index);
        self.contents[index] = x;
        ret
    }
}