hicc_std/
std_stack.rs

1hicc::cpp! {
2    #include <stack>
3}
4
5hicc::import_class! {
6    #[cpp(class = "template<class T, class Container> std::stack<T, Container>")]
7    pub class stack<T> {
8        /// ```
9        /// use hicc_std::StackInt;
10        /// let mut stack = StackInt::new();
11        /// assert!(stack.is_empty());
12        /// stack.push(&1);
13        /// assert!(!stack.is_empty());
14        /// ```
15        #[cpp(method = "bool empty() const")]
16        pub fn is_empty(&self) -> bool;
17
18        /// ```
19        /// use hicc_std::StackInt;
20        /// let mut stack = StackInt::new();
21        /// assert_eq!(stack.size(), 0);
22        /// stack.push(&1);
23        /// assert_eq!(stack.size(), 1);
24        /// ```
25        #[cpp(method = "size_t size() const")]
26        pub fn size(&self) -> usize;
27
28        /// 如果为空不做任何改变.
29        /// ```
30        /// use hicc_std::StackInt;
31        /// let mut stack = StackInt::new();
32        /// stack.pop();
33        /// stack.push(&1);
34        /// assert_eq!(stack.size(), 1);
35        /// stack.pop();
36        /// assert_eq!(stack.size(), 0);
37        /// ```
38        pub fn pop(&mut self) {
39            if !self.is_empty() {
40                unsafe { self._pop() };
41            }
42        }
43        #[cpp(method = "void pop()")]
44        unsafe fn _pop(&mut self);
45
46        /// ```
47        /// use hicc_std::StackInt;
48        /// let mut stack = StackInt::new();
49        /// stack.push(&1);
50        /// assert_eq!(stack.top(), Some(&1));
51        /// ```
52        #[cpp(method = "void push(const T&)")]
53        pub fn push(&mut self, val: &T);
54
55        /// ```
56        /// use hicc_std::StackInt;
57        /// let mut stack = StackInt::new();
58        /// assert_eq!(stack.top(), None);
59        /// stack.push(&1);
60        /// assert_eq!(stack.top(), Some(&1));
61        /// ```
62        pub fn top(&self) -> Option<T::OutputRef<'_>> {
63            if !self.is_empty() {
64                return unsafe { Some(self._top()) };
65            }
66            None
67        }
68        #[cpp(method = "const T& top() const")]
69        unsafe fn _top(&self) -> &T;
70
71        /// ```
72        /// use hicc_std::StackInt;
73        /// let mut stack = StackInt::new();
74        /// assert_eq!(stack.top_mut(), None);
75        /// stack.push(&1);
76        /// *stack.top_mut().unwrap() += 1;
77        /// assert_eq!(stack.top_mut(), Some(&mut 2));
78        ///
79        /// use hicc_std::{string, StackString};
80        /// use hicc::AbiClass;
81        /// let mut stack = StackString::new();
82        /// stack.push(&string::from(c"hello"));
83        /// assert_eq!(stack.top(), Some(string::from(c"hello").into_ref()));
84        /// stack.top_mut().unwrap().write(string::from(c"world"));
85        /// assert_eq!(stack.top(), Some(string::from(c"world").into_ref()));
86        /// ```
87        pub fn top_mut(&mut self) -> Option<T::OutputRefMut<'_>> {
88            if !self.is_empty() {
89                return unsafe { Some(self._top_mut()) };
90            }
91            None
92        }
93        #[cpp(method = "T& top()")]
94        unsafe fn _top_mut(&mut self) -> &mut T;
95
96        /// ```
97        /// use hicc_std::StackInt;
98        /// let mut stack = StackInt::new();
99        /// stack.push(&1);
100        /// let mut other = StackInt::new();
101        /// other.push(&2);
102        /// assert_eq!(stack.top(), Some(&1));
103        /// assert_eq!(other.top(), Some(&2));
104        /// stack.swap(&mut other);
105        /// assert_eq!(stack.top(), Some(&2));
106        /// assert_eq!(other.top(), Some(&1));
107        /// ```
108        #[cpp(method = "void swap(Self&)")]
109        pub fn swap(&mut self, other: &mut Self);
110    }
111
112    unsafe impl<T: hicc::AbiType + Sync> Send for stack<T> {}
113    unsafe impl<T: hicc::AbiType + Sync> Sync for stack<T> {}
114}