2014-03-19 09:51:08 -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.
|
|
|
|
|
|
|
|
use std::default::Default;
|
2014-12-22 11:04:23 -06:00
|
|
|
use std::fmt;
|
|
|
|
use std::iter::FromIterator;
|
|
|
|
use std::ops::Deref;
|
2014-11-23 22:56:34 -06:00
|
|
|
use std::vec;
|
2014-03-19 09:51:08 -05:00
|
|
|
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
|
|
|
|
2014-11-23 22:56:34 -06:00
|
|
|
/// A non-growable owned slice. This is a separate type to allow the
|
|
|
|
/// representation to change.
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
|
2014-03-19 09:51:08 -05:00
|
|
|
pub struct OwnedSlice<T> {
|
2014-11-23 22:56:34 -06:00
|
|
|
data: Box<[T]>
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
|
2015-01-20 17:45:07 -06:00
|
|
|
impl<T:fmt::Debug> fmt::Debug for OwnedSlice<T> {
|
2014-07-11 23:50:57 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2014-11-23 22:56:34 -06:00
|
|
|
self.data.fmt(fmt)
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> OwnedSlice<T> {
|
|
|
|
pub fn empty() -> OwnedSlice<T> {
|
2014-11-23 22:56:34 -06:00
|
|
|
OwnedSlice { data: box [] }
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
2014-11-23 22:56:34 -06:00
|
|
|
pub fn from_vec(v: Vec<T>) -> OwnedSlice<T> {
|
|
|
|
OwnedSlice { data: v.into_boxed_slice() }
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
pub fn into_vec(self) -> Vec<T> {
|
2014-11-23 22:56:34 -06:00
|
|
|
self.data.into_vec()
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_slice<'a>(&'a self) -> &'a [T] {
|
2014-11-23 22:56:34 -06:00
|
|
|
&*self.data
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
|
2014-12-19 14:52:10 -06:00
|
|
|
pub fn move_iter(self) -> vec::IntoIter<T> {
|
2014-09-14 22:27:36 -05:00
|
|
|
self.into_vec().into_iter()
|
2014-09-13 11:06:01 -05:00
|
|
|
}
|
|
|
|
|
2014-12-02 13:46:01 -06:00
|
|
|
pub fn map<U, F: FnMut(&T) -> U>(&self, f: F) -> OwnedSlice<U> {
|
2014-03-19 09:51:08 -05:00
|
|
|
self.iter().map(f).collect()
|
|
|
|
}
|
2014-11-23 22:56:34 -06:00
|
|
|
}
|
2014-10-30 15:43:24 -05:00
|
|
|
|
2015-01-01 13:53:20 -06:00
|
|
|
impl<T> Deref for OwnedSlice<T> {
|
|
|
|
type Target = [T];
|
|
|
|
|
2014-11-23 22:56:34 -06:00
|
|
|
fn deref(&self) -> &[T] {
|
|
|
|
self.as_slice()
|
|
|
|
}
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Default for OwnedSlice<T> {
|
|
|
|
fn default() -> OwnedSlice<T> {
|
|
|
|
OwnedSlice::empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Clone> Clone for OwnedSlice<T> {
|
|
|
|
fn clone(&self) -> OwnedSlice<T> {
|
2015-02-01 20:53:25 -06:00
|
|
|
OwnedSlice::from_vec(self.to_vec())
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> FromIterator<T> for OwnedSlice<T> {
|
2015-01-01 21:55:09 -06:00
|
|
|
fn from_iter<I: Iterator<Item=T>>(iter: I) -> OwnedSlice<T> {
|
2014-03-19 09:51:08 -05:00
|
|
|
OwnedSlice::from_vec(iter.collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 00:24:50 -06:00
|
|
|
impl<T: Encodable> Encodable for OwnedSlice<T> {
|
|
|
|
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-02-01 20:53:25 -06:00
|
|
|
Encodable::encode(&**self, s)
|
2015-01-04 00:24:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Decodable> Decodable for OwnedSlice<T> {
|
|
|
|
fn decode<D: Decoder>(d: &mut D) -> Result<OwnedSlice<T>, D::Error> {
|
|
|
|
Ok(OwnedSlice::from_vec(match Decodable::decode(d) {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => return Err(e)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|