@@ -32,6 +32,7 @@ use std::option;
32
32
use std:: slice;
33
33
use std:: vec;
34
34
35
+ use crate :: drops:: { NoDrop , TrivialDrop } ;
35
36
#[ cfg( feature = "parsing" ) ]
36
37
use crate :: parse:: { Parse , ParseStream , Result } ;
37
38
#[ cfg( feature = "parsing" ) ]
@@ -104,21 +105,21 @@ impl<T, P> Punctuated<T, P> {
104
105
/// Returns an iterator over borrowed syntax tree nodes of type `&T`.
105
106
pub fn iter ( & self ) -> Iter < T > {
106
107
Iter {
107
- inner : Box :: new ( PrivateIter {
108
+ inner : Box :: new ( NoDrop :: new ( PrivateIter {
108
109
inner : self . inner . iter ( ) ,
109
110
last : self . last . as_ref ( ) . map ( Box :: as_ref) . into_iter ( ) ,
110
- } ) ,
111
+ } ) ) ,
111
112
}
112
113
}
113
114
114
115
/// Returns an iterator over mutably borrowed syntax tree nodes of type
115
116
/// `&mut T`.
116
117
pub fn iter_mut ( & mut self ) -> IterMut < T > {
117
118
IterMut {
118
- inner : Box :: new ( PrivateIterMut {
119
+ inner : Box :: new ( NoDrop :: new ( PrivateIterMut {
119
120
inner : self . inner . iter_mut ( ) ,
120
121
last : self . last . as_mut ( ) . map ( Box :: as_mut) . into_iter ( ) ,
121
- } ) ,
122
+ } ) ) ,
122
123
}
123
124
}
124
125
@@ -721,24 +722,31 @@ pub struct Iter<'a, T: 'a> {
721
722
// The `Item = &'a T` needs to be specified to support rustc 1.31 and older.
722
723
// On modern compilers we would be able to write just IterTrait<'a, T> where
723
724
// Item can be inferred unambiguously from the supertrait.
724
- inner : Box < dyn IterTrait < ' a , T , Item = & ' a T > + ' a > ,
725
+ inner : Box < NoDrop < dyn IterTrait < ' a , T , Item = & ' a T > + ' a > > ,
725
726
}
726
727
727
728
trait IterTrait < ' a , T : ' a > :
728
729
DoubleEndedIterator < Item = & ' a T > + ExactSizeIterator < Item = & ' a T >
729
730
{
730
- fn clone_box ( & self ) -> Box < dyn IterTrait < ' a , T , Item = & ' a T > + ' a > ;
731
+ fn clone_box ( & self ) -> Box < NoDrop < dyn IterTrait < ' a , T , Item = & ' a T > + ' a > > ;
731
732
}
732
733
733
734
struct PrivateIter < ' a , T : ' a , P : ' a > {
734
735
inner : slice:: Iter < ' a , ( T , P ) > ,
735
736
last : option:: IntoIter < & ' a T > ,
736
737
}
737
738
739
+ impl < ' a , T , P > TrivialDrop for PrivateIter < ' a , T , P >
740
+ where
741
+ slice:: Iter < ' a , ( T , P ) > : TrivialDrop ,
742
+ option:: IntoIter < & ' a T > : TrivialDrop ,
743
+ {
744
+ }
745
+
738
746
#[ cfg( any( feature = "full" , feature = "derive" ) ) ]
739
747
pub ( crate ) fn empty_punctuated_iter < ' a , T > ( ) -> Iter < ' a , T > {
740
748
Iter {
741
- inner : Box :: new ( iter:: empty ( ) ) ,
749
+ inner : Box :: new ( NoDrop :: new ( iter:: empty ( ) ) ) ,
742
750
}
743
751
}
744
752
@@ -813,10 +821,14 @@ impl<'a, T, P> Clone for PrivateIter<'a, T, P> {
813
821
impl < ' a , T , I > IterTrait < ' a , T > for I
814
822
where
815
823
T : ' a ,
816
- I : DoubleEndedIterator < Item = & ' a T > + ExactSizeIterator < Item = & ' a T > + Clone + ' a ,
824
+ I : DoubleEndedIterator < Item = & ' a T >
825
+ + ExactSizeIterator < Item = & ' a T >
826
+ + Clone
827
+ + TrivialDrop
828
+ + ' a ,
817
829
{
818
- fn clone_box ( & self ) -> Box < dyn IterTrait < ' a , T , Item = & ' a T > + ' a > {
819
- Box :: new ( self . clone ( ) )
830
+ fn clone_box ( & self ) -> Box < NoDrop < dyn IterTrait < ' a , T , Item = & ' a T > + ' a > > {
831
+ Box :: new ( NoDrop :: new ( self . clone ( ) ) )
820
832
}
821
833
}
822
834
@@ -826,7 +838,7 @@ where
826
838
///
827
839
/// [module documentation]: self
828
840
pub struct IterMut < ' a , T : ' a > {
829
- inner : Box < dyn IterMutTrait < ' a , T , Item = & ' a mut T > + ' a > ,
841
+ inner : Box < NoDrop < dyn IterMutTrait < ' a , T , Item = & ' a mut T > + ' a > > ,
830
842
}
831
843
832
844
trait IterMutTrait < ' a , T : ' a > :
@@ -839,10 +851,17 @@ struct PrivateIterMut<'a, T: 'a, P: 'a> {
839
851
last : option:: IntoIter < & ' a mut T > ,
840
852
}
841
853
854
+ impl < ' a , T , P > TrivialDrop for PrivateIterMut < ' a , T , P >
855
+ where
856
+ slice:: IterMut < ' a , ( T , P ) > : TrivialDrop ,
857
+ option:: IntoIter < & ' a mut T > : TrivialDrop ,
858
+ {
859
+ }
860
+
842
861
#[ cfg( any( feature = "full" , feature = "derive" ) ) ]
843
862
pub ( crate ) fn empty_punctuated_iter_mut < ' a , T > ( ) -> IterMut < ' a , T > {
844
863
IterMut {
845
- inner : Box :: new ( iter:: empty ( ) ) ,
864
+ inner : Box :: new ( NoDrop :: new ( iter:: empty ( ) ) ) ,
846
865
}
847
866
}
848
867
0 commit comments