Skip to content
Open
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
35 changes: 34 additions & 1 deletion quickphf_codegen/src/const_instantiable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait ConstInstantiable {

/// Provides blanket implementation of [`ConstInstantiable`] which defers to
/// [`Debug`](core::fmt::Debug) representation of type.
pub trait DebugInstantiable: std::fmt::Debug {}
pub trait DebugInstantiable: fmt::Debug {}

impl<T: DebugInstantiable> ConstInstantiable for T {
fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -119,3 +119,36 @@ where
}
}
}

impl<const N: usize, T: ConstInstantiable> ConstInstantiable for [T; N] {
fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_const_slice_inner(self, f)
}
}

impl<const N: usize, T: ConstInstantiable> ConstInstantiable for &[T; N] {
fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(&self[..]).fmt_const_new(f)
}
}

impl<T: ConstInstantiable> ConstInstantiable for &[T] {
fn fmt_const_new(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "&")?;
fmt_const_slice_inner(self, f)
}
}

fn fmt_const_slice_inner<T: ConstInstantiable>(
slice: &[T],
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
write!(f, "[")?;
for (i, value) in slice.iter().enumerate() {
value.fmt_const_new(f)?;
if i < slice.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, "]")
}