#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "arbitrary1")]
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
mod arbitrary1;
#[cfg(feature = "proptest1")]
#[cfg_attr(docsrs, doc(cfg(feature = "proptest")))]
mod proptest1;
#[cfg(feature = "quickcheck1")]
#[cfg_attr(docsrs, doc(cfg(feature = "quickcheck")))]
mod quickcheck1;
#[cfg(feature = "schemars08")]
#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
mod schemars08;
#[cfg(feature = "schemars09")]
#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
mod schemars09;
#[cfg(feature = "schemars1")]
#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
mod schemars1;
#[cfg(feature = "serde1")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
mod serde1;
mod array;
mod iter;
mod mirror_std {
mod cmp;
mod from;
mod partial_eq;
mod try_from;
}
mod slice;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
mod vec;
use core::{convert::Infallible, fmt, num::NonZeroUsize};
#[derive(Debug, Clone, Copy, Hash)]
#[repr(transparent)]
pub struct NonEmpty<T: ?Sized> {
inner: T,
}
pub type Array<T, const N: usize> = NonEmpty<[T; N]>;
pub type Slice<T> = NonEmpty<[T]>;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub type Vec<T> = NonEmpty<alloc::vec::Vec<T>>;
#[macro_export]
macro_rules! slice {
($($el:expr),+ $(,)?) => {
unsafe {
$crate::Slice::new_unchecked(&[$($el),*])
}
};
}
#[macro_export]
macro_rules! array {
($($el:expr),+ $(,)?) => {
unsafe {
$crate::Array::new_unchecked([$($el),*])
}
};
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[macro_export]
macro_rules! vec {
($($el:expr),+ $(,)?) => {
$crate::Vec::from(unsafe {
$crate::Array::new_unchecked([$($el),*])
})
};
($el:expr; $n:expr) => {
$crate::Vec::filled($el, $crate::nonzero!($n))
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! nonzero {
($expr:expr) => {{
const NONZERO: $crate::__private::core::num::NonZeroUsize =
match $crate::__private::core::num::NonZeroUsize::new($expr) {
$crate::__private::core::option::Option::Some(it) => it,
_ => $crate::__private::core::panic!("expression evaluated to zero"),
};
NONZERO
}};
}
#[doc(hidden)]
pub mod __private {
pub extern crate core;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Error(());
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("collection was empty")
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
macro_rules! transmuting {
() => {}; (const $(#[$meta:meta])* $ident:ident($in:ty) -> $out:ty; $($rest:tt)*) => {
$(#[$meta])*
pub const unsafe fn $ident(src: $in) -> $out {
debug_assert!(!src.is_empty());
unsafe { core::mem::transmute(src) }
}
$crate::transmuting!($($rest)*);
};
($(#[$meta:meta])* $ident:ident($in:ty) -> $out:ty; $($rest:tt)*) => {
$(#[$meta])*
pub unsafe fn $ident(src: $in) -> $out {
debug_assert!(!src.is_empty());
unsafe { core::mem::transmute(src) }
}
$crate::transmuting!($($rest)*);
};
}
pub(crate) use transmuting;
macro_rules! map_non_empty {
() => {}; (const $(#[$meta:meta])* $ident:ident($in:ty) -> $out:ty: $mapper:path; $($rest:tt)*) => {
$(#[$meta])*
pub const fn $ident(src: $in) -> Option<$out> {
match src.is_empty() {
true => None,
false => Some(unsafe { $mapper(src) })
}
}
$crate::map_non_empty!($($rest)*);
};
($(#[$meta:meta])* $ident:ident($in:ty) -> $out:ty: $mapper:path; $($rest:tt)*) => {
$(#[$meta])*
pub fn $ident(src: $in) -> Option<$out> {
match src.is_empty() {
true => None,
false => Some(unsafe { $mapper(src) })
}
}
$crate::map_non_empty!($($rest)*);
};
}
pub(crate) use map_non_empty;
macro_rules! as_ref_as_mut {
($($(<$ty_param:ident $(, const $const_param:ident: usize)?>)? for $self:ty as $ty:ty);* $(;)?) => {
$(
impl$(<$ty_param $(, const $const_param: usize)?>)? ::core::convert::AsRef<$ty> for $self {
fn as_ref(&self) -> &$ty { self }
}
impl$(<$ty_param $(, const $const_param: usize)?>)? ::core::convert::AsMut<$ty> for $self {
fn as_mut(&mut self) -> &mut $ty { self }
}
)*
};
}
pub(crate) use as_ref_as_mut;
macro_rules! borrow_borrow_mut {
($($(<$ty_param:ident $(, const $const_param:ident: usize)?>)? for $self:ty as $ty:ty);* $(;)?) => {
$(
impl$(<$ty_param $(, const $const_param: usize)?>)? ::core::borrow::Borrow<$ty> for $self {
fn borrow(&self) -> &$ty { self }
}
impl$(<$ty_param $(, const $const_param: usize)?>)? ::core::borrow::BorrowMut<$ty> for $self {
fn borrow_mut(&mut self) -> &mut $ty { self }
}
)*
};
}
pub(crate) use borrow_borrow_mut;
macro_rules! slice_iter {
(<$ty_param:ident $(, const $const_param:ident: usize)?> for $self:ty) => {
impl<'a, $ty_param $(, const $const_param: usize)?> ::core::iter::IntoIterator for &'a $self {
type Item = &'a $ty_param;
type IntoIter = ::core::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, $ty_param $(, const $const_param: usize)?> ::core::iter::IntoIterator for &'a mut $self {
type Item = &'a mut $ty_param;
type IntoIter = ::core::slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
};
}
pub(crate) use slice_iter;
#[track_caller]
const unsafe fn non_zero_usize(n: usize) -> NonZeroUsize {
match NonZeroUsize::new(n) {
Some(it) => it,
None => unreachable(),
}
}
#[track_caller]
const unsafe fn unreachable() -> ! {
match cfg!(debug_assertions) {
true => unreachable(),
false => unsafe { core::hint::unreachable_unchecked() },
}
}
#[derive(Debug, Clone, Copy)]
pub struct TryFromSliceError(());
impl From<Infallible> for TryFromSliceError {
fn from(value: Infallible) -> Self {
match value {}
}
}
impl fmt::Display for TryFromSliceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("could not convert slice to array")
}
}