Skip to content

Commit

Permalink
Use usual debug generation features, drop std dependency
Browse files Browse the repository at this point in the history
Display has been moved into an own file.
  • Loading branch information
alexkazik committed Dec 31, 2023
1 parent 82e0a2f commit e8a9717
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
29 changes: 12 additions & 17 deletions src/debug.rs
Expand Up @@ -19,22 +19,11 @@
// SOFTWARE.

use crate::Map;
use core::fmt;
use core::fmt::{Debug, Display, Formatter};
use core::fmt::{self, Debug, Formatter};

impl<K: PartialEq + Display, V: Display, const N: usize> Display for Map<K, V, N> {
impl<K: PartialEq + Debug, V: Debug, const N: usize> Debug for Map<K, V, N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
<&Self as Debug>::fmt(&self, f)
}
}

impl<K: PartialEq + Display, V: Display, const N: usize> Debug for Map<K, V, N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut parts = vec![];
for (k, v) in self.iter() {
parts.push(std::format!("{k}: {v}"));
}
f.write_str(std::format!("{{{}}}", parts.join(", ").as_str()).as_str())
f.debug_map().entries(self.iter()).finish()
}
}

Expand All @@ -48,14 +37,20 @@ mod test {
let mut m: Map<String, i32, 10> = Map::new();
m.insert("one".to_string(), 42);
m.insert("two".to_string(), 16);
assert_eq!("{one: 42, two: 16}", format!("{:?}", m));
assert_eq!(r#"{"one": 42, "two": 16}"#, format!("{:?}", m));
}

#[test]
fn displays_map() {
fn debug_alternate_map() {
let mut m: Map<String, i32, 10> = Map::new();
m.insert("one".to_string(), 42);
m.insert("two".to_string(), 16);
assert_eq!("{one: 42, two: 16}", format!("{}", m));
assert_eq!(
r#"{
"one": 42,
"two": 16,
}"#,
format!("{:#?}", m)
);
}
}
55 changes: 55 additions & 0 deletions src/display.rs
@@ -0,0 +1,55 @@
// Copyright (c) 2023 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::Map;
use core::fmt::{self, Display, Formatter, Write};

impl<K: PartialEq + Display, V: Display, const N: usize> Display for Map<K, V, N> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut first = true;
f.write_char('{')?;
for (k, v) in self {
if first {
first = false;
} else {
f.write_str(", ")?;
}
k.fmt(f)?;
f.write_str(": ")?;
v.fmt(f)?;
}
f.write_char('}')?;
Ok(())
}
}

#[cfg(test)]
mod test {

use super::*;

#[test]
fn displays_map() {
let mut m: Map<String, i32, 10> = Map::new();
m.insert("one".to_string(), 42);
m.insert("two".to_string(), 16);
assert_eq!(r#"{one: 42, two: 16}"#, format!("{}", m));
}
}
5 changes: 2 additions & 3 deletions src/lib.rs
Expand Up @@ -50,11 +50,10 @@
#![allow(clippy::multiple_inherent_impl)]
#![allow(clippy::multiple_crate_versions)]

#[cfg(feature = "std")]
mod debug;

mod clone;
mod ctors;
mod debug;
mod display;
mod eq;
mod from;
mod index;
Expand Down

0 comments on commit e8a9717

Please sign in to comment.