|
36 | 36 | //! implementation changes (using a special thread-local heap, for example).
|
37 | 37 | //! Moreover, a switch to, e.g. `P<'a, T>` would be easy and mostly automated.
|
38 | 38 |
|
39 |
| -use std::fmt::{self, Display, Debug}; |
40 |
| -use std::hash::{Hash, Hasher}; |
| 39 | +use std::fmt; |
| 40 | +use std::iter::FromIterator; |
41 | 41 | use std::ops::Deref;
|
42 | 42 | use std::ptr;
|
43 |
| - |
| 43 | +use std::slice; |
| 44 | +use std::vec; |
44 | 45 | use serialize::{Encodable, Decodable, Encoder, Decoder};
|
45 | 46 |
|
46 | 47 | /// An owned smart pointer.
|
47 |
| -pub struct P<T> { |
| 48 | +#[derive(PartialEq, Eq, Hash)] |
| 49 | +pub struct P<T: ?Sized> { |
48 | 50 | ptr: Box<T>
|
49 | 51 | }
|
50 | 52 |
|
| 53 | +// ---------------------------------------------------------------------------- |
| 54 | +// Common impls |
| 55 | + |
| 56 | +impl<T: ?Sized> Deref for P<T> { |
| 57 | + type Target = T; |
| 58 | + |
| 59 | + fn deref(&self) -> &T { |
| 60 | + &self.ptr |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<T: ?Sized + fmt::Debug> fmt::Debug for P<T> { |
| 65 | + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 66 | + fmt::Debug::fmt(&self.ptr, fmt) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// ---------------------------------------------------------------------------- |
| 71 | +// Impls for one boxed element `P<T>` |
| 72 | + |
51 | 73 | #[allow(non_snake_case)]
|
52 | 74 | /// Construct a `P<T>` from a `T` value.
|
53 | 75 | pub fn P<T: 'static>(value: T) -> P<T> {
|
@@ -78,60 +100,138 @@ impl<T: 'static> P<T> {
|
78 | 100 | }
|
79 | 101 | }
|
80 | 102 |
|
81 |
| -impl<T> Deref for P<T> { |
82 |
| - type Target = T; |
| 103 | +impl<T: 'static + Clone> Clone for P<T> { |
| 104 | + fn clone(&self) -> P<T> { |
| 105 | + P((**self).clone()) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl<T: fmt::Display> fmt::Display for P<T> { |
| 110 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 111 | + fmt::Display::fmt(&**self, f) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl<T> fmt::Pointer for P<T> { |
| 116 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 117 | + fmt::Pointer::fmt(&self.ptr, f) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl<T: 'static + Decodable> Decodable for P<T> { |
| 122 | + fn decode<D: Decoder>(d: &mut D) -> Result<P<T>, D::Error> { |
| 123 | + Decodable::decode(d).map(P) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl<T: Encodable> Encodable for P<T> { |
| 128 | + fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { |
| 129 | + (**self).encode(s) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// ---------------------------------------------------------------------------- |
| 134 | +// Impls for boxed array `P<[T]>` (ex-`OwnedSlice`) |
| 135 | + |
| 136 | +impl<T> P<[T]> { |
| 137 | + pub fn new() -> Self { |
| 138 | + P { ptr: Box::new([]) as Box<[T]> } |
| 139 | + } |
| 140 | + |
| 141 | + #[unstable(feature = "rustc_private", issue = "0")] |
| 142 | + #[rustc_deprecated(since = "1.6.0", reason = "use `P::new` instead")] |
| 143 | + pub fn empty() -> P<[T]> { |
| 144 | + P { ptr: Box::new([]) as Box<[T]> } |
| 145 | + } |
83 | 146 |
|
84 |
| - fn deref<'a>(&'a self) -> &'a T { |
| 147 | + #[unstable(feature = "rustc_private", issue = "0")] |
| 148 | + #[rustc_deprecated(since = "1.6.0", reason = "use `P::from` instead")] |
| 149 | + pub fn from_vec(v: Vec<T>) -> P<[T]> { |
| 150 | + P { ptr: v.into_boxed_slice() } |
| 151 | + } |
| 152 | + |
| 153 | + #[unstable(feature = "rustc_private", issue = "0")] |
| 154 | + #[rustc_deprecated(since = "1.6.0", reason = "use `P::into` instead")] |
| 155 | + pub fn into_vec(self) -> Vec<T> { |
| 156 | + self.ptr.into_vec() |
| 157 | + } |
| 158 | + |
| 159 | + #[unstable(feature = "rustc_private", issue = "0")] |
| 160 | + #[rustc_deprecated(since = "1.6.0", reason = "use `&owned_slice[..]` instead")] |
| 161 | + pub fn as_slice<'a>(&'a self) -> &'a [T] { |
85 | 162 | &*self.ptr
|
86 | 163 | }
|
| 164 | + |
| 165 | + #[unstable(feature = "rustc_private", issue = "0")] |
| 166 | + #[rustc_deprecated(since = "1.6.0", reason = "use `P::into_iter` instead")] |
| 167 | + pub fn move_iter(self) -> vec::IntoIter<T> { |
| 168 | + self.ptr.into_vec().into_iter() |
| 169 | + } |
| 170 | + |
| 171 | + #[unstable(feature = "rustc_private", issue = "0")] |
| 172 | + #[rustc_deprecated(since = "1.6.0", reason = "use `iter().map(f).collect()` instead")] |
| 173 | + pub fn map<U, F: FnMut(&T) -> U>(&self, f: F) -> P<[U]> { |
| 174 | + self.iter().map(f).collect() |
| 175 | + } |
87 | 176 | }
|
88 | 177 |
|
89 |
| -impl<T: 'static + Clone> Clone for P<T> { |
90 |
| - fn clone(&self) -> P<T> { |
91 |
| - P((**self).clone()) |
| 178 | +impl<T: Clone> Clone for P<[T]> { |
| 179 | + fn clone(&self) -> Self { |
| 180 | + P { ptr: self.ptr.clone() } |
92 | 181 | }
|
93 | 182 | }
|
94 | 183 |
|
95 |
| -impl<T: PartialEq> PartialEq for P<T> { |
96 |
| - fn eq(&self, other: &P<T>) -> bool { |
97 |
| - **self == **other |
| 184 | +impl<T> From<Vec<T>> for P<[T]> { |
| 185 | + fn from(v: Vec<T>) -> Self { |
| 186 | + P { ptr: v.into_boxed_slice() } |
98 | 187 | }
|
99 | 188 | }
|
100 | 189 |
|
101 |
| -impl<T: Eq> Eq for P<T> {} |
| 190 | +impl<T> Into<Vec<T>> for P<[T]> { |
| 191 | + fn into(self) -> Vec<T> { |
| 192 | + self.ptr.into_vec() |
| 193 | + } |
| 194 | +} |
102 | 195 |
|
103 |
| -impl<T: Debug> Debug for P<T> { |
104 |
| - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
105 |
| - Debug::fmt(&**self, f) |
| 196 | +impl<T> FromIterator<T> for P<[T]> { |
| 197 | + fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self { |
| 198 | + P::from(iter.into_iter().collect::<Vec<_>>()) |
106 | 199 | }
|
107 | 200 | }
|
108 |
| -impl<T: Display> Display for P<T> { |
109 |
| - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
110 |
| - Display::fmt(&**self, f) |
| 201 | + |
| 202 | +impl<T> IntoIterator for P<[T]> { |
| 203 | + type Item = T; |
| 204 | + type IntoIter = vec::IntoIter<T>; |
| 205 | + |
| 206 | + fn into_iter(self) -> Self::IntoIter { |
| 207 | + self.ptr.into_vec().into_iter() |
111 | 208 | }
|
112 | 209 | }
|
113 | 210 |
|
114 |
| -#[stable(feature = "rust1", since = "1.0.0")] |
115 |
| -impl<T> fmt::Pointer for P<T> { |
116 |
| - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
117 |
| - fmt::Pointer::fmt(&self.ptr, f) |
| 211 | +impl<'a, T> IntoIterator for &'a P<[T]> { |
| 212 | + type Item = &'a T; |
| 213 | + type IntoIter = slice::Iter<'a, T>; |
| 214 | + fn into_iter(self) -> Self::IntoIter { |
| 215 | + self.ptr.iter() |
118 | 216 | }
|
119 | 217 | }
|
120 | 218 |
|
121 |
| -impl<T: Hash> Hash for P<T> { |
122 |
| - fn hash<H: Hasher>(&self, state: &mut H) { |
123 |
| - (**self).hash(state); |
| 219 | +impl<'a, T> IntoIterator for &'a mut P<[T]> { |
| 220 | + type Item = &'a mut T; |
| 221 | + type IntoIter = slice::IterMut<'a, T>; |
| 222 | + fn into_iter(self) -> Self::IntoIter { |
| 223 | + self.ptr.iter_mut() |
124 | 224 | }
|
125 | 225 | }
|
126 | 226 |
|
127 |
| -impl<T: 'static + Decodable> Decodable for P<T> { |
128 |
| - fn decode<D: Decoder>(d: &mut D) -> Result<P<T>, D::Error> { |
129 |
| - Decodable::decode(d).map(P) |
| 227 | +impl<T: Encodable> Encodable for P<[T]> { |
| 228 | + fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { |
| 229 | + Encodable::encode(&self.ptr, s) |
130 | 230 | }
|
131 | 231 | }
|
132 | 232 |
|
133 |
| -impl<T: Encodable> Encodable for P<T> { |
134 |
| - fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { |
135 |
| - (**self).encode(s) |
| 233 | +impl<T: Decodable> Decodable for P<[T]> { |
| 234 | + fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> { |
| 235 | + Ok(P { ptr: try!(Decodable::decode(d)) }) |
136 | 236 | }
|
137 | 237 | }
|
0 commit comments