Skip to content

Commit a8ded1d

Browse files
Rymanalexcrichton
authored andcommitted
---
yaml --- r: 120220 b: refs/heads/dist-snap c: 826aeea h: refs/heads/master v: v3
1 parent 85230ad commit a8ded1d

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: 1813e5aa1a03b0596b8de7abd1af31edf5d6098f
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
9-
refs/heads/dist-snap: 0a707140cb68cbe9aaa99b820fcd4660dc1ea466
9+
refs/heads/dist-snap: 826aeea007a99192c992688fb52ebc6530f715c7
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1212
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/src/libcore/slice.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ pub trait ImmutableVector<'a, T> {
505505
fn bsearch(&self, f: |&T| -> Ordering) -> Option<uint>;
506506

507507
/**
508-
* Returns a mutable reference to the first element in this slice
508+
* Returns an immutable reference to the first element in this slice
509509
* and adjusts the slice in place so that it no longer contains
510510
* that element. O(1).
511511
*
@@ -523,7 +523,7 @@ pub trait ImmutableVector<'a, T> {
523523
fn shift_ref(&mut self) -> Option<&'a T>;
524524

525525
/**
526-
* Returns a mutable reference to the last element in this slice
526+
* Returns an immutable reference to the last element in this slice
527527
* and adjusts the slice in place so that it no longer contains
528528
* that element. O(1).
529529
*
@@ -693,18 +693,22 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
693693
}
694694

695695
fn shift_ref(&mut self) -> Option<&'a T> {
696-
if self.len() == 0 { return None; }
697696
unsafe {
698697
let s: &mut Slice<T> = transmute(self);
699-
Some(&*raw::shift_ptr(s))
698+
match raw::shift_ptr(s) {
699+
Some(p) => Some(&*p),
700+
None => None
701+
}
700702
}
701703
}
702704

703705
fn pop_ref(&mut self) -> Option<&'a T> {
704-
if self.len() == 0 { return None; }
705706
unsafe {
706707
let s: &mut Slice<T> = transmute(self);
707-
Some(&*raw::pop_ptr(s))
708+
match raw::pop_ptr(s) {
709+
Some(p) => Some(&*p),
710+
None => None
711+
}
708712
}
709713
}
710714
}
@@ -1059,22 +1063,26 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
10591063
}
10601064

10611065
fn mut_shift_ref(&mut self) -> Option<&'a mut T> {
1062-
if self.len() == 0 { return None; }
10631066
unsafe {
10641067
let s: &mut Slice<T> = transmute(self);
1065-
// FIXME #13933: this `&` -> `&mut` cast is a little
1066-
// dubious
1067-
Some(&mut *(raw::shift_ptr(s) as *mut _))
1068+
match raw::shift_ptr(s) {
1069+
// FIXME #13933: this `&` -> `&mut` cast is a little
1070+
// dubious
1071+
Some(p) => Some(&mut *(p as *mut _)),
1072+
None => None,
1073+
}
10681074
}
10691075
}
10701076

10711077
fn mut_pop_ref(&mut self) -> Option<&'a mut T> {
1072-
if self.len() == 0 { return None; }
10731078
unsafe {
10741079
let s: &mut Slice<T> = transmute(self);
1075-
// FIXME #13933: this `&` -> `&mut` cast is a little
1076-
// dubious
1077-
Some(&mut *(raw::pop_ptr(s) as *mut _))
1080+
match raw::pop_ptr(s) {
1081+
// FIXME #13933: this `&` -> `&mut` cast is a little
1082+
// dubious
1083+
Some(p) => Some(&mut *(p as *mut _)),
1084+
None => None,
1085+
}
10781086
}
10791087
}
10801088

@@ -1165,6 +1173,7 @@ pub mod raw {
11651173
use iter::Iterator;
11661174
use ptr::RawPtr;
11671175
use raw::Slice;
1176+
use option::{None, Option, Some};
11681177

11691178
/**
11701179
* Form a slice from a pointer and length (as a number of units,
@@ -1198,27 +1207,29 @@ pub mod raw {
11981207

11991208
/**
12001209
* Returns a pointer to first element in slice and adjusts
1201-
* slice so it no longer contains that element. Fails if
1202-
* slice is empty. O(1).
1210+
* slice so it no longer contains that element. Returns None
1211+
* if the slice is empty. O(1).
12031212
*/
1204-
pub unsafe fn shift_ptr<T>(slice: &mut Slice<T>) -> *T {
1205-
if slice.len == 0 { fail!("shift on empty slice"); }
1213+
#[inline]
1214+
pub unsafe fn shift_ptr<T>(slice: &mut Slice<T>) -> Option<*T> {
1215+
if slice.len == 0 { return None; }
12061216
let head: *T = slice.data;
12071217
slice.data = slice.data.offset(1);
12081218
slice.len -= 1;
1209-
head
1219+
Some(head)
12101220
}
12111221

12121222
/**
12131223
* Returns a pointer to last element in slice and adjusts
1214-
* slice so it no longer contains that element. Fails if
1215-
* slice is empty. O(1).
1224+
* slice so it no longer contains that element. Returns None
1225+
* if the slice is empty. O(1).
12161226
*/
1217-
pub unsafe fn pop_ptr<T>(slice: &mut Slice<T>) -> *T {
1218-
if slice.len == 0 { fail!("pop on empty slice"); }
1227+
#[inline]
1228+
pub unsafe fn pop_ptr<T>(slice: &mut Slice<T>) -> Option<*T> {
1229+
if slice.len == 0 { return None; }
12191230
let tail: *T = slice.data.offset((slice.len - 1) as int);
12201231
slice.len -= 1;
1221-
tail
1232+
Some(tail)
12221233
}
12231234
}
12241235

0 commit comments

Comments
 (0)