Skip to content

Commit 8042fdd

Browse files
committed
Merge OwnedSlice into ptr::P
1 parent db61fc7 commit 8042fdd

File tree

2 files changed

+137
-144
lines changed

2 files changed

+137
-144
lines changed

src/libsyntax/owned_slice.rs

+4-111
Original file line numberDiff line numberDiff line change
@@ -8,114 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::fmt;
12-
use std::iter::FromIterator;
13-
use std::ops::Deref;
14-
use std::slice;
15-
use std::vec;
16-
17-
/// A non-growable owned slice. This is a separate type to allow the
18-
/// representation to change.
19-
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
20-
pub struct OwnedSlice<T> {
21-
data: Box<[T]>
22-
}
23-
24-
impl<T> OwnedSlice<T> {
25-
pub fn new() -> OwnedSlice<T> {
26-
OwnedSlice { data: Box::new([]) }
27-
}
28-
29-
#[unstable(feature = "rustc_private", issue = "0")]
30-
#[rustc_deprecated(since = "1.6.0", reason = "use `OwnedSlice::new` instead")]
31-
pub fn empty() -> OwnedSlice<T> {
32-
OwnedSlice { data: Box::new([]) }
33-
}
34-
35-
#[unstable(feature = "rustc_private", issue = "0")]
36-
#[rustc_deprecated(since = "1.6.0", reason = "use `OwnedSlice::from` instead")]
37-
pub fn from_vec(v: Vec<T>) -> OwnedSlice<T> {
38-
OwnedSlice { data: v.into_boxed_slice() }
39-
}
40-
41-
#[unstable(feature = "rustc_private", issue = "0")]
42-
#[rustc_deprecated(since = "1.6.0", reason = "use `OwnedSlice::into` instead")]
43-
pub fn into_vec(self) -> Vec<T> {
44-
self.data.into_vec()
45-
}
46-
47-
#[unstable(feature = "rustc_private", issue = "0")]
48-
#[rustc_deprecated(since = "1.6.0", reason = "use `&owned_slice[..]` instead")]
49-
pub fn as_slice<'a>(&'a self) -> &'a [T] {
50-
&*self.data
51-
}
52-
53-
#[unstable(feature = "rustc_private", issue = "0")]
54-
#[rustc_deprecated(since = "1.6.0", reason = "use `OwnedSlice::into_iter` instead")]
55-
pub fn move_iter(self) -> vec::IntoIter<T> {
56-
self.data.into_vec().into_iter()
57-
}
58-
59-
#[unstable(feature = "rustc_private", issue = "0")]
60-
#[rustc_deprecated(since = "1.6.0", reason = "use `iter().map(f).collect()` instead")]
61-
pub fn map<U, F: FnMut(&T) -> U>(&self, f: F) -> OwnedSlice<U> {
62-
self.iter().map(f).collect()
63-
}
64-
}
65-
66-
impl<T> Deref for OwnedSlice<T> {
67-
type Target = [T];
68-
69-
fn deref(&self) -> &[T] {
70-
&self.data
71-
}
72-
}
73-
74-
impl<T> From<Vec<T>> for OwnedSlice<T> {
75-
fn from(v: Vec<T>) -> Self {
76-
OwnedSlice { data: v.into_boxed_slice() }
77-
}
78-
}
79-
80-
impl<T> Into<Vec<T>> for OwnedSlice<T> {
81-
fn into(self) -> Vec<T> {
82-
self.data.into_vec()
83-
}
84-
}
85-
86-
impl<T: fmt::Debug> fmt::Debug for OwnedSlice<T> {
87-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
88-
self.data.fmt(fmt)
89-
}
90-
}
91-
92-
impl<T> FromIterator<T> for OwnedSlice<T> {
93-
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> OwnedSlice<T> {
94-
OwnedSlice::from(iter.into_iter().collect::<Vec<_>>())
95-
}
96-
}
97-
98-
impl<T> IntoIterator for OwnedSlice<T> {
99-
type Item = T;
100-
type IntoIter = vec::IntoIter<T>;
101-
102-
fn into_iter(self) -> Self::IntoIter {
103-
self.data.into_vec().into_iter()
104-
}
105-
}
106-
107-
impl<'a, T> IntoIterator for &'a OwnedSlice<T> {
108-
type Item = &'a T;
109-
type IntoIter = slice::Iter<'a, T>;
110-
fn into_iter(self) -> Self::IntoIter {
111-
self.data.iter()
112-
}
113-
}
114-
115-
impl<'a, T> IntoIterator for &'a mut OwnedSlice<T> {
116-
type Item = &'a mut T;
117-
type IntoIter = slice::IterMut<'a, T>;
118-
fn into_iter(self) -> Self::IntoIter {
119-
self.data.iter_mut()
120-
}
121-
}
11+
/// A non-growable owned slice.
12+
#[unstable(feature = "rustc_private", issue = "0")]
13+
#[rustc_deprecated(since = "1.6.0", reason = "use `ptr::P<[T]>` instead")]
14+
pub type OwnedSlice<T> = ::ptr::P<[T]>;

src/libsyntax/ptr.rs

+133-33
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,40 @@
3636
//! implementation changes (using a special thread-local heap, for example).
3737
//! Moreover, a switch to, e.g. `P<'a, T>` would be easy and mostly automated.
3838
39-
use std::fmt::{self, Display, Debug};
40-
use std::hash::{Hash, Hasher};
39+
use std::fmt;
40+
use std::iter::FromIterator;
4141
use std::ops::Deref;
4242
use std::ptr;
43-
43+
use std::slice;
44+
use std::vec;
4445
use serialize::{Encodable, Decodable, Encoder, Decoder};
4546

4647
/// An owned smart pointer.
47-
pub struct P<T> {
48+
#[derive(PartialEq, Eq, Hash)]
49+
pub struct P<T: ?Sized> {
4850
ptr: Box<T>
4951
}
5052

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+
5173
#[allow(non_snake_case)]
5274
/// Construct a `P<T>` from a `T` value.
5375
pub fn P<T: 'static>(value: T) -> P<T> {
@@ -78,60 +100,138 @@ impl<T: 'static> P<T> {
78100
}
79101
}
80102

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+
}
83146

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] {
85162
&*self.ptr
86163
}
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+
}
87176
}
88177

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() }
92181
}
93182
}
94183

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() }
98187
}
99188
}
100189

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+
}
102195

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<_>>())
106199
}
107200
}
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()
111208
}
112209
}
113210

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()
118216
}
119217
}
120218

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()
124224
}
125225
}
126226

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)
130230
}
131231
}
132232

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)) })
136236
}
137237
}

0 commit comments

Comments
 (0)