1use super::*;
4use alloc::collections::LinkedList;
5use core::hash::BuildHasher;
6use hashbrown::HashSet;
7
8pub unsafe trait ContainerExactly<T> {
14 const LEN: usize;
16
17 type Uninit;
19
20 fn uninit() -> Self::Uninit;
22
23 fn write(uninit: &mut Self::Uninit, i: usize, item: T);
25
26 unsafe fn drop_before(uninit: &mut Self::Uninit, i: usize);
32
33 unsafe fn take(uninit: Self::Uninit) -> Self;
39}
40
41unsafe impl<T, const N: usize> ContainerExactly<T> for [T; N] {
43 const LEN: usize = N;
44
45 type Uninit = [MaybeUninit<T>; N];
46 fn uninit() -> Self::Uninit {
47 MaybeUninitExt::uninit_array()
48 }
49 fn write(uninit: &mut Self::Uninit, i: usize, item: T) {
50 uninit[i].write(item);
51 }
52 unsafe fn drop_before(uninit: &mut Self::Uninit, i: usize) {
53 uninit[..i].iter_mut().for_each(|o| o.assume_init_drop());
54 }
55 unsafe fn take(uninit: Self::Uninit) -> Self {
56 MaybeUninitExt::array_assume_init(uninit)
57 }
58}
59
60unsafe impl<T, C> ContainerExactly<T> for Box<C>
62where
63 C: ContainerExactly<T>,
64{
65 const LEN: usize = C::LEN;
66 type Uninit = Box<C::Uninit>;
67 fn uninit() -> Self::Uninit {
68 Box::new(C::uninit())
69 }
70 fn write(uninit: &mut Self::Uninit, i: usize, item: T) {
71 C::write(&mut *uninit, i, item)
72 }
73 unsafe fn drop_before(uninit: &mut Self::Uninit, i: usize) {
74 C::drop_before(&mut *uninit, i)
75 }
76 unsafe fn take(uninit: Self::Uninit) -> Self {
77 Box::from_raw(Box::into_raw(uninit) as *mut C)
78 }
79}
80
81pub trait Seq<'p, T> {
140 type Item<'a>: Borrow<T>
142 where
143 Self: 'a;
144
145 type Iter<'a>: Iterator<Item = Self::Item<'a>>
147 where
148 Self: 'a;
149
150 fn seq_iter(&self) -> Self::Iter<'_>;
152
153 fn contains(&self, val: &T) -> bool
155 where
156 T: PartialEq;
157
158 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
160 where
161 'p: 'b;
162
163 #[doc(hidden)]
164 #[cfg(feature = "debug")]
165 fn seq_info(&self, _scope: &mut debug::NodeScope) -> debug::SeqInfo {
166 let ty = core::any::type_name::<Self>();
167 debug::SeqInfo::Unknown(ty.split_once('<').map_or(ty, |(ty, _)| ty).to_string())
168 }
169}
170
171impl<'p, T: Clone> Seq<'p, T> for T {
172 type Item<'a>
173 = &'a T
174 where
175 Self: 'a;
176
177 type Iter<'a>
178 = core::iter::Once<&'a T>
179 where
180 Self: 'a;
181
182 #[inline(always)]
183 fn seq_iter(&self) -> Self::Iter<'_> {
184 core::iter::once(self)
185 }
186
187 #[inline(always)]
188 fn contains(&self, val: &T) -> bool
189 where
190 T: PartialEq,
191 {
192 self == val
193 }
194
195 #[inline]
196 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
197 where
198 'p: 'b,
199 {
200 MaybeRef::Val(item.clone())
201 }
202
203 #[doc(hidden)]
204 #[cfg(feature = "debug")]
205 default fn seq_info(&self, _scope: &mut debug::NodeScope) -> debug::SeqInfo {
206 let ty = core::any::type_name::<Self>();
207 debug::SeqInfo::Unknown(ty.split_once('<').map_or(ty, |(ty, _)| ty).to_string())
208 }
209}
210
211#[doc(hidden)]
212#[cfg(feature = "debug")]
213impl<'p, T: Clone + core::fmt::Debug> Seq<'p, T> for T {
214 default fn seq_info(&self, _scope: &mut debug::NodeScope) -> debug::SeqInfo {
215 debug::SeqInfo::Opaque(format!("{self:?}"))
216 }
217}
218
219#[doc(hidden)]
220#[cfg(feature = "debug")]
221impl Seq<'_, char> for char {
222 fn seq_info(&self, _scope: &mut debug::NodeScope) -> debug::SeqInfo {
223 debug::SeqInfo::Char(*self)
224 }
225}
226
227impl<'p, T> Seq<'p, T> for &'p T {
228 type Item<'a>
229 = &'p T
230 where
231 Self: 'a;
232
233 type Iter<'a>
234 = core::iter::Once<&'p T>
235 where
236 Self: 'a;
237
238 #[inline(always)]
239 fn seq_iter(&self) -> Self::Iter<'_> {
240 core::iter::once(*self)
241 }
242
243 #[inline(always)]
244 fn contains(&self, val: &T) -> bool
245 where
246 T: PartialEq,
247 {
248 *self == val
249 }
250
251 #[inline]
252 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
253 where
254 'p: 'b,
255 {
256 MaybeRef::Ref(item)
257 }
258}
259
260impl<'p, T> Seq<'p, T> for &'p [T] {
261 type Item<'a>
262 = &'p T
263 where
264 Self: 'a;
265
266 type Iter<'a>
267 = core::slice::Iter<'p, T>
268 where
269 Self: 'a;
270
271 #[inline(always)]
272 fn seq_iter(&self) -> Self::Iter<'_> {
273 (self as &[T]).iter()
274 }
275
276 #[inline(always)]
277 fn contains(&self, val: &T) -> bool
278 where
279 T: PartialEq,
280 {
281 <[T]>::contains(self, val)
282 }
283
284 #[inline]
285 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
286 where
287 'p: 'b,
288 {
289 MaybeRef::Ref(item)
290 }
291}
292
293impl<'p, T: Clone, const N: usize> Seq<'p, T> for [T; N] {
294 type Item<'a>
295 = &'a T
296 where
297 Self: 'a;
298
299 type Iter<'a>
300 = core::slice::Iter<'a, T>
301 where
302 Self: 'a;
303
304 #[inline(always)]
305 fn seq_iter(&self) -> Self::Iter<'_> {
306 self.iter()
307 }
308
309 #[inline(always)]
310 fn contains(&self, val: &T) -> bool
311 where
312 T: PartialEq,
313 {
314 <[T]>::contains(self, val)
315 }
316
317 #[inline]
318 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
319 where
320 'p: 'b,
321 {
322 MaybeRef::Val(item.clone())
323 }
324}
325
326impl<'p, T, const N: usize> Seq<'p, T> for &'p [T; N] {
327 type Item<'a>
328 = &'p T
329 where
330 Self: 'a;
331
332 type Iter<'a>
333 = core::slice::Iter<'p, T>
334 where
335 Self: 'a;
336
337 #[inline(always)]
338 fn seq_iter(&self) -> Self::Iter<'_> {
339 self.iter()
340 }
341
342 #[inline(always)]
343 fn contains(&self, val: &T) -> bool
344 where
345 T: PartialEq,
346 {
347 #[allow(clippy::explicit_auto_deref)] <[T]>::contains(*self, val)
349 }
350
351 #[inline]
352 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
353 where
354 'p: 'b,
355 {
356 MaybeRef::Ref(item)
357 }
358}
359
360impl<'p, T: Clone> Seq<'p, T> for Vec<T> {
361 type Item<'a>
362 = &'a T
363 where
364 Self: 'a;
365
366 type Iter<'a>
367 = core::slice::Iter<'a, T>
368 where
369 Self: 'a;
370
371 #[inline(always)]
372 fn seq_iter(&self) -> Self::Iter<'_> {
373 self.iter()
374 }
375
376 #[inline(always)]
377 fn contains(&self, val: &T) -> bool
378 where
379 T: PartialEq,
380 {
381 <[T]>::contains(self, val)
382 }
383
384 #[inline]
385 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
386 where
387 'p: 'b,
388 {
389 MaybeRef::Val(item.clone())
390 }
391}
392
393impl<'p, T: Clone> Seq<'p, T> for LinkedList<T> {
394 type Item<'a>
395 = &'a T
396 where
397 Self: 'a;
398
399 type Iter<'a>
400 = alloc::collections::linked_list::Iter<'a, T>
401 where
402 Self: 'a;
403
404 #[inline(always)]
405 fn seq_iter(&self) -> Self::Iter<'_> {
406 self.iter()
407 }
408
409 #[inline(always)]
410 fn contains(&self, val: &T) -> bool
411 where
412 T: PartialEq,
413 {
414 LinkedList::contains(self, val)
415 }
416
417 #[inline]
418 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
419 where
420 'p: 'b,
421 {
422 MaybeRef::Val(item.clone())
423 }
424}
425
426impl<'p, T: Clone + Eq + Hash, S: BuildHasher> Seq<'p, T> for HashSet<T, S> {
427 type Item<'a>
428 = &'a T
429 where
430 Self: 'a;
431
432 type Iter<'a>
433 = hashbrown::hash_set::Iter<'a, T>
434 where
435 Self: 'a;
436
437 #[inline(always)]
438 fn seq_iter(&self) -> Self::Iter<'_> {
439 self.iter()
440 }
441
442 #[inline(always)]
443 fn contains(&self, val: &T) -> bool
444 where
445 T: PartialEq,
446 {
447 HashSet::contains(self, val)
448 }
449
450 #[inline]
451 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
452 where
453 'p: 'b,
454 S: 'b,
455 {
456 MaybeRef::Val(item.clone())
457 }
458}
459
460#[cfg(feature = "std")]
461impl<'p, T: Clone + Eq + Hash, S: BuildHasher> Seq<'p, T> for std::collections::HashSet<T, S> {
462 type Item<'a>
463 = &'a T
464 where
465 Self: 'a;
466
467 type Iter<'a>
468 = std::collections::hash_set::Iter<'a, T>
469 where
470 Self: 'a;
471
472 #[inline(always)]
473 fn seq_iter(&self) -> Self::Iter<'_> {
474 self.iter()
475 }
476
477 #[inline(always)]
478 fn contains(&self, val: &T) -> bool
479 where
480 T: PartialEq,
481 {
482 self.contains(val)
483 }
484
485 #[inline]
486 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
487 where
488 'p: 'b,
489 S: 'b,
490 {
491 MaybeRef::Val(item.clone())
492 }
493}
494
495impl<'p, T: Clone + Ord> Seq<'p, T> for alloc::collections::BTreeSet<T> {
496 type Item<'a>
497 = &'a T
498 where
499 Self: 'a;
500
501 type Iter<'a>
502 = alloc::collections::btree_set::Iter<'a, T>
503 where
504 Self: 'a;
505
506 #[inline(always)]
507 fn seq_iter(&self) -> Self::Iter<'_> {
508 self.iter()
509 }
510
511 #[inline(always)]
512 fn contains(&self, val: &T) -> bool
513 where
514 T: PartialEq,
515 {
516 self.contains(val)
517 }
518
519 #[inline]
520 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
521 where
522 'p: 'b,
523 {
524 MaybeRef::Val(item.clone())
525 }
526}
527
528impl<'p, T> Seq<'p, T> for Range<T>
529where
530 T: Clone + PartialOrd, Self: Iterator<Item = T>,
532{
533 type Item<'a>
534 = T
535 where
536 Self: 'a;
537
538 type Iter<'a>
539 = Range<T>
540 where
541 Self: 'a;
542
543 #[inline(always)]
544 fn seq_iter(&self) -> Self::Iter<'_> {
545 (*self).clone()
546 }
547
548 #[inline(always)]
549 fn contains(&self, val: &T) -> bool {
550 Range::contains(self, val)
551 }
552
553 #[inline]
554 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
555 where
556 'p: 'b,
557 {
558 MaybeRef::Val(item)
559 }
560}
561
562impl<'p, T> Seq<'p, T> for core::ops::RangeInclusive<T>
563where
564 T: Clone + PartialOrd,
565 Self: Iterator<Item = T>,
566{
567 type Item<'a>
568 = T
569 where
570 Self: 'a;
571
572 type Iter<'a>
573 = core::ops::RangeInclusive<T>
574 where
575 Self: 'a;
576
577 #[inline(always)]
578 fn seq_iter(&self) -> Self::Iter<'_> {
579 self.clone()
580 }
581
582 #[inline(always)]
583 fn contains(&self, val: &T) -> bool {
584 core::ops::RangeInclusive::contains(self, val)
585 }
586
587 #[inline]
588 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
589 where
590 'p: 'b,
591 {
592 MaybeRef::Val(item)
593 }
594}
595
596impl<'p, T> Seq<'p, T> for RangeFrom<T>
597where
598 T: Clone + PartialOrd,
599 Self: Iterator<Item = T>,
600{
601 type Item<'a>
602 = T
603 where
604 Self: 'a;
605
606 type Iter<'a>
607 = RangeFrom<T>
608 where
609 Self: 'a;
610
611 #[inline(always)]
612 fn seq_iter(&self) -> Self::Iter<'_> {
613 self.clone()
614 }
615
616 #[inline(always)]
617 fn contains(&self, val: &T) -> bool {
618 RangeFrom::contains(self, val)
619 }
620
621 #[inline]
622 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, T>
623 where
624 'p: 'b,
625 {
626 MaybeRef::Val(item)
627 }
628}
629
630impl<'p> Seq<'p, char> for str {
631 type Item<'a>
632 = char
633 where
634 Self: 'a;
635
636 type Iter<'a>
637 = core::str::Chars<'a>
638 where
639 Self: 'a;
640
641 #[inline(always)]
642 fn seq_iter(&self) -> Self::Iter<'_> {
643 self.chars()
644 }
645
646 #[inline(always)]
647 fn contains(&self, val: &char) -> bool {
648 self.contains(*val)
649 }
650
651 #[inline]
652 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, char>
653 where
654 'p: 'b,
655 {
656 MaybeRef::Val(item)
657 }
658}
659
660impl<'p> Seq<'p, char> for String {
661 type Item<'a>
662 = char
663 where
664 Self: 'a;
665
666 type Iter<'a>
667 = core::str::Chars<'a>
668 where
669 Self: 'a;
670
671 #[inline(always)]
672 fn seq_iter(&self) -> Self::Iter<'_> {
673 self.chars()
674 }
675
676 #[inline(always)]
677 fn contains(&self, val: &char) -> bool {
678 str::contains(self, *val)
679 }
680
681 #[inline]
682 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, char>
683 where
684 'p: 'b,
685 {
686 MaybeRef::Val(item)
687 }
688}
689
690impl<'p> Seq<'p, char> for &'p str {
691 type Item<'a>
692 = char
693 where
694 Self: 'a;
695
696 type Iter<'a>
697 = core::str::Chars<'a>
698 where
699 Self: 'a;
700
701 #[inline(always)]
702 fn seq_iter(&self) -> Self::Iter<'_> {
703 self.chars()
704 }
705
706 #[inline(always)]
707 fn contains(&self, val: &char) -> bool {
708 str::contains(self, *val)
709 }
710
711 #[inline]
712 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, char>
713 where
714 'p: 'b,
715 {
716 MaybeRef::Val(item)
717 }
718
719 #[doc(hidden)]
720 #[cfg(feature = "debug")]
721 fn seq_info(&self, _scope: &mut debug::NodeScope) -> debug::SeqInfo {
722 debug::SeqInfo::String(self.to_string())
723 }
724}
725
726impl<'p> Seq<'p, &'p Grapheme> for &'p str {
727 type Item<'a>
728 = &'p Grapheme
729 where
730 Self: 'a;
731
732 type Iter<'a>
733 = GraphemesIter<'p>
734 where
735 Self: 'a;
736
737 #[inline(always)]
738 fn seq_iter(&self) -> Self::Iter<'_> {
739 Graphemes::new(self).iter()
740 }
741
742 #[inline(always)]
743 fn contains(&self, val: &&'p Grapheme) -> bool {
744 Graphemes::new(self).contains(val)
745 }
746
747 #[inline]
748 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, &'p Grapheme>
749 where
750 'p: 'b,
751 {
752 MaybeRef::Val(item)
753 }
754
755 #[doc(hidden)]
756 #[cfg(feature = "debug")]
757 fn seq_info(&self, _scope: &mut debug::NodeScope) -> debug::SeqInfo {
758 debug::SeqInfo::String(self.to_string())
759 }
760}
761
762impl<'p> Seq<'p, &'p Grapheme> for &'p Graphemes {
763 type Item<'a>
764 = &'p Grapheme
765 where
766 Self: 'a;
767
768 type Iter<'a>
769 = GraphemesIter<'p>
770 where
771 Self: 'a;
772
773 #[inline(always)]
774 fn seq_iter(&self) -> Self::Iter<'_> {
775 self.iter()
776 }
777
778 #[inline(always)]
779 fn contains(&self, val: &&'p Grapheme) -> bool {
780 self.iter().any(|i| i == *val)
781 }
782
783 #[inline]
784 fn to_maybe_ref<'b>(item: Self::Item<'b>) -> MaybeRef<'p, &'p Grapheme>
785 where
786 'p: 'b,
787 {
788 MaybeRef::Val(item)
789 }
790}
791
792pub trait OrderedSeq<'p, T>: Seq<'p, T> {}
796
797impl<T: Clone> OrderedSeq<'_, T> for T {}
798
799impl<'p, T> OrderedSeq<'p, T> for &'p T {}
800impl<'p, T> OrderedSeq<'p, T> for &'p [T] {}
801impl<T: Clone, const N: usize> OrderedSeq<'_, T> for [T; N] {}
802impl<'p, T, const N: usize> OrderedSeq<'p, T> for &'p [T; N] {}
803impl<T: Clone> OrderedSeq<'_, T> for Vec<T> {}
804impl<'p, T> OrderedSeq<'p, T> for Range<T> where Self: Seq<'p, T> {}
805impl<'p, T> OrderedSeq<'p, T> for core::ops::RangeInclusive<T> where Self: Seq<'p, T> {}
806impl<'p, T> OrderedSeq<'p, T> for RangeFrom<T> where Self: Seq<'p, T> {}
807
808impl OrderedSeq<'_, char> for str {}
809impl OrderedSeq<'_, char> for String {}
810impl<'p> OrderedSeq<'p, char> for &'p str {}
811impl<'p> OrderedSeq<'p, &'p Grapheme> for &'p str {}
812impl<'p> OrderedSeq<'p, &'p Grapheme> for &'p Graphemes {}
813
814#[cfg(test)]
815mod test {
816 use super::*;
817
818 fn init_container<C: ContainerExactly<usize>>() -> C {
819 let mut uninit = C::uninit();
820 for idx in 0..C::LEN {
821 C::write(&mut uninit, idx, idx);
822 }
823 unsafe { C::take(uninit) }
825 }
826
827 fn drop_container<C: ContainerExactly<usize>>() {
828 let mut uninit = C::uninit();
829 for idx in 0..(C::LEN / 2) {
830 C::write(&mut uninit, idx, idx);
831 }
832 unsafe { C::drop_before(&mut uninit, C::LEN / 2) };
834 }
835
836 #[test]
837 fn exact_array() {
838 let c = init_container::<[usize; 4]>();
839 assert_eq!(&c, &[0, 1, 2, 3]);
840 drop_container::<[usize; 4]>();
841 }
842
843 }