Skip to content

Commit bfc9df0

Browse files
WIP
1 parent 57f4d7a commit bfc9df0

File tree

5 files changed

+617
-407
lines changed

5 files changed

+617
-407
lines changed

library/core/src/slice/iter.rs

+66-30
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ pub(super) trait SplitIter: DoubleEndedIterator {
376376

377377
split_iter! {
378378
#[stable(feature = "rust1", since = "1.0.0")]
379+
#[debug(stable(feature = "core_impl_debug", since = "1.9.0"))]
379380
#[fused(stable(feature = "fused", since = "1.26.0"))]
380381
#[must_use = "iterators are lazy and do nothing unless consumed"]
381382
/// An iterator over subslices separated by elements that match a predicate
@@ -393,12 +394,16 @@ split_iter! {
393394
/// [`split`]: slice::split
394395
/// [slices]: slice
395396
struct Split<shared_ref: &'a> {
396-
#[stable(feature = "core_impl_debug", since = "1.9.0")]
397-
debug: "Split",
398397
include_leading: false,
399398
include_trailing: false,
400399
}
400+
}
401401

402+
impl<'a, T, P> Split<'a, T, P>
403+
where
404+
T: 'a,
405+
P: FnMut(&T) -> bool,
406+
{
402407
/// Returns a slice which contains items not yet handled by split.
403408
/// # Example
404409
///
@@ -417,6 +422,7 @@ split_iter! {
417422

418423
split_iter! {
419424
#[stable(feature = "rust1", since = "1.0.0")]
425+
#[debug(stable(feature = "core_impl_debug", since = "1.9.0"))]
420426
#[fused(stable(feature = "fused", since = "1.26.0"))]
421427
#[must_use = "iterators are lazy and do nothing unless consumed"]
422428
/// An iterator over the mutable subslices of the vector which are separated
@@ -434,15 +440,14 @@ split_iter! {
434440
/// [`split_mut`]: slice::split_mut
435441
/// [slices]: slice
436442
struct SplitMut<mut_ref: &'a> {
437-
#[stable(feature = "core_impl_debug", since = "1.9.0")]
438-
debug: "SplitMut",
439443
include_leading: false,
440444
include_trailing: false,
441445
}
442446
}
443447

444448
split_iter! {
445449
#[stable(feature = "split_inclusive", since = "1.51.0")]
450+
#[debug(stable(feature = "core_impl_debug", since = "1.9.0"))]
446451
#[fused(stable(feature = "split_inclusive", since = "1.51.0"))]
447452
#[must_use = "iterators are lazy and do nothing unless consumed"]
448453
/// An iterator over subslices separated by elements that match a predicate
@@ -461,15 +466,14 @@ split_iter! {
461466
/// [`split_inclusive`]: slice::split_inclusive
462467
/// [slices]: slice
463468
struct SplitInclusive<shared_ref: &'a> {
464-
#[stable(feature = "split_inclusive", since = "1.51.0")]
465-
debug: "SplitInclusive",
466469
include_leading: false,
467470
include_trailing: true,
468471
}
469472
}
470473

471474
split_iter! {
472475
#[stable(feature = "split_inclusive", since = "1.51.0")]
476+
#[debug(stable(feature = "split_inclusive", since = "1.51.0"))]
473477
#[fused(stable(feature = "split_inclusive", since = "1.51.0"))]
474478
#[must_use = "iterators are lazy and do nothing unless consumed"]
475479
/// An iterator over the mutable subslices of the vector which are separated
@@ -488,15 +492,14 @@ split_iter! {
488492
/// [`split_inclusive_mut`]: slice::split_inclusive_mut
489493
/// [slices]: slice
490494
struct SplitInclusiveMut<mut_ref: &'a> {
491-
#[stable(feature = "split_inclusive", since = "1.51.0")]
492-
debug: "SplitInclusiveMut",
493495
include_leading: false,
494496
include_trailing: true,
495497
}
496498
}
497499

498500
split_iter! {
499501
#[unstable(feature = "split_inclusive_variants", issue = "none")]
502+
#[debug(stable(feature = "split_inclusive", since = "1.51.0"))]
500503
#[fused(unstable(feature = "split_inclusive_variants", issue = "none"))]
501504
#[must_use = "iterators are lazy and do nothing unless consumed"]
502505
/// An iterator over subslices separated by elements that match a predicate
@@ -517,15 +520,14 @@ split_iter! {
517520
/// [`split_left_inclusive`]: slice::split_left_inclusive
518521
/// [slices]: slice
519522
struct SplitLeftInclusive<shared_ref: &'a> {
520-
#[unstable(feature = "split_inclusive_variants", issue = "none")]
521-
debug: "SplitLeftInclusive",
522523
include_leading: true,
523524
include_trailing: false,
524525
}
525526
}
526527

527528
split_iter! {
528529
#[unstable(feature = "split_inclusive_variants", issue = "none")]
530+
#[debug(unstable(feature = "split_inclusive_variants", issue = "none"))]
529531
#[fused(unstable(feature = "split_inclusive_variants", issue = "none"))]
530532
#[must_use = "iterators are lazy and do nothing unless consumed"]
531533
/// An iterator over the mutable subslices of the vector which are separated
@@ -547,8 +549,6 @@ split_iter! {
547549
/// [`split_left_inclusive_mut`]: slice::split_left_inclusive_mut
548550
/// [slices]: slice
549551
struct SplitLeftInclusiveMut<mut_ref: &'a> {
550-
#[unstable(feature = "split_inclusive_variants", issue = "none")]
551-
debug: "SplitLeftInclusiveMut",
552552
include_leading: true,
553553
include_trailing: false,
554554
}
@@ -571,7 +571,7 @@ reverse_iter! {
571571
///
572572
/// [`rsplit`]: slice::rsplit
573573
/// [slices]: slice
574-
pub struct RSplit { "RSplit"; Split }: Clone
574+
pub struct RSplit { inner: Split }: Clone
575575
}
576576

577577
reverse_iter! {
@@ -594,7 +594,7 @@ reverse_iter! {
594594
///
595595
/// [`rsplit_left_inclusive`]: slice::rsplit_inclusive
596596
/// [slices]: slice
597-
pub struct RSplitInclusive { "RSplitInclusive"; SplitInclusive }: Clone
597+
pub struct RSplitInclusive { inner: SplitInclusive }: Clone
598598
}
599599

600600
reverse_iter! {
@@ -617,7 +617,7 @@ reverse_iter! {
617617
///
618618
/// [`rsplit_left_inclusive`]: slice::rsplit_left_inclusive
619619
/// [slices]: slice
620-
pub struct RSplitLeftInclusive { "RSplitLeftInclusive"; SplitLeftInclusive }: Clone
620+
pub struct RSplitLeftInclusive { inner: SplitLeftInclusive }: Clone
621621
}
622622

623623
reverse_iter! {
@@ -637,7 +637,7 @@ reverse_iter! {
637637
///
638638
/// [`rsplit_mut`]: slice::rsplit_mut
639639
/// [slices]: slice
640-
pub struct RSplitMut { "RSplitMut"; SplitMut }
640+
pub struct RSplitMut { inner: SplitMut }
641641
}
642642

643643
reverse_iter! {
@@ -660,7 +660,7 @@ reverse_iter! {
660660
///
661661
/// [`rsplit_inclusive_mut`]: slice::rsplit_inclusive_mut
662662
/// [slices]: slice
663-
pub struct RSplitInclusiveMut { "RSplitInclusiveMut" ; SplitInclusiveMut }
663+
pub struct RSplitInclusiveMut { inner: SplitInclusiveMut }
664664
}
665665

666666
reverse_iter! {
@@ -683,7 +683,7 @@ reverse_iter! {
683683
///
684684
/// [`rsplit_left_inclusive_mut`]: slice::rsplit_left_inclusive_mut
685685
/// [slices]: slice
686-
pub struct RSplitLeftInclusiveMut { "RSplitLeftInclusiveMut" ; SplitLeftInclusiveMut }
686+
pub struct RSplitLeftInclusiveMut { inner: SplitLeftInclusiveMut }
687687
}
688688

689689
/// An private iterator over subslices separated by elements that
@@ -741,7 +741,10 @@ iter_n! {
741741
///
742742
/// [`splitn`]: slice::splitn
743743
/// [slices]: slice
744-
pub struct SplitN {"SplitN"; Split }: Clone
744+
pub struct SplitN { inner: Split }: Clone
745+
746+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
747+
fn max_items;
745748
}
746749

747750
iter_n! {
@@ -763,7 +766,10 @@ iter_n! {
763766
///
764767
/// [`rsplitn`]: slice::rsplitn
765768
/// [slices]: slice
766-
pub struct RSplitN {"RSplitN"; RSplit }: Clone
769+
pub struct RSplitN { inner: RSplit }: Clone
770+
771+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
772+
fn max_items;
767773
}
768774

769775
iter_n! {
@@ -784,7 +790,10 @@ iter_n! {
784790
///
785791
/// [`splitn_mut`]: slice::splitn_mut
786792
/// [slices]: slice
787-
pub struct SplitNMut {"SplitNMut"; SplitMut }
793+
pub struct SplitNMut { inner: SplitMut }
794+
795+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
796+
fn max_items;
788797
}
789798

790799
iter_n! {
@@ -806,7 +815,10 @@ iter_n! {
806815
///
807816
/// [`rsplitn_mut`]: slice::rsplitn_mut
808817
/// [slices]: slice
809-
pub struct RSplitNMut {"RSplitNMut"; RSplitMut }
818+
pub struct RSplitNMut { inner: RSplitMut }
819+
820+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
821+
fn max_items;
810822
}
811823

812824
iter_n! {
@@ -830,7 +842,10 @@ iter_n! {
830842
///
831843
/// [`splitn_inclusive`]: slice::splitn_inclusive
832844
/// [slices]: slice
833-
pub struct SplitNInclusive {"SplitNInclusive"; SplitInclusive}: Clone
845+
pub struct SplitNInclusive { inner: SplitInclusive }: Clone
846+
847+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
848+
fn max_items;
834849
}
835850

836851
iter_n! {
@@ -854,7 +869,10 @@ iter_n! {
854869
///
855870
/// [`splitn_left_inclusive`]: slice::splitn_left_inclusive
856871
/// [slices]: slice
857-
pub struct SplitNLeftInclusive {"SplitNLeftInclusive"; SplitLeftInclusive}: Clone
872+
pub struct SplitNLeftInclusive { inner: SplitLeftInclusive }: Clone
873+
874+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
875+
fn max_items;
858876
}
859877

860878
iter_n! {
@@ -878,7 +896,10 @@ iter_n! {
878896
///
879897
/// [`rsplitn_inclusive`]: slice::rsplitn_inclusive
880898
/// [slices]: slice
881-
pub struct RSplitNInclusive {"RSplitNInclusive"; RSplitInclusive}: Clone
899+
pub struct RSplitNInclusive { inner: RSplitInclusive }: Clone
900+
901+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
902+
fn max_items;
882903
}
883904

884905
iter_n! {
@@ -902,7 +923,10 @@ iter_n! {
902923
///
903924
/// [`rsplitn_left_inclusive`]: slice::rsplitn_left_inclusive
904925
/// [slices]: slice
905-
pub struct RSplitNLeftInclusive {"RSplitNLeftInclusive"; RSplitLeftInclusive}: Clone
926+
pub struct RSplitNLeftInclusive { inner: RSplitLeftInclusive }: Clone
927+
928+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
929+
fn max_items;
906930
}
907931

908932
iter_n! {
@@ -926,7 +950,10 @@ iter_n! {
926950
///
927951
/// [`splitn_inclusive_mut`]: slice::splitn_inclusive_mut
928952
/// [slices]: slice
929-
pub struct SplitNInclusiveMut {"SplitNInclusiveMut"; SplitInclusiveMut}
953+
pub struct SplitNInclusiveMut { inner: SplitInclusiveMut }
954+
955+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
956+
fn max_items;
930957
}
931958

932959
iter_n! {
@@ -950,7 +977,10 @@ iter_n! {
950977
///
951978
/// [`splitn_left_inclusive_mut`]: slice::splitn_left_inclusive_mut
952979
/// [slices]: slice
953-
pub struct SplitNLeftInclusiveMut {"SplitNLeftInclusiveMut"; SplitLeftInclusiveMut}
980+
pub struct SplitNLeftInclusiveMut { inner: SplitLeftInclusiveMut }
981+
982+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
983+
fn max_items;
954984
}
955985

956986
iter_n! {
@@ -974,7 +1004,10 @@ iter_n! {
9741004
///
9751005
/// [`rsplitn_inclusive_mut`]: slice::rsplitn_inclusive_mut
9761006
/// [slices]: slice
977-
pub struct RSplitNInclusiveMut {"RSplitNInclusiveMut"; RSplitInclusiveMut}
1007+
pub struct RSplitNInclusiveMut { inner: RSplitInclusiveMut }
1008+
1009+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
1010+
fn max_items;
9781011
}
9791012

9801013
iter_n! {
@@ -998,7 +1031,10 @@ iter_n! {
9981031
///
9991032
/// [`rsplitn_left_inclusive_mut`]: slice::rsplitn_left_inclusive_mut
10001033
/// [slices]: slice
1001-
pub struct RSplitNLeftInclusiveMut {"RSplitNLeftInclusiveMut"; RSplitLeftInclusiveMut}
1034+
pub struct RSplitNLeftInclusiveMut { inner: RSplitLeftInclusiveMut }
1035+
1036+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
1037+
fn max_items;
10021038
}
10031039

10041040
/// An iterator over overlapping subslices of length `size`.

library/core/src/slice/iter/macros.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,13 @@ macro_rules! iterator {
400400
macro_rules! split_iter {
401401
(
402402
#[$stability:meta]
403+
#[debug($debug_stability:meta)]
403404
#[fused($fused_stability:meta)]
404405
$(#[$outer:meta])*
405406
struct $split_iter:ident<
406407
$(shared_ref: & $lt:lifetime)?
407408
$(mut_ref: & $m_lt:lifetime)?
408409
> {
409-
#[$debug_stability:meta]
410-
debug: $debug_name:literal,
411410
include_leading: $include_leading:literal,
412411
include_trailing: $include_trailing:literal,
413412
}
@@ -442,7 +441,7 @@ macro_rules! split_iter {
442441
P: FnMut(&T) -> bool,
443442
{
444443
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
445-
f.debug_struct($debug_name)
444+
f.debug_struct(stringify!(split_iter))
446445
.field("v", &self.v)
447446
.field("finished", &self.finished)
448447
.finish()
@@ -659,7 +658,7 @@ macro_rules! reverse_iter {
659658
(
660659
#[$stability:meta]
661660
$(#[$outer:meta])*
662-
$vis:vis struct $rev:ident { $str:literal ; $inner:ident } $(: $clone:ident)?
661+
$vis:vis struct $rev:ident { inner: $inner:ident } $(: $clone:ident)?
663662
) => {
664663
$(#[$outer])*
665664
#[$stability]
@@ -683,7 +682,7 @@ macro_rules! reverse_iter {
683682
P: FnMut(&T) -> bool,
684683
{
685684
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
686-
f.debug_struct($str)
685+
f.debug_struct(stringify!($rev))
687686
.field("v", &self.inner.v)
688687
.field("finished", &self.inner.finished)
689688
.finish()
@@ -754,7 +753,10 @@ macro_rules! iter_n {
754753
#[$stability:meta]
755754
#[fused($fused_stability:meta)]
756755
$(#[$outer:meta])*
757-
$vis:vis struct $iter_n:ident { $str:literal ; $inner:ident } $(: $clone:ident)?
756+
$vis:vis struct $iter_n:ident { inner: $inner:ident } $(: $clone:ident)?
757+
758+
$(#[$max_items_attrs:meta])*
759+
fn max_items;
758760
) => {
759761
$(#[$outer])*
760762
#[$stability]
@@ -790,7 +792,7 @@ macro_rules! iter_n {
790792
P: FnMut(&T) -> bool,
791793
{
792794
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
793-
f.debug_struct($str).field("inner", &self.inner).finish()
795+
f.debug_struct(stringify!($iter_n)).field("inner", &self.inner).finish()
794796
}
795797
}
796798

@@ -814,5 +816,13 @@ macro_rules! iter_n {
814816

815817
#[$fused_stability]
816818
impl<'a, T, P> FusedIterator for $iter_n<'a, T, P> where P: FnMut(&T) -> bool {}
819+
820+
impl<'a, T, P> $inner<'a, T, P> where P: FnMut(&T) -> bool {
821+
$(#[$max_items_attrs])*
822+
#[inline]
823+
pub fn max_items(self, n: usize) -> $iter_n<'a, T, P> {
824+
$iter_n::new(self, n)
825+
}
826+
}
817827
};
818828
}

0 commit comments

Comments
 (0)