replace some assert's with debug_assert's; slightly more optimal Optional impls
This commit is contained in:
@@ -23,7 +23,7 @@ impl<'a, T> Index<usize> for SizedArray<&'a RuntimeArray<T>> {
|
|||||||
type Output=T;
|
type Output=T;
|
||||||
|
|
||||||
fn index(&self, idx: usize) -> &Self::Output {
|
fn index(&self, idx: usize) -> &Self::Output {
|
||||||
assert!(idx < self.len);
|
debug_assert!(idx < self.len);
|
||||||
unsafe {
|
unsafe {
|
||||||
self.items.index(idx)
|
self.items.index(idx)
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ impl<'a, T> Index<usize> for SizedArray<&'a mut RuntimeArray<T>> {
|
|||||||
type Output=T;
|
type Output=T;
|
||||||
|
|
||||||
fn index(&self, idx: usize) -> &Self::Output {
|
fn index(&self, idx: usize) -> &Self::Output {
|
||||||
assert!(idx < self.len);
|
debug_assert!(idx < self.len);
|
||||||
unsafe {
|
unsafe {
|
||||||
self.items.index(idx)
|
self.items.index(idx)
|
||||||
}
|
}
|
||||||
@@ -42,7 +42,7 @@ impl<'a, T> Index<usize> for SizedArray<&'a mut RuntimeArray<T>> {
|
|||||||
}
|
}
|
||||||
impl<'a, T> IndexMut<usize> for SizedArray<&'a mut RuntimeArray<T>> {
|
impl<'a, T> IndexMut<usize> for SizedArray<&'a mut RuntimeArray<T>> {
|
||||||
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
|
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
|
||||||
assert!(idx < self.len);
|
debug_assert!(idx < self.len);
|
||||||
unsafe {
|
unsafe {
|
||||||
self.items.index_mut(idx)
|
self.items.index_mut(idx)
|
||||||
}
|
}
|
||||||
|
@@ -43,7 +43,7 @@ impl<T> Optional<T> {
|
|||||||
|
|
||||||
pub fn and_then<U: Default, F: FnOnce(T) -> Optional<U>>(self, f: F) -> Optional<U> {
|
pub fn and_then<U: Default, F: FnOnce(T) -> Optional<U>>(self, f: F) -> Optional<U> {
|
||||||
if self.present != 0 {
|
if self.present != 0 {
|
||||||
f(self.unwrap())
|
f(self.data)
|
||||||
} else {
|
} else {
|
||||||
Optional::none()
|
Optional::none()
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ impl<T> Optional<T> {
|
|||||||
|
|
||||||
pub fn unwrap_or(self, default: T) -> T {
|
pub fn unwrap_or(self, default: T) -> T {
|
||||||
if self.present != 0 {
|
if self.present != 0 {
|
||||||
self.unwrap()
|
self.data
|
||||||
} else {
|
} else {
|
||||||
default
|
default
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ impl<T: Default> Default for Optional<T> {
|
|||||||
impl<T0: Default, T1: Default> Optional<(T0, T1)> {
|
impl<T0: Default, T1: Default> Optional<(T0, T1)> {
|
||||||
pub fn flatten((f0, f1): (Optional<T0>, Optional<T1>)) -> Self {
|
pub fn flatten((f0, f1): (Optional<T0>, Optional<T1>)) -> Self {
|
||||||
if f0.present != 0 && f1.present != 0 {
|
if f0.present != 0 && f1.present != 0 {
|
||||||
Optional::some((f0.unwrap(), f1.unwrap()))
|
Optional::some((f0.data, f1.data))
|
||||||
} else {
|
} else {
|
||||||
Optional::none()
|
Optional::none()
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,7 @@ impl<T0: Default, T1: Default> Optional<(T0, T1)> {
|
|||||||
impl<T> Into<Option<T>> for Optional<T> {
|
impl<T> Into<Option<T>> for Optional<T> {
|
||||||
fn into(self) -> Option<T> {
|
fn into(self) -> Option<T> {
|
||||||
if self.present != 0 {
|
if self.present != 0 {
|
||||||
Some(self.unwrap())
|
Some(self.data)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user