1
- use std:: ops;
1
+ use std:: ops:: { Bound , Range , RangeBounds } ;
2
2
3
3
/// A owned window around an underlying buffer.
4
4
///
@@ -20,7 +20,7 @@ use std::ops;
20
20
#[ derive( Debug ) ]
21
21
pub struct Window < T > {
22
22
inner : T ,
23
- range : ops :: Range < usize > ,
23
+ range : Range < usize > ,
24
24
}
25
25
26
26
impl < T : AsRef < [ u8 ] > > Window < T > {
@@ -29,8 +29,8 @@ impl<T: AsRef<[u8]>> Window<T> {
29
29
///
30
30
/// Further methods can be called on the returned `Window<T>` to alter the
31
31
/// window into the data provided.
32
- pub fn new ( t : T ) -> Window < T > {
33
- Window {
32
+ pub fn new ( t : T ) -> Self {
33
+ Self {
34
34
range : 0 ..t. as_ref ( ) . len ( ) ,
35
35
inner : t,
36
36
}
@@ -65,43 +65,36 @@ impl<T: AsRef<[u8]>> Window<T> {
65
65
self . range . end
66
66
}
67
67
68
- /// Changes the starting index of this window to the index specified.
68
+ /// Changes the range of this window to the range specified.
69
69
///
70
70
/// Returns the windows back to chain multiple calls to this method.
71
71
///
72
72
/// # Panics
73
73
///
74
- /// This method will panic if `start` is out of bounds for the underlying
75
- /// slice or if it comes after the `end` configured in this window.
76
- pub fn set_start ( & mut self , start : usize ) -> & mut Window < T > {
77
- assert ! ( start <= self . inner. as_ref( ) . len( ) ) ;
78
- assert ! ( start <= self . range. end) ;
79
- self . range . start = start;
80
- self
81
- }
82
-
83
- /// Changes the end index of this window to the index specified.
84
- ///
85
- /// Returns the windows back to chain multiple calls to this method.
74
+ /// This method will panic if `range` is out of bounds for the underlying
75
+ /// slice or if [`start_bound()`] of `range` comes after the [`end_bound()`].
86
76
///
87
- /// # Panics
88
- ///
89
- /// This method will panic if `end` is out of bounds for the underlying
90
- /// slice or if it comes before the `start` configured in this window.
91
- pub fn set_end ( & mut self , end : usize ) -> & mut Window < T > {
77
+ /// [`start_bound()`]: std::ops::RangeBounds::start_bound
78
+ /// [`end_bound()`]: std::ops::RangeBounds::end_bound
79
+ pub fn set < R : RangeBounds < usize > > ( & mut self , range : R ) -> & mut Self {
80
+ let start = match range. start_bound ( ) {
81
+ Bound :: Included ( n) => * n,
82
+ Bound :: Excluded ( n) => * n + 1 ,
83
+ Bound :: Unbounded => 0 ,
84
+ } ;
85
+ let end = match range. end_bound ( ) {
86
+ Bound :: Included ( n) => * n + 1 ,
87
+ Bound :: Excluded ( n) => * n,
88
+ Bound :: Unbounded => self . inner . as_ref ( ) . len ( ) ,
89
+ } ;
90
+
92
91
assert ! ( end <= self . inner. as_ref( ) . len( ) ) ;
93
- assert ! ( self . range. start <= end) ;
92
+ assert ! ( start <= end) ;
93
+
94
+ self . range . start = start;
94
95
self . range . end = end;
95
96
self
96
97
}
97
-
98
- // TODO: how about a generic set() method along the lines of:
99
- //
100
- // buffer.set(..3)
101
- // .set(0..2)
102
- // .set(4..)
103
- //
104
- // etc.
105
98
}
106
99
107
100
impl < T : AsRef < [ u8 ] > > AsRef < [ u8 ] > for Window < T > {
0 commit comments