1#![cfg_attr(not(test), no_std)]
3#![allow(incomplete_features, internal_features)]
4#![feature(
5 const_destruct,
6 adt_const_params,
7 generic_const_exprs,
8 core_intrinsics,
9 iter_intersperse,
10 const_trait_impl,
11 maybe_uninit_array_assume_init,
12 array_windows,
13 iter_map_windows
14)]
15#![warn(
16 clippy::undocumented_unsafe_blocks,
17 clippy::missing_const_for_fn,
18 clippy::missing_safety_doc,
19 clippy::suboptimal_flops,
20 unsafe_op_in_unsafe_fn,
21 clippy::dbg_macro,
22 clippy::use_self,
23 missing_docs
24)]
25use core::{
26 array::from_fn,
27 intrinsics::transmute_unchecked,
28 marker::Destruct,
29 mem::{offset_of, MaybeUninit as MU},
30};
31pub mod pervasive;
32mod slice;
33mod tuple;
34#[doc(inline)]
35pub use slice::Slice;
36pub use tuple::*;
37
38pub mod prelude {
43 #[doc(inline)]
44 pub use super::{
45 pervasive::prelude::*, range, slice::r, slice::Slice, splat, Array, ArrayTools, Chunked,
46 CollectArray, Couple, Deconstruct, Deconstruct_, Flatten, Join, Split, Tuple, Zip,
47 };
48 #[doc(inline)]
49 pub use core::array::from_fn;
50}
51
52#[repr(C)]
53struct Pair<X, Y>(X, Y);
54impl<X, Y> Pair<X, Y> {
55 const fn tuple() -> bool {
56 (size_of::<(X, Y)>() == size_of::<Self>())
57 & (offset_of!(Self, 0) == offset_of!((X, Y), 0))
58 & (offset_of!(Self, 1) == offset_of!((X, Y), 1))
59 }
60
61 const fn into(self) -> (X, Y) {
62 if Self::tuple() {
63 unsafe { transmute_unchecked::<Self, (X, Y)>(self) }
65 } else {
66 let out = unsafe { (core::ptr::read(&self.0), core::ptr::read(&self.1)) };
68 core::mem::forget(self);
69 out
70 }
71 }
72
73 const unsafe fn splat<T>(x: T) -> (X, Y) {
74 assert!(core::mem::size_of::<T>() == core::mem::size_of::<Pair<X, Y>>());
75 unsafe { transmute_unchecked::<_, Self>(x) }.into()
77 }
78}
79
80pub fn splat<T: Clone, const N: usize>(a: T) -> [T; N] {
82 from_fn(|_| a.clone())
83}
84
85pub const fn range<const N: usize>() -> [usize; N] {
92 let mut out = unsafe { MU::<[MU<usize>; N]>::uninit().assume_init() };
93 let mut i = 0usize;
94 while i < out.len() {
95 out[i] = MU::new(i);
96 i += 1;
97 }
98 unsafe { transmute_unchecked(out) }
99}
100
101pub trait CollectArray<T> {
103 fn carr<const N: usize>(&mut self) -> [T; N];
109}
110
111impl<T, I: Iterator<Item = T>> CollectArray<T> for I {
112 fn carr<const N: usize>(&mut self) -> [T; N] {
113 from_fn(|_| self.next().unwrap())
114 }
115}
116
117#[const_trait]
129pub trait Deconstruct<T, const N: usize> {
130 fn uncons(self) -> (T, [T; N - 1]);
139 fn unsnoc(self) -> ([T; N - 1], T);
148}
149
150pub trait Deconstruct_<T, const N: usize> {
154 fn last(self) -> T;
157 fn init(self) -> [T; N - 1];
166 fn head(self) -> T;
169 fn tail(self) -> [T; N - 1];
178}
179
180impl<T, const N: usize> const Deconstruct<T, N> for [T; N] {
181 #[doc(alias = "pop_front")]
182 fn uncons(self) -> (T, [T; N - 1]) {
183 unsafe { Pair::splat(self) }
185 }
186
187 #[doc(alias = "pop")]
188 fn unsnoc(self) -> ([T; N - 1], T) {
189 unsafe { Pair::splat(self) }
191 }
192}
193
194impl<T, const N: usize> Deconstruct_<T, N> for [T; N]
195where
196 [(); N - 1]:,
197{
198 fn last(self) -> T {
199 self.unsnoc().1
200 }
201 #[doc(alias = "trunc")]
202 fn init(self) -> [T; N - 1] {
203 self.unsnoc().0
204 }
205 fn head(self) -> T {
206 self.uncons().0
207 }
208 fn tail(self) -> [T; N - 1] {
209 self.uncons().1
210 }
211}
212
213#[const_trait]
215pub trait Join<T, const N: usize, const O: usize, U> {
216 fn join(self, with: U) -> [T; N + O];
225}
226
227#[const_trait]
229pub trait Couple<T, const N: usize, const O: usize> {
230 fn couple(self, with: [T; O]) -> [T; N + O];
237}
238
239impl<T, const N: usize, const O: usize> const Couple<T, N, O> for [T; N] {
240 fn couple(self, with: [T; O]) -> [T; N + O] {
241 unsafe { transmute_unchecked(Pair(self, with)) }
243 }
244}
245
246impl<T, const N: usize> const Join<T, N, 1, T> for [T; N] {
247 fn join(self, with: T) -> [T; N + 1] {
248 self.couple([with])
249 }
250}
251
252impl<T> const Join<T, 1, 1, T> for T {
253 fn join(self, with: T) -> [T; 2] {
254 [self, with]
255 }
256}
257
258impl<T, const O: usize> const Join<T, 1, O, [T; O]> for T {
259 fn join(self, with: [T; O]) -> [T; 1 + O] {
260 [self].couple(with)
261 }
262}
263
264pub(crate) const fn assert_zero(x: usize) -> usize {
265 if x != 0 {
266 panic!("expected zero");
267 } else {
268 0
269 }
270}
271
272#[allow(private_bounds)]
274#[const_trait]
275pub trait Chunked<T, const N: usize> {
276 #[allow(private_bounds)]
284 fn chunked<const C: usize>(self) -> [[T; C]; N / C]
285 where
286 [(); assert_zero(N % C)]:;
288}
289
290impl<const N: usize, T> const Chunked<T, N> for [T; N] {
291 #[allow(private_bounds)]
292 fn chunked<const C: usize>(self) -> [[T; C]; N / C]
293 where
294 [(); assert_zero(N % C)]:,
295 {
296 let retval = unsafe { self.as_ptr().cast::<[[T; C]; N / C]>().read() };
298 core::mem::forget(self);
299 retval
300 }
301}
302
303#[const_trait]
305pub trait Flatten<T, const N: usize, const N2: usize> {
306 fn flatten(self) -> [T; N * N2];
327}
328
329impl<T, const N: usize, const N2: usize> const Flatten<T, N, N2> for [[T; N]; N2] {
330 fn flatten(self) -> [T; N * N2] {
331 unsafe { core::intrinsics::transmute_unchecked(self) }
333 }
334}
335
336#[const_trait]
337pub trait Split<T, const N: usize> {
339 fn split<const AT: usize>(self) -> ([T; AT], [T; N - AT]);
347 fn take<const AT: usize>(self) -> [T; AT]
354 where
355 [(); N - AT]:,
356 T: [const] Destruct;
357 fn drop<const AT: usize>(self) -> [T; N - AT]
359 where
360 T: [const] Destruct;
361}
362
363impl<T, const N: usize> const Split<T, N> for [T; N] {
364 fn split<const AT: usize>(self) -> ([T; AT], [T; N - AT]) {
365 unsafe { Pair::splat(self) }
367 }
368 fn take<const M: usize>(self) -> [T; M]
369 where
370 [(); N - M]:,
372 T: [const] Destruct,
373 {
374 self.split::<M>().0
375 }
376 fn drop<const M: usize>(self) -> [T; N - M]
377 where
378 T: [const] Destruct,
379 {
380 self.split::<M>().1
381 }
382}
383#[const_trait]
384pub trait Zip<T, const N: usize> {
386 fn zip<U>(self, with: [U; N]) -> [(T, U); N];
388}
389
390impl<T, const N: usize> const Zip<T, N> for [T; N] {
391 fn zip<U>(self, with: [U; N]) -> [(T, U); N] {
392 let mut out = unsafe { MU::<[MU<_>; N]>::uninit().assume_init() };
393 let mut i = 0usize;
394 while i < out.len() {
395 out[i] = MU::new(unsafe { (self.as_ptr().add(i).read(), with.as_ptr().add(i).read()) });
396 i += 1;
397 }
398 core::mem::forget((self, with));
399 unsafe { transmute_unchecked(out) }
400 }
401}
402
403pub trait ArrayTools<T, const N: usize> {
405 fn skip<const BY: usize>(self) -> [T; N - BY];
407 fn step<const STEP: usize>(self) -> [T; 1 + (N - 1) / (STEP)];
419 fn intersperse(self, with: T) -> [T; (N * 2) - 1]
427 where
428 T: Clone;
429 fn each(self, apply: impl FnMut(T));
431 fn enumerate(self) -> [(T, usize); N];
433 fn windowed<const W: usize>(&self) -> [&[T; W]; N - W + 1];
440 fn inspect(self, f: impl FnMut(&T)) -> Self;
442 fn rev(self) -> Self;
444 fn interleave(self, with: [T; N]) -> [T; N * 2];
451 fn cartesian_product<U: Clone, const M: usize>(&self, with: &[U; M]) -> [(T, U); N + M]
458 where
459 T: Clone;
460 fn sort(self) -> Self
462 where
463 T: Ord;
464 fn sum(self) -> T
466 where
467 T: core::iter::Sum<T>;
468 fn product(self) -> T
470 where
471 T: core::iter::Product<T>;
472}
473
474impl<T, const N: usize> ArrayTools<T, N> for [T; N] {
475 fn skip<const BY: usize>(self) -> [T; N - BY] {
476 self.into_iter().skip(BY).carr()
477 }
478 fn step<const STEP: usize>(self) -> [T; 1 + (N - 1) / (STEP)] {
479 self.into_iter().step_by(STEP).carr()
480 }
481 fn intersperse(self, with: T) -> [T; (N * 2) - 1]
482 where
483 T: Clone,
484 {
485 self.into_iter().intersperse(with).carr()
486 }
487
488 fn each(self, apply: impl FnMut(T)) {
489 self.into_iter().for_each(apply);
490 }
491
492 fn enumerate(self) -> [(T, usize); N] {
493 let mut n = 0;
494 self.map(|x| {
495 let o = n;
496 n += 1;
497 (x, o)
498 })
499 }
500
501 fn windowed<const W: usize>(&self) -> [&[T; W]; N - W + 1] {
502 self.array_windows().carr()
503 }
504
505 fn inspect(self, f: impl FnMut(&T)) -> Self {
506 self.iter().for_each(f);
507 self
508 }
509
510 fn rev(mut self) -> Self {
511 self.reverse();
512 self
513 }
514
515 fn interleave(self, with: [T; N]) -> [T; N * 2] {
516 let mut which = true;
517 let mut a = self.into_iter();
518 let mut b = with.into_iter();
519 from_fn(|_| {
520 which = !which;
521 match which {
522 false => a.next().unwrap(),
523 true => b.next().unwrap(),
524 }
525 })
526 }
527
528 fn cartesian_product<U: Clone, const M: usize>(&self, with: &[U; M]) -> [(T, U); N + M]
529 where
530 T: Clone,
531 {
532 self.iter()
533 .flat_map(|a| with.iter().map(move |b| (a.clone(), b.clone())))
534 .carr()
535 }
536
537 fn sort(mut self) -> Self
538 where
539 T: Ord,
540 {
541 <[T]>::sort_unstable(&mut self);
542 self
543 }
544
545 fn sum(self) -> T
546 where
547 T: core::iter::Sum<T>,
548 {
549 self.into_iter().sum()
550 }
551
552 fn product(self) -> T
553 where
554 T: core::iter::Product<T>,
555 {
556 self.into_iter().product()
557 }
558}