remove unmutable slices from Buffer

This commit is contained in:
Szymon Walter 2018-03-19 08:28:59 +01:00
parent 5991420aa8
commit 172c267917

View file

@ -16,31 +16,17 @@ where
{ {
fn len(&self) -> Length; fn len(&self) -> Length;
fn commit(&mut self, slice: Option<BufferCommit<T>>); fn commit(&mut self, slice: Option<BufferCommit<T>>);
unsafe fn slice_unchecked(&self, range: Range<usize>) -> &[T]; unsafe fn slice_unchecked<'a>(
unsafe fn slice_unchecked_mut<'a>(
&'a self, &'a self,
range: Range<usize>, range: Range<usize>,
) -> BufferSlice<'a, T> { ) -> BufferSlice<'a, T>;
let index = range.start;
let slice = self.slice_unchecked(range);
BufferSlice::new(slice, index)
}
fn slice(&self, range: Range<usize>) -> Option<&[T]> { fn slice<'a>(
if self.len() >= range.end && self.len() > range.start {
unsafe { Some(self.slice_unchecked(range)) }
} else {
None
}
}
fn slice_mut<'a>(
&'a mut self, &'a mut self,
range: Range<usize>, range: Range<usize>,
) -> Option<BufferSlice<'a, T>> { ) -> Option<BufferSlice<'a, T>> {
if self.len() >= range.end && self.len() > range.start { if self.len() >= range.end && self.len() > range.start {
unsafe { Some(self.slice_unchecked_mut(range)) } unsafe { Some(self.slice_unchecked(range)) }
} else { } else {
None None
} }
@ -233,8 +219,12 @@ where
}); });
} }
unsafe fn slice_unchecked(&self, range: Range<usize>) -> &[T] { unsafe fn slice_unchecked<'a>(
self.as_ref().get_unchecked(range) &'a self,
range: Range<usize>,
) -> BufferSlice<'a, T> {
let index = range.start;
BufferSlice::new(self.as_ref().get_unchecked(range), index)
} }
} }
@ -267,7 +257,7 @@ mod tests {
fn buffer() { fn buffer() {
let mut buffer = vec![0; 1024]; let mut buffer = vec![0; 1024];
let commit = { let commit = {
let mut slice = buffer.slice_mut(256..512).unwrap(); let mut slice = buffer.slice(256..512).unwrap();
slice.iter_mut().for_each(|x| *x = 1); slice.iter_mut().for_each(|x| *x = 1);
slice.commit() slice.commit()
}; };