Skip to content

Commit

Permalink
feat(zero-sized): add zero-sized Display types, change `Printable{I…
Browse files Browse the repository at this point in the history
…ter, Tuple}` signatures
  • Loading branch information
andrewsonin committed May 15, 2024
1 parent 4b2bc10 commit ba8031e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- next-header -->

## [2.0.0] - 2024-05-15
### Added
- `auxiliary::SpacedComma`
- `auxiliary::LeftSquareBracket`
- `auxiliary::RightSquareBracket`
### Changed
- `PrintableIter` and `PrintableTuple` signatures

## [1.0.0] - 2024-05-15
### Added
- `PrintableIter`
- `PrintableTuple`

<!-- next-url -->
[2.0.0]: https://github.com/andrewsonin/lazy_ref/releases/tag/v2.0.0
[1.0.0]: https://github.com/andrewsonin/lazy_ref/releases/tag/v1.0.0
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "printable"
version = "1.0.1"
version = "2.0.0"
authors = ["Andrew Sonin <sonin.cel@yandex.ru>"]
categories = ["value-formatting"]
description = "Provides `std::fmt::Display` wrapper for iterators and tuples."
Expand Down
37 changes: 37 additions & 0 deletions src/auxiliary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::fmt::{Debug, Display, Formatter, Write};

/// Zero-sized type, which is displayed as `, `.
#[derive(Debug)]
#[repr(transparent)]
pub struct SpacedComma;

impl Display for SpacedComma {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(", ")
}
}

/// Zero-sized type, which is displayed as `[`.
#[derive(Debug)]
#[repr(transparent)]
pub struct LeftSquareBracket;

impl Display for LeftSquareBracket {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_char('[')
}
}

/// Zero-sized type, which is displayed as `]`.
#[derive(Debug)]
#[repr(transparent)]
pub struct RightSquareBracket;

impl Display for RightSquareBracket {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_char(']')
}
}
17 changes: 12 additions & 5 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::auxiliary::{LeftSquareBracket, RightSquareBracket, SpacedComma};
use std::fmt::{Display, Formatter};

use crate::wrapper::{DisplayWithSep, PrintableWrapper};
Expand Down Expand Up @@ -54,14 +55,15 @@ where
#[inline]
fn printable(
self,
) -> PrintableWrapper<&'static str, &'static str, &'static str, IterHandler<Self>> {
) -> PrintableWrapper<SpacedComma, LeftSquareBracket, RightSquareBracket, IterHandler<Self>>
{
#[cfg(feature = "unstable-assert-no-drop")]
let () = Self::NO_DROP_CHECK;
PrintableWrapper {
inner: IterHandler(self),
sep: ", ",
left_bound: "[",
right_bound: "]",
sep: SpacedComma,
left_bound: LeftSquareBracket,
right_bound: RightSquareBracket,
}
}
}
Expand All @@ -74,7 +76,12 @@ where
}

impl<T> From<T>
for PrintableWrapper<&'static str, &'static str, &'static str, IterHandler<T::IntoIter>>
for PrintableWrapper<
SpacedComma,
LeftSquareBracket,
RightSquareBracket,
IterHandler<T::IntoIter>,
>
where
T: IntoIterator,
T::Item: Display,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![doc = include_str!("../README.md")]
#![allow(clippy::module_name_repetitions)]

/// Implements various auxiliary utilities.
pub mod auxiliary;
/// Implements [`Display`](std::fmt::Display) wrapper for iterators.
pub mod iter;
/// Implements [`Display`](std::fmt::Display) wrapper for tuples.
Expand Down
15 changes: 10 additions & 5 deletions src/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::fmt::{Display, Formatter};

use crate::{prelude::PrintableWrapper, wrapper::DisplayWithSep};
use crate::{
auxiliary::{LeftSquareBracket, RightSquareBracket, SpacedComma},
prelude::PrintableWrapper,
wrapper::DisplayWithSep,
};

/// Wraps tuples into [`PrintableWrapper`].
///
Expand All @@ -20,12 +24,13 @@ pub trait PrintableTuple: Sized {
#[inline]
fn printable(
self,
) -> PrintableWrapper<&'static str, &'static str, &'static str, TupleHandler<Self>> {
) -> PrintableWrapper<SpacedComma, LeftSquareBracket, RightSquareBracket, TupleHandler<Self>>
{
PrintableWrapper {
inner: TupleHandler(self),
sep: ", ",
left_bound: "[",
right_bound: "]",
sep: SpacedComma,
left_bound: LeftSquareBracket,
right_bound: RightSquareBracket,
}
}
}
Expand Down

0 comments on commit ba8031e

Please sign in to comment.