Reinstate AsSlice impls for Option and Result
This commit is contained in:
parent
3b0550c3a9
commit
eb2fdc8b06
|
@ -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 }
|
||||
|
|
|
@ -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
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Reference in New Issue