2014-04-25 12:10:03 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-06-28 13:20:49 -05:00
|
|
|
use std::default::Default;
|
2014-05-01 08:17:36 -05:00
|
|
|
use std::fmt;
|
2014-04-25 12:10:03 -05:00
|
|
|
use std::iter::FromIterator;
|
2014-06-28 13:20:49 -05:00
|
|
|
use std::path::BytesContainer;
|
2014-11-02 19:04:32 -06:00
|
|
|
use std::slice::{mod, Permutations};
|
2014-04-25 12:10:03 -05:00
|
|
|
|
2014-05-01 08:17:36 -05:00
|
|
|
// Note 1: It is not clear whether the flexibility of providing both
|
|
|
|
// the `Growable` and `FixedLen` variants is sufficiently useful.
|
|
|
|
// Consider restricting to just a two variant enum.
|
2014-04-25 12:10:03 -05:00
|
|
|
|
2014-05-01 08:17:36 -05:00
|
|
|
// Note 2: Once Dynamically Sized Types (DST) lands, it might be
|
|
|
|
// reasonable to replace this with something like `enum MaybeOwned<'a,
|
2014-05-05 20:56:44 -05:00
|
|
|
// Sized? U>{ Owned(Box<U>), Borrowed(&'a U) }`; and then `U` could be
|
2014-05-01 08:17:36 -05:00
|
|
|
// instantiated with `[T]` or `str`, etc. Of course, that would imply
|
|
|
|
// removing the `Growable` variant, which relates to note 1 above.
|
|
|
|
// Alternatively, we might add `MaybeOwned` for the general case but
|
|
|
|
// keep some form of `MaybeOwnedVector` to avoid unnecessary copying
|
|
|
|
// of the contents of `Vec<T>`, since we anticipate that to be a
|
|
|
|
// frequent way to dynamically construct a vector.
|
|
|
|
|
2014-06-06 12:27:49 -05:00
|
|
|
/// MaybeOwnedVector<'a,T> abstracts over `Vec<T>`, `&'a [T]`.
|
2014-04-25 12:10:03 -05:00
|
|
|
///
|
|
|
|
/// Some clients will have a pre-allocated vector ready to hand off in
|
|
|
|
/// a slice; others will want to create the set on the fly and hand
|
2014-06-06 12:27:49 -05:00
|
|
|
/// off ownership, via `Growable`.
|
2014-08-27 20:46:52 -05:00
|
|
|
pub enum MaybeOwnedVector<'a,T:'a> {
|
|
|
|
Growable(Vec<T>),
|
|
|
|
Borrowed(&'a [T]),
|
|
|
|
}
|
|
|
|
|
2014-05-01 08:17:36 -05:00
|
|
|
/// Trait for moving into a `MaybeOwnedVector`
|
|
|
|
pub trait IntoMaybeOwnedVector<'a,T> {
|
|
|
|
/// Moves self into a `MaybeOwnedVector`
|
|
|
|
fn into_maybe_owned(self) -> MaybeOwnedVector<'a,T>;
|
|
|
|
}
|
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
impl<'a,T:'a> IntoMaybeOwnedVector<'a,T> for Vec<T> {
|
2014-05-01 08:17:36 -05:00
|
|
|
#[inline]
|
|
|
|
fn into_maybe_owned(self) -> MaybeOwnedVector<'a,T> { Growable(self) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,T> IntoMaybeOwnedVector<'a,T> for &'a [T] {
|
|
|
|
#[inline]
|
|
|
|
fn into_maybe_owned(self) -> MaybeOwnedVector<'a,T> { Borrowed(self) }
|
|
|
|
}
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
impl<'a,T> MaybeOwnedVector<'a,T> {
|
|
|
|
pub fn iter(&'a self) -> slice::Items<'a,T> {
|
|
|
|
match self {
|
2014-10-29 17:26:29 -05:00
|
|
|
&Growable(ref v) => v.as_slice().iter(),
|
2014-04-25 12:10:03 -05:00
|
|
|
&Borrowed(ref v) => v.iter(),
|
|
|
|
}
|
|
|
|
}
|
2014-10-30 15:43:24 -05:00
|
|
|
|
|
|
|
pub fn len(&self) -> uint { self.as_slice().len() }
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
2014-04-25 12:10:03 -05:00
|
|
|
}
|
|
|
|
|
2014-06-28 13:20:49 -05:00
|
|
|
impl<'a, T: PartialEq> PartialEq for MaybeOwnedVector<'a, T> {
|
|
|
|
fn eq(&self, other: &MaybeOwnedVector<T>) -> bool {
|
|
|
|
self.as_slice() == other.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Eq> Eq for MaybeOwnedVector<'a, T> {}
|
|
|
|
|
|
|
|
impl<'a, T: PartialOrd> PartialOrd for MaybeOwnedVector<'a, T> {
|
2014-10-29 20:21:37 -05:00
|
|
|
fn partial_cmp(&self, other: &MaybeOwnedVector<T>) -> Option<Ordering> {
|
|
|
|
self.as_slice().partial_cmp(other.as_slice())
|
|
|
|
}
|
2014-06-28 13:20:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Ord> Ord for MaybeOwnedVector<'a, T> {
|
2014-10-29 20:21:37 -05:00
|
|
|
fn cmp(&self, other: &MaybeOwnedVector<T>) -> Ordering {
|
|
|
|
self.as_slice().cmp(other.as_slice())
|
|
|
|
}
|
2014-06-28 13:20:49 -05:00
|
|
|
}
|
|
|
|
|
2014-10-04 18:22:42 -05:00
|
|
|
impl<'a, T: PartialEq, V: AsSlice<T>> Equiv<V> for MaybeOwnedVector<'a, T> {
|
2014-06-28 13:20:49 -05:00
|
|
|
fn equiv(&self, other: &V) -> bool {
|
|
|
|
self.as_slice() == other.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 12:10:03 -05:00
|
|
|
// The `Vector` trait is provided in the prelude and is implemented on
|
|
|
|
// both `&'a [T]` and `Vec<T>`, so it makes sense to try to support it
|
|
|
|
// seamlessly. The other vector related traits from the prelude do
|
|
|
|
// not appear to be implemented on both `&'a [T]` and `Vec<T>`. (It
|
|
|
|
// is possible that this is an oversight in some cases.)
|
|
|
|
//
|
|
|
|
// In any case, with `Vector` in place, the client can just use
|
|
|
|
// `as_slice` if they prefer that over `match`.
|
|
|
|
|
2014-10-04 18:22:42 -05:00
|
|
|
impl<'b,T> AsSlice<T> for MaybeOwnedVector<'b,T> {
|
2014-04-25 12:10:03 -05:00
|
|
|
fn as_slice<'a>(&'a self) -> &'a [T] {
|
|
|
|
match self {
|
|
|
|
&Growable(ref v) => v.as_slice(),
|
|
|
|
&Borrowed(ref v) => v.as_slice(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,T> FromIterator<T> for MaybeOwnedVector<'a,T> {
|
2014-07-17 23:44:59 -05:00
|
|
|
fn from_iter<I:Iterator<T>>(iterator: I) -> MaybeOwnedVector<'a,T> {
|
2014-05-01 08:17:36 -05:00
|
|
|
// If we are building from scratch, might as well build the
|
|
|
|
// most flexible variant.
|
2014-04-25 12:10:03 -05:00
|
|
|
Growable(FromIterator::from_iter(iterator))
|
|
|
|
}
|
|
|
|
}
|
2014-05-01 08:17:36 -05:00
|
|
|
|
|
|
|
impl<'a,T:fmt::Show> fmt::Show for MaybeOwnedVector<'a,T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.as_slice().fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-02 19:04:32 -06:00
|
|
|
impl<'a,T:Clone> CloneSliceAllocPrelude<T> for MaybeOwnedVector<'a,T> {
|
2014-05-01 08:17:36 -05:00
|
|
|
/// Returns a copy of `self`.
|
2014-07-16 15:37:28 -05:00
|
|
|
fn to_vec(&self) -> Vec<T> {
|
|
|
|
self.as_slice().to_vec()
|
2014-05-01 08:17:36 -05:00
|
|
|
}
|
2014-11-02 19:04:32 -06:00
|
|
|
|
|
|
|
fn partitioned(&self, f: |&T| -> bool) -> (Vec<T>, Vec<T>) {
|
|
|
|
self.as_slice().partitioned(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn permutations(&self) -> Permutations<T> {
|
|
|
|
self.as_slice().permutations()
|
|
|
|
}
|
2014-05-01 08:17:36 -05:00
|
|
|
}
|
|
|
|
|
2014-06-28 13:20:49 -05:00
|
|
|
impl<'a, T: Clone> Clone for MaybeOwnedVector<'a, T> {
|
|
|
|
fn clone(&self) -> MaybeOwnedVector<'a, T> {
|
|
|
|
match *self {
|
2014-10-11 01:03:55 -05:00
|
|
|
Growable(ref v) => Growable(v.clone()),
|
2014-06-28 13:20:49 -05:00
|
|
|
Borrowed(v) => Borrowed(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Default for MaybeOwnedVector<'a, T> {
|
|
|
|
fn default() -> MaybeOwnedVector<'a, T> {
|
|
|
|
Growable(Vec::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BytesContainer for MaybeOwnedVector<'a, u8> {
|
|
|
|
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
|
|
|
|
self.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 08:17:36 -05:00
|
|
|
impl<'a,T:Clone> MaybeOwnedVector<'a,T> {
|
|
|
|
/// Convert `self` into a growable `Vec`, not making a copy if possible.
|
|
|
|
pub fn into_vec(self) -> Vec<T> {
|
|
|
|
match self {
|
|
|
|
Growable(v) => v,
|
2014-09-17 14:56:31 -05:00
|
|
|
Borrowed(v) => v.to_vec(),
|
2014-05-01 08:17:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|