]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/owned_slice.rs
Merge VariantData and VariantData_
[rust.git] / src / libsyntax / owned_slice.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::default::Default;
12 use std::fmt;
13 use std::iter::{IntoIterator, FromIterator};
14 use std::ops::Deref;
15 use std::slice;
16 use std::vec;
17 use serialize::{Encodable, Decodable, Encoder, Decoder};
18
19 /// A non-growable owned slice. This is a separate type to allow the
20 /// representation to change.
21 #[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
22 pub struct OwnedSlice<T> {
23     data: Box<[T]>
24 }
25
26 impl<T:fmt::Debug> fmt::Debug for OwnedSlice<T> {
27     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
28         self.data.fmt(fmt)
29     }
30 }
31
32 impl<T> OwnedSlice<T> {
33     pub fn empty() -> OwnedSlice<T> {
34         OwnedSlice  { data: Box::new([]) }
35     }
36
37     #[inline(never)]
38     pub fn from_vec(v: Vec<T>) -> OwnedSlice<T> {
39         OwnedSlice { data: v.into_boxed_slice() }
40     }
41
42     #[inline(never)]
43     pub fn into_vec(self) -> Vec<T> {
44         self.data.into_vec()
45     }
46
47     pub fn as_slice<'a>(&'a self) -> &'a [T] {
48         &*self.data
49     }
50
51     pub fn move_iter(self) -> vec::IntoIter<T> {
52         self.into_vec().into_iter()
53     }
54
55     pub fn map<U, F: FnMut(&T) -> U>(&self, f: F) -> OwnedSlice<U> {
56         self.iter().map(f).collect()
57     }
58 }
59
60 impl<T> Deref for OwnedSlice<T> {
61     type Target = [T];
62
63     fn deref(&self) -> &[T] {
64         self.as_slice()
65     }
66 }
67
68 impl<T> Default for OwnedSlice<T> {
69     fn default() -> OwnedSlice<T> {
70         OwnedSlice::empty()
71     }
72 }
73
74 impl<T: Clone> Clone for OwnedSlice<T> {
75     fn clone(&self) -> OwnedSlice<T> {
76         OwnedSlice::from_vec(self.to_vec())
77     }
78 }
79
80 impl<T> FromIterator<T> for OwnedSlice<T> {
81     fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> OwnedSlice<T> {
82         OwnedSlice::from_vec(iter.into_iter().collect())
83     }
84 }
85
86 impl<'a, T> IntoIterator for &'a OwnedSlice<T> {
87     type Item = &'a T;
88     type IntoIter = slice::Iter<'a, T>;
89     fn into_iter(self) -> Self::IntoIter {
90         self.data.into_iter()
91     }
92 }
93
94 impl<T: Encodable> Encodable for OwnedSlice<T> {
95     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
96         Encodable::encode(&**self, s)
97     }
98 }
99
100 impl<T: Decodable> Decodable for OwnedSlice<T> {
101     fn decode<D: Decoder>(d: &mut D) -> Result<OwnedSlice<T>, D::Error> {
102         Ok(OwnedSlice::from_vec(match Decodable::decode(d) {
103             Ok(t) => t,
104             Err(e) => return Err(e)
105         }))
106     }
107 }