@@ -17,7 +17,8 @@ use core::ops::Index;
17
17
use core:: { fmt, intrinsics, mem, ptr} ;
18
18
19
19
use borrow:: Borrow ;
20
- use Bound :: { self , Excluded , Included , Unbounded } ;
20
+ use Bound :: { Excluded , Included , Unbounded } ;
21
+ use range:: RangeArgument ;
21
22
22
23
use super :: node:: { self , Handle , NodeRef , marker} ;
23
24
use super :: search;
@@ -654,10 +655,12 @@ impl<K: Ord, V> BTreeMap<K, V> {
654
655
self . fix_right_edge ( ) ;
655
656
}
656
657
657
- /// Constructs a double-ended iterator over a sub-range of elements in the map, starting
658
- /// at min, and ending at max. If min is `Unbounded`, then it will be treated as "negative
659
- /// infinity", and if max is `Unbounded`, then it will be treated as "positive infinity".
660
- /// Thus range(Unbounded, Unbounded) will yield the whole collection.
658
+ /// Constructs a double-ended iterator over a sub-range of elements in the map.
659
+ /// The simplest way is to use the range synax `min..max`, thus `range(min..max)` will
660
+ /// yield elements from min (inclusive) to max (exclusive).
661
+ /// The range may also be entered as `(Bound<T>, Bound<T>)`, so for example
662
+ /// `range((Excluded(4), Included(10)))` will yield a left-exclusive, right-inclusive
663
+ /// range from 4 to 10.
661
664
///
662
665
/// # Examples
663
666
///
@@ -667,26 +670,25 @@ impl<K: Ord, V> BTreeMap<K, V> {
667
670
/// #![feature(btree_range, collections_bound)]
668
671
///
669
672
/// use std::collections::BTreeMap;
670
- /// use std::collections::Bound::{ Included, Unbounded} ;
673
+ /// use std::collections::Bound::Included;
671
674
///
672
675
/// let mut map = BTreeMap::new();
673
676
/// map.insert(3, "a");
674
677
/// map.insert(5, "b");
675
678
/// map.insert(8, "c");
676
- /// for (&key, &value) in map.range(Included(&4), Included(&8)) {
679
+ /// for (&key, &value) in map.range(( Included(&4), Included(&8) )) {
677
680
/// println!("{}: {}", key, value);
678
681
/// }
679
- /// assert_eq!(Some((&5, &"b")), map.range(Included(&4), Unbounded ).next());
682
+ /// assert_eq!(Some((&5, &"b")), map.range(4.. ).next());
680
683
/// ```
681
684
#[ unstable( feature = "btree_range" ,
682
685
reason = "matches collection reform specification, waiting for dust to settle" ,
683
686
issue = "27787" ) ]
684
- pub fn range < Min : ?Sized + Ord , Max : ?Sized + Ord > ( & self ,
685
- min : Bound < & Min > ,
686
- max : Bound < & Max > )
687
- -> Range < K , V >
688
- where K : Borrow < Min > + Borrow < Max >
687
+ pub fn range < T : ?Sized , R > ( & self , range : R ) -> Range < K , V >
688
+ where T : Ord , K : Borrow < T > , R : RangeArgument < T >
689
689
{
690
+ let min = range. start ( ) ;
691
+ let max = range. end ( ) ;
690
692
let front = match min {
691
693
Included ( key) => {
692
694
match search:: search_tree ( self . root . as_ref ( ) , key) {
@@ -745,25 +747,26 @@ impl<K: Ord, V> BTreeMap<K, V> {
745
747
}
746
748
}
747
749
748
- /// Constructs a mutable double-ended iterator over a sub-range of elements in the map, starting
749
- /// at min, and ending at max. If min is `Unbounded`, then it will be treated as "negative
750
- /// infinity", and if max is `Unbounded`, then it will be treated as "positive infinity".
751
- /// Thus range(Unbounded, Unbounded) will yield the whole collection.
750
+ /// Constructs a mutable double-ended iterator over a sub-range of elements in the map.
751
+ /// The simplest way is to use the range synax `min..max`, thus `range(min..max)` will
752
+ /// yield elements from min (inclusive) to max (exclusive).
753
+ /// The range may also be entered as `(Bound<T>, Bound<T>)`, so for example
754
+ /// `range((Excluded(4), Included(10)))` will yield a left-exclusive, right-inclusive
755
+ /// range from 4 to 10.
752
756
///
753
757
/// # Examples
754
758
///
755
759
/// Basic usage:
756
760
///
757
761
/// ```
758
- /// #![feature(btree_range, collections_bound )]
762
+ /// #![feature(btree_range)]
759
763
///
760
764
/// use std::collections::BTreeMap;
761
- /// use std::collections::Bound::{Included, Excluded};
762
765
///
763
766
/// let mut map: BTreeMap<&str, i32> = ["Alice", "Bob", "Carol", "Cheryl"].iter()
764
767
/// .map(|&s| (s, 0))
765
768
/// .collect();
766
- /// for (_, balance) in map.range_mut(Included( "B"), Excluded( "Cheryl") ) {
769
+ /// for (_, balance) in map.range_mut("B".. "Cheryl") {
767
770
/// *balance += 100;
768
771
/// }
769
772
/// for (name, balance) in &map {
@@ -773,12 +776,11 @@ impl<K: Ord, V> BTreeMap<K, V> {
773
776
#[ unstable( feature = "btree_range" ,
774
777
reason = "matches collection reform specification, waiting for dust to settle" ,
775
778
issue = "27787" ) ]
776
- pub fn range_mut < Min : ?Sized + Ord , Max : ?Sized + Ord > ( & mut self ,
777
- min : Bound < & Min > ,
778
- max : Bound < & Max > )
779
- -> RangeMut < K , V >
780
- where K : Borrow < Min > + Borrow < Max >
779
+ pub fn range_mut < T : ?Sized , R > ( & mut self , range : R ) -> RangeMut < K , V >
780
+ where T : Ord , K : Borrow < T > , R : RangeArgument < T >
781
781
{
782
+ let min = range. start ( ) ;
783
+ let max = range. end ( ) ;
782
784
let root1 = self . root . as_mut ( ) ;
783
785
let root2 = unsafe { ptr:: read ( & root1) } ;
784
786
0 commit comments