Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Debug impl for UnvalidatedTinyAsciiStr #4189

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion utils/tinystr/src/unvalidated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::TinyAsciiStr;
use crate::TinyStrError;
use core::fmt;

/// A fixed-length bytes array that is expected to be an ASCII string but does not enforce that invariant.
///
Expand All @@ -12,9 +13,19 @@ use crate::TinyStrError;
///
/// The main advantage of this type over `[u8; N]` is that it serializes as a string in
/// human-readable formats like JSON.
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
#[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
pub struct UnvalidatedTinyAsciiStr<const N: usize>(pub(crate) [u8; N]);

impl<const N: usize> fmt::Debug for UnvalidatedTinyAsciiStr<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Debug as a string if possible
match self.try_into_tinystr() {
Ok(s) => fmt::Debug::fmt(&s, f),
Err(_) => fmt::Debug::fmt(&self.0, f),
}
}
}

impl<const N: usize> UnvalidatedTinyAsciiStr<N> {
#[inline]
// Converts into a [`TinyAsciiStr`]. Fails if the bytes are not valid ASCII.
Expand Down
Loading