From a8bcb922de0a5a462a6e8a53816f681c6e3255ba Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Sat, 6 Oct 2018 22:02:19 -0800 Subject: [PATCH 1/5] try_split_at --- src/libcore/slice/mod.rs | 92 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index a50426ba886bb..a4da5a3ff539b 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -804,6 +804,61 @@ impl [T] { (&self[..mid], &self[mid..]) } + /// Divides one slice into two at an index. + /// + /// The first will contain all indices from `[0, mid)` (excluding + /// the index `mid` itself) and the second will contain all + /// indices from `[mid, len)` (excluding the index `len` itself). + /// + /// Returns `None` if `mid > len`. + /// + /// # Examples + /// + /// ``` + /// let v = [1, 2, 3, 4, 5, 6]; + /// + /// { + /// match v.split_at(0) { + /// Some((left, right)) => { + /// assert!(left == []); + /// assert!(right == [1, 2, 3, 4, 5, 6]); + /// }, + /// None => assert!(false), + /// } + /// } + /// + /// { + /// match v.split_at(2) { + /// Some((left, right)) => { + /// assert!(left == [1, 2]); + /// assert!(right == [3, 4, 5, 6]); + /// }, + /// None => assert!(false), + /// } + /// } + /// + /// { + /// match v.split_at(6) { + /// Some((left, right)) => { + /// assert!(left == [1, 2, 3, 4, 5, 6]); + /// assert!(right == []); + /// }, + /// None => assert!(false), + /// } + /// } + /// + /// { + /// assert!(v.split_at(7).is_none()) + /// } + /// ``` + #[unstable(feature = "try_split_at", issue = "0")] + #[inline] + pub fn try_split_at(&self, mid: usize) -> Option<(&[T], &[T])> { + if mid > self.len() { None } else { + unsafe { Some((self.get_unchecked(..mid), self.get_unchecked(mid..))) } + } + } + /// Divides one mutable slice into two at an index. /// /// The first will contain all indices from `[0, mid)` (excluding @@ -842,6 +897,43 @@ impl [T] { } } + /// Divides one mutable slice into two at an index. + /// + /// The first will contain all indices from `[0, mid)` (excluding + /// the index `mid` itself) and the second will contain all + /// indices from `[mid, len)` (excluding the index `len` itself). + /// + /// Returns `None` if `mid > len`. + /// + /// # Examples + /// + /// ``` + /// let mut v = [1, 0, 3, 0, 5, 6]; + /// // scoped to restrict the lifetime of the borrows + /// { + /// match v.split_at_mut(2) { + /// Some((left, right)) => { + /// assert!(left == [1, 0]); + /// assert!(right == [3, 0, 5, 6]); + /// left[1] = 2; + /// right[1] = 4; + /// }, + /// None => assert!(false), + /// } + /// assert!(v == [1, 2, 3, 4, 5, 6]); + /// ``` + #[unstable(feature = "try_split_at", issue = "0")] + #[inline] + pub fn try_split_at_mut(&mut self, mid: usize) -> Option<(&mut [T], &mut [T])> { + let len = self.len(); + let ptr = self.as_mut_ptr(); + + if mid > self.len() { None } else { + unsafe { Some((from_raw_parts_mut(ptr, mid), + from_raw_parts_mut(ptr.add(mid), len - mid))) } + } + } + /// Returns an iterator over subslices separated by elements that match /// `pred`. The matched element is not contained in the subslices. /// From fb0a16c30cf42091f7e5255160d7697621ee5813 Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Sat, 6 Oct 2018 22:05:21 -0800 Subject: [PATCH 2/5] try_split_at issue --- src/libcore/slice/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index a4da5a3ff539b..31a1a45528633 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -851,7 +851,7 @@ impl [T] { /// assert!(v.split_at(7).is_none()) /// } /// ``` - #[unstable(feature = "try_split_at", issue = "0")] + #[unstable(feature = "try_split_at", issue = "54886")] #[inline] pub fn try_split_at(&self, mid: usize) -> Option<(&[T], &[T])> { if mid > self.len() { None } else { @@ -922,7 +922,7 @@ impl [T] { /// } /// assert!(v == [1, 2, 3, 4, 5, 6]); /// ``` - #[unstable(feature = "try_split_at", issue = "0")] + #[unstable(feature = "try_split_at", issue = "54886")] #[inline] pub fn try_split_at_mut(&mut self, mid: usize) -> Option<(&mut [T], &mut [T])> { let len = self.len(); From 0740afe2cea231083e58f94a68fadc7cf999ad29 Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Sat, 6 Oct 2018 22:08:10 -0800 Subject: [PATCH 3/5] refactorize slightly --- src/libcore/slice/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 31a1a45528633..dbe8d06461509 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -855,7 +855,7 @@ impl [T] { #[inline] pub fn try_split_at(&self, mid: usize) -> Option<(&[T], &[T])> { if mid > self.len() { None } else { - unsafe { Some((self.get_unchecked(..mid), self.get_unchecked(mid..))) } + Some(unsafe { (self.get_unchecked(..mid), self.get_unchecked(mid..)) }) } } @@ -929,8 +929,8 @@ impl [T] { let ptr = self.as_mut_ptr(); if mid > self.len() { None } else { - unsafe { Some((from_raw_parts_mut(ptr, mid), - from_raw_parts_mut(ptr.add(mid), len - mid))) } + Some(unsafe { (from_raw_parts_mut(ptr, mid), + from_raw_parts_mut(ptr.add(mid), len - mid)) }) } } From f8a51e5ae0ad2e406f30faa8cbcd932cbbe03dde Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Sun, 7 Oct 2018 12:07:02 -0800 Subject: [PATCH 4/5] redefine `try_split_at` safely --- src/libcore/slice/mod.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index dbe8d06461509..5746b44d2f614 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -854,9 +854,7 @@ impl [T] { #[unstable(feature = "try_split_at", issue = "54886")] #[inline] pub fn try_split_at(&self, mid: usize) -> Option<(&[T], &[T])> { - if mid > self.len() { None } else { - Some(unsafe { (self.get_unchecked(..mid), self.get_unchecked(mid..)) }) - } + if mid > self.len() { None } else { Some(self.split_at(mid)) } } /// Divides one mutable slice into two at an index. @@ -925,13 +923,7 @@ impl [T] { #[unstable(feature = "try_split_at", issue = "54886")] #[inline] pub fn try_split_at_mut(&mut self, mid: usize) -> Option<(&mut [T], &mut [T])> { - let len = self.len(); - let ptr = self.as_mut_ptr(); - - if mid > self.len() { None } else { - Some(unsafe { (from_raw_parts_mut(ptr, mid), - from_raw_parts_mut(ptr.add(mid), len - mid)) }) - } + if mid > self.len() { None } else { Some(self.split_at_mut(mid)) } } /// Returns an iterator over subslices separated by elements that match From b521c83f42043b3028526348327bbeac4b31218b Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Sun, 7 Oct 2018 12:08:36 -0800 Subject: [PATCH 5/5] unbreak doctests --- src/libcore/slice/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 5746b44d2f614..c239ce5491ca8 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -818,7 +818,7 @@ impl [T] { /// let v = [1, 2, 3, 4, 5, 6]; /// /// { - /// match v.split_at(0) { + /// match v.try_split_at(0) { /// Some((left, right)) => { /// assert!(left == []); /// assert!(right == [1, 2, 3, 4, 5, 6]); @@ -828,7 +828,7 @@ impl [T] { /// } /// /// { - /// match v.split_at(2) { + /// match v.try_split_at(2) { /// Some((left, right)) => { /// assert!(left == [1, 2]); /// assert!(right == [3, 4, 5, 6]); @@ -838,7 +838,7 @@ impl [T] { /// } /// /// { - /// match v.split_at(6) { + /// match v.try_split_at(6) { /// Some((left, right)) => { /// assert!(left == [1, 2, 3, 4, 5, 6]); /// assert!(right == []); @@ -873,7 +873,7 @@ impl [T] { /// let mut v = [1, 0, 3, 0, 5, 6]; /// // scoped to restrict the lifetime of the borrows /// { - /// let (left, right) = v.split_at_mut(2); + /// let (left, right) = v.try_split_at_mut(2); /// assert!(left == [1, 0]); /// assert!(right == [3, 0, 5, 6]); /// left[1] = 2;