diff options
| author | Nick Cameron <ncameron@mozilla.com> | 2014-10-07 15:55:52 +1300 |
|---|---|---|
| committer | Nick Cameron <ncameron@mozilla.com> | 2014-10-07 15:55:52 +1300 |
| commit | eb2fdc8b065218476744ed428097c859616c62f0 (patch) | |
| tree | 8de0df13d8b19c5acaa234e1efe90a119a906a4e /src | |
| parent | 3b0550c3a94baa8ed4a5c25fc69a2859acb65673 (diff) | |
| download | rust-eb2fdc8b065218476744ed428097c859616c62f0.tar.gz rust-eb2fdc8b065218476744ed428097c859616c62f0.zip | |
Reinstate AsSlice impls for Option and Result
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/option.rs | 16 | ||||
| -rw-r--r-- | src/libcore/result.rs | 21 |
2 files changed, 37 insertions, 0 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index ec7d655b087..9b66f900d9c 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -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 } diff --git a/src/libcore/result.rs b/src/libcore/result.rs index f734ee8d0cb..caede952e2f 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -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}; @@ -840,6 +841,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 ///////////////////////////////////////////////////////////////////////////// |
