microstack/ctors.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
21use crate::Stack;
22use std::mem::MaybeUninit;
23
24impl<V: Copy, const N: usize> Default for Stack<V, N> {
25 /// Make a default empty [`Stack`].
26 #[inline]
27 #[must_use]
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl<V: Copy, const N: usize> Stack<V, N> {
34 /// Make it.
35 ///
36 /// The size of the stack is defined by the generic argument.
37 #[inline]
38 #[must_use]
39 #[allow(clippy::uninit_assumed_init)]
40 pub const fn new() -> Self {
41 unsafe {
42 Self {
43 next: 0,
44 items: MaybeUninit::<[V; N]>::uninit().assume_init(),
45 }
46 }
47 }
48}
49
50#[test]
51fn makes_default_stack() {
52 let s: Stack<u8, 8> = Stack::default();
53 assert_eq!(0, s.len());
54}
55
56#[test]
57fn makes_new_stack() {
58 let s: Stack<u8, 8> = Stack::new();
59 assert_eq!(0, s.len());
60}