Reinstate AsSlice impls for Option and Result

This commit is contained in:
Nick Cameron 2014-10-07 15:55:52 +13:00
parent 3b0550c3a9
commit eb2fdc8b06
2 changed files with 37 additions and 0 deletions

View File

@ -149,6 +149,7 @@ use iter::{Iterator, DoubleEndedIterator, FromIterator, ExactSize};
use mem;
use result::{Result, Ok, Err};
use slice;
use slice::AsSlice;
// Note that this is not a lang item per se, but it has a hidden dependency on
// `Iterator`, which is one. The compiler assumes that the `next` method of
@ -845,6 +846,21 @@ impl<T: Default> Option<T> {
// Trait implementations
/////////////////////////////////////////////////////////////////////////////
impl<T> AsSlice<T> for Option<T> {
/// Convert from `Option<T>` to `&[T]` (without copying)
#[inline]
#[stable]
fn as_slice<'a>(&'a self) -> &'a [T] {
match *self {
Some(ref x) => slice::ref_slice(x),
None => {
let result: &[_] = &[];
result
}
}
}
}
impl<T> Default for Option<T> {
#[inline]
fn default() -> Option<T> { None }

View File

@ -280,6 +280,7 @@ use clone::Clone;
use cmp::PartialEq;
use std::fmt::Show;
use slice;
use slice::AsSlice;
use iter::{Iterator, DoubleEndedIterator, FromIterator, ExactSize};
use option::{None, Option, Some};
@ -839,6 +840,26 @@ impl<T: Show, E> Result<T, E> {
}
}
/////////////////////////////////////////////////////////////////////////////
// Trait implementations
/////////////////////////////////////////////////////////////////////////////
impl<T, E> AsSlice<T> for Result<T, E> {
/// Convert from `Result<T, E>` to `&[T]` (without copying)
#[inline]
#[stable]
fn as_slice<'a>(&'a self) -> &'a [T] {
match *self {
Ok(ref x) => slice::ref_slice(x),
Err(_) => {
// work around lack of implicit coercion from fixed-size array to slice
let emp: &[_] = &[];
emp
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
// The Result Iterator
/////////////////////////////////////////////////////////////////////////////