use core::fmt::{self, Display};
#[cfg(feature = "cmp")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "cmp")))]
pub mod cmp;
mod bytes_pattern;
mod slice_concatenation;
mod slice_const_methods;
mod slice_filler;
mod slice_split_off;
#[cfg(feature = "cmp")]
mod slice_is_sorted_methods;
#[cfg(feature = "iter")]
mod slice_iter_methods;
pub use bytes_pattern::BytesPattern;
pub(crate) use bytes_pattern::PatternNorm;
pub use self::slice_concatenation::*;
pub use self::slice_const_methods::*;
pub use self::slice_filler::*;
pub use self::slice_split_off::*;
#[cfg(feature = "cmp")]
pub use self::slice_is_sorted_methods::*;
#[cfg(feature = "iter")]
pub use slice_iter_methods::*;
__declare_slice_cmp_fns! {
import_path = "konst",
(
,
,
,
u8,
eq_bytes,
cmp_bytes,
)
}
__declare_fns_with_docs! {
(Option<&'a [u8]>, (eq_option_bytes, cmp_option_bytes))
docs(default)
macro = __impl_option_cmp_fns!(
for['a,]
params(l, r)
eq_comparison = eq_bytes(l, r),
cmp_comparison = cmp_bytes(l, r),
parameter_copyability = copy,
),
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct TryIntoArrayError {
slice_len: usize,
array_len: usize,
}
impl Display for TryIntoArrayError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"could not convert slice of length `{}` to array of length `{}`",
self.slice_len, self.array_len,
)
}
}
const _: () = {
use const_panic::{
PanicFmt, PanicVal, flatten_panicvals,
fmt::{self as cfmt, ComputePvCount, FmtArg, FmtKind},
};
impl PanicFmt for TryIntoArrayError {
type This = Self;
type Kind = const_panic::IsCustomType;
const PV_COUNT: usize = ComputePvCount {
field_amount: 2,
summed_pv_count: <usize>::PV_COUNT * 2,
delimiter: cfmt::TypeDelim::Braced,
}
.call();
}
impl TryIntoArrayError {
pub const fn to_panicvals(
&self,
fmtarg: FmtArg,
) -> [PanicVal<'static>; TryIntoArrayError::PV_COUNT] {
match fmtarg.fmt_kind {
FmtKind::Debug => {
flatten_panicvals! {fmtarg;
"TryIntoArrayError",
open: cfmt::OpenBrace,
"slice_len: ", usize => self.slice_len, cfmt::COMMA_SEP,
"array_len: ", usize => self.array_len, cfmt::COMMA_TERM,
close: cfmt::CloseBrace,
}
}
_ => const_panic::utils::flatten_panicvals(&[&[
PanicVal::write_str("could not convert slice of length `"),
PanicVal::from_usize(self.slice_len, FmtArg::DEBUG),
PanicVal::write_str("` to array of length `"),
PanicVal::from_usize(self.array_len, FmtArg::DEBUG),
PanicVal::write_str("`"),
]]),
}
}
}
};
#[inline]
pub const fn try_into_array<T, const N: usize>(slice: &[T]) -> Result<&[T; N], TryIntoArrayError> {
if slice.len() == N {
let ptr = slice.as_ptr() as *const [T; N];
unsafe { Ok(&*ptr) }
} else {
Err(TryIntoArrayError {
slice_len: slice.len(),
array_len: N,
})
}
}
#[inline]
pub const fn try_into_array_mut<T, const N: usize>(
slice: &mut [T],
) -> Result<&mut [T; N], TryIntoArrayError> {
if slice.len() == N {
unsafe { Ok(&mut *(slice as *mut [T] as *mut [T; N])) }
} else {
Err(TryIntoArrayError {
slice_len: slice.len(),
array_len: N,
})
}
}
#[doc(hidden)]
pub struct __AssertSlice<'a, T> {
pub x: &'a [T],
}
#[doc(hidden)]
pub struct __AssertSliceMut<'a, T> {
pub x: &'a mut [T],
}