mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Implement slice::Vector for Option<T> and CVec<T>
This commit is contained in:
parent
bc24819bb2
commit
2467c6e5a7
@ -143,6 +143,7 @@
|
||||
|
||||
use cmp::{PartialEq, Eq, Ord};
|
||||
use default::Default;
|
||||
use slice::Vector;
|
||||
use iter::{Iterator, DoubleEndedIterator, FromIterator, ExactSize};
|
||||
use mem;
|
||||
use slice;
|
||||
@ -216,15 +217,6 @@ impl<T> Option<T> {
|
||||
match *self { Some(ref mut x) => Some(x), None => None }
|
||||
}
|
||||
|
||||
/// Convert from `Option<T>` to `&[T]` (without copying)
|
||||
#[inline]
|
||||
pub fn as_slice<'r>(&'r self) -> &'r [T] {
|
||||
match *self {
|
||||
Some(ref x) => slice::ref_slice(x),
|
||||
None => &[]
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from `Option<T>` to `&mut [T]` (without copying)
|
||||
#[inline]
|
||||
pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [T] {
|
||||
@ -526,6 +518,17 @@ impl<T: Default> Option<T> {
|
||||
// Trait implementations
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T> Vector<T> for Option<T> {
|
||||
/// Convert from `Option<T>` to `&[T]` (without copying)
|
||||
#[inline]
|
||||
fn as_slice<'a>(&'a self) -> &'a [T] {
|
||||
match *self {
|
||||
Some(ref x) => slice::ref_slice(x),
|
||||
None => &[]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Option<T> {
|
||||
#[inline]
|
||||
fn default() -> Option<T> { None }
|
||||
|
@ -43,6 +43,7 @@ use option::{Option, Some, None};
|
||||
use ptr::RawPtr;
|
||||
use ptr;
|
||||
use raw;
|
||||
use slice::Vector;
|
||||
|
||||
/// The type representing a foreign chunk of memory
|
||||
pub struct CVec<T> {
|
||||
@ -101,13 +102,6 @@ impl<T> CVec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// View the stored data as a slice.
|
||||
pub fn as_slice<'a>(&'a self) -> &'a [T] {
|
||||
unsafe {
|
||||
mem::transmute(raw::Slice { data: self.base as *const T, len: self.len })
|
||||
}
|
||||
}
|
||||
|
||||
/// View the stored data as a mutable slice.
|
||||
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
|
||||
unsafe {
|
||||
@ -151,6 +145,15 @@ impl<T> CVec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Vector<T> for CVec<T> {
|
||||
/// View the stored data as a slice.
|
||||
fn as_slice<'a>(&'a self) -> &'a [T] {
|
||||
unsafe {
|
||||
mem::transmute(raw::Slice { data: self.base as *const T, len: self.len })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Collection for CVec<T> {
|
||||
fn len(&self) -> uint { self.len }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user