microstack/lib.rs
1// Copyright (c) 2023 Yegor Bugayenko
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included
11// in all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! This is a simplest and the fastest implementation of a stack on stack,
22//! when stack elements are `Copy` implementing primitives.
23//!
24//! For example, here is how a stack can be created:
25//!
26//! ```
27//! use microstack::Stack;
28//! let mut s : Stack<u64, 10> = Stack::new();
29//! s.push(1);
30//! s.push(2);
31//! assert_eq!(2, s.pop());
32//! assert_eq!(1, s.len());
33//! ```
34//!
35//! Creating a [`Stack`] requires knowing the maximum size of it, upfront. This is
36//! what the second type argument `10` is for, in the example above. The stack
37//! will have exactly ten elements. An attempt to add an 11th element will lead
38//! to a panic.
39
40#![doc(html_root_url = "https://docs.rs/microstack/0.0.7")]
41#![deny(warnings)]
42#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
43#![allow(clippy::multiple_inherent_impl)]
44#![allow(clippy::multiple_crate_versions)]
45
46use std::marker::PhantomData;
47
48mod clone;
49mod ctors;
50mod debug;
51mod iterators;
52#[cfg(feature = "serde")]
53mod serialization;
54mod stack;
55
56/// This is a simplest and the fastest implementation of a stack on stack,
57/// when stack elements are `Copy` implementing primitives.
58///
59/// For example, here is how a stack can be created:
60///
61/// ```
62/// use microstack::Stack;
63/// let mut s : Stack<u64, 10> = Stack::new();
64/// s.push(1);
65/// s.push(2);
66/// assert_eq!(2, s.pop());
67/// ```
68///
69pub struct Stack<V: Copy, const N: usize> {
70 /// The next available position in the array.
71 next: usize,
72 /// The fixed-size array of values.
73 items: [V; N],
74}
75
76/// Iterator.
77pub struct Iter<'a, V: Copy, const N: usize> {
78 /// The position.
79 pos: usize,
80 /// The next available position in the array.
81 next: usize,
82 /// The fixed-size array of values.
83 items: *const V,
84 _marker: PhantomData<&'a V>,
85}
86
87/// Into-iterator.
88pub struct IntoIter<V: Copy, const N: usize> {
89 /// The position.
90 pos: usize,
91 /// The next available position in the array.
92 next: usize,
93 /// The fixed-size array of values.
94 items: *const V,
95}