Skip to content

Commit 856246c

Browse files
committed
libsyntax: Merge OwnedSlice into ptr::P
1 parent ce7bc51 commit 856246c

File tree

2 files changed

+90
-115
lines changed

2 files changed

+90
-115
lines changed

src/libsyntax/owned_slice.rs

+2-97
Original file line numberDiff line numberDiff line change
@@ -8,100 +8,5 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

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-
}
11+
/// A non-growable owned slice.
12+
pub type OwnedSlice<T> = ::ptr::P<[T]>;

src/libsyntax/ptr.rs

+88-18
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@
3737
//! Moreover, a switch to, e.g. `P<'a, T>` would be easy and mostly automated.
3838
3939
use std::fmt::{self, Display, Debug};
40-
use std::hash::{Hash, Hasher};
40+
use std::iter::FromIterator;
4141
use std::ops::Deref;
42-
use std::ptr;
42+
use std::{ptr, slice, vec};
4343

4444
use serialize::{Encodable, Decodable, Encoder, Decoder};
4545

4646
/// An owned smart pointer.
47-
pub struct P<T> {
47+
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
48+
pub struct P<T: ?Sized> {
4849
ptr: Box<T>
4950
}
5051

@@ -92,14 +93,6 @@ impl<T: 'static + Clone> Clone for P<T> {
9293
}
9394
}
9495

95-
impl<T: PartialEq> PartialEq for P<T> {
96-
fn eq(&self, other: &P<T>) -> bool {
97-
**self == **other
98-
}
99-
}
100-
101-
impl<T: Eq> Eq for P<T> {}
102-
10396
impl<T: Debug> Debug for P<T> {
10497
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10598
Debug::fmt(&**self, f)
@@ -111,19 +104,12 @@ impl<T: Display> Display for P<T> {
111104
}
112105
}
113106

114-
#[stable(feature = "rust1", since = "1.0.0")]
115107
impl<T> fmt::Pointer for P<T> {
116108
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117109
fmt::Pointer::fmt(&self.ptr, f)
118110
}
119111
}
120112

121-
impl<T: Hash> Hash for P<T> {
122-
fn hash<H: Hasher>(&self, state: &mut H) {
123-
(**self).hash(state);
124-
}
125-
}
126-
127113
impl<T: 'static + Decodable> Decodable for P<T> {
128114
fn decode<D: Decoder>(d: &mut D) -> Result<P<T>, D::Error> {
129115
Decodable::decode(d).map(P)
@@ -135,3 +121,87 @@ impl<T: Encodable> Encodable for P<T> {
135121
(**self).encode(s)
136122
}
137123
}
124+
125+
126+
impl<T:fmt::Debug> fmt::Debug for P<[T]> {
127+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
128+
self.ptr.fmt(fmt)
129+
}
130+
}
131+
132+
impl<T> P<[T]> {
133+
pub fn empty() -> P<[T]> {
134+
P { ptr: Default::default() }
135+
}
136+
137+
#[inline(never)]
138+
pub fn from_vec(v: Vec<T>) -> P<[T]> {
139+
P { ptr: v.into_boxed_slice() }
140+
}
141+
142+
#[inline(never)]
143+
pub fn into_vec(self) -> Vec<T> {
144+
self.ptr.into_vec()
145+
}
146+
147+
pub fn as_slice<'a>(&'a self) -> &'a [T] {
148+
&*self.ptr
149+
}
150+
151+
pub fn move_iter(self) -> vec::IntoIter<T> {
152+
self.into_vec().into_iter()
153+
}
154+
155+
pub fn map<U, F: FnMut(&T) -> U>(&self, f: F) -> P<[U]> {
156+
self.iter().map(f).collect()
157+
}
158+
}
159+
160+
impl<T> Deref for P<[T]> {
161+
type Target = [T];
162+
163+
fn deref(&self) -> &[T] {
164+
self.as_slice()
165+
}
166+
}
167+
168+
impl<T> Default for P<[T]> {
169+
fn default() -> P<[T]> {
170+
P::empty()
171+
}
172+
}
173+
174+
impl<T: Clone> Clone for P<[T]> {
175+
fn clone(&self) -> P<[T]> {
176+
P::from_vec(self.to_vec())
177+
}
178+
}
179+
180+
impl<T> FromIterator<T> for P<[T]> {
181+
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> P<[T]> {
182+
P::from_vec(iter.into_iter().collect())
183+
}
184+
}
185+
186+
impl<'a, T> IntoIterator for &'a P<[T]> {
187+
type Item = &'a T;
188+
type IntoIter = slice::Iter<'a, T>;
189+
fn into_iter(self) -> Self::IntoIter {
190+
self.ptr.into_iter()
191+
}
192+
}
193+
194+
impl<T: Encodable> Encodable for P<[T]> {
195+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
196+
Encodable::encode(&**self, s)
197+
}
198+
}
199+
200+
impl<T: Decodable> Decodable for P<[T]> {
201+
fn decode<D: Decoder>(d: &mut D) -> Result<P<[T]>, D::Error> {
202+
Ok(P::from_vec(match Decodable::decode(d) {
203+
Ok(t) => t,
204+
Err(e) => return Err(e)
205+
}))
206+
}
207+
}

0 commit comments

Comments
 (0)