use crate::Stack;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
impl<V: Display + Copy, const N: usize> Display for Stack<V, N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
<&Self as Debug>::fmt(&self, f)
}
}
impl<V: Display + Copy, const N: usize> Debug for Stack<V, N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut parts = vec![];
for v in self.iter() {
parts.push(format!("{v}"));
}
f.write_str(format!("[{}]", parts.join(", ").as_str()).as_str())
}
}
#[test]
fn debugs_stack() {
let mut s: Stack<&str, 10> = Stack::new();
unsafe { s.push_unchecked("one") };
unsafe { s.push_unchecked("two") };
assert_eq!("[one, two]", format!("{:?}", s));
}
#[test]
fn displays_stack() {
let mut s: Stack<&str, 10> = Stack::new();
unsafe { s.push_unchecked("one") };
unsafe { s.push_unchecked("two") };
assert_eq!("[one, two]", format!("{}", s));
}