From 172c267917453e87d1884c9d1a70c01eb8779956 Mon Sep 17 00:00:00 2001 From: Szymon Walter Date: Mon, 19 Mar 2018 08:28:59 +0100 Subject: [PATCH] remove unmutable slices from `Buffer` --- src/buffer/mod.rs | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 54ca4ba..1aedf59 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -16,31 +16,17 @@ where { fn len(&self) -> Length; fn commit(&mut self, slice: Option>); - unsafe fn slice_unchecked(&self, range: Range) -> &[T]; - - unsafe fn slice_unchecked_mut<'a>( + unsafe fn slice_unchecked<'a>( &'a self, range: Range, - ) -> BufferSlice<'a, T> { - let index = range.start; - let slice = self.slice_unchecked(range); - BufferSlice::new(slice, index) - } + ) -> BufferSlice<'a, T>; - fn slice(&self, range: Range) -> Option<&[T]> { - if self.len() >= range.end && self.len() > range.start { - unsafe { Some(self.slice_unchecked(range)) } - } else { - None - } - } - - fn slice_mut<'a>( + fn slice<'a>( &'a mut self, range: Range, ) -> Option> { if self.len() >= range.end && self.len() > range.start { - unsafe { Some(self.slice_unchecked_mut(range)) } + unsafe { Some(self.slice_unchecked(range)) } } else { None } @@ -233,8 +219,12 @@ where }); } - unsafe fn slice_unchecked(&self, range: Range) -> &[T] { - self.as_ref().get_unchecked(range) + unsafe fn slice_unchecked<'a>( + &'a self, + range: Range, + ) -> BufferSlice<'a, T> { + let index = range.start; + BufferSlice::new(self.as_ref().get_unchecked(range), index) } } @@ -267,7 +257,7 @@ mod tests { fn buffer() { let mut buffer = vec![0; 1024]; 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.commit() };