From 09d4a436a748a39f13d2d6d6c6ba56a885bb0d0c Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 16 Dec 2015 20:58:26 +0300 Subject: [PATCH] libsyntax: Merge OwnedSlice into ptr::P --- src/libsyntax/owned_slice.rs | 99 +------------------------------- src/libsyntax/ptr.rs | 106 +++++++++++++++++++++++++++++------ 2 files changed, 90 insertions(+), 115 deletions(-) diff --git a/src/libsyntax/owned_slice.rs b/src/libsyntax/owned_slice.rs index 83369689a94..820c7e7e4a6 100644 --- a/src/libsyntax/owned_slice.rs +++ b/src/libsyntax/owned_slice.rs @@ -8,100 +8,5 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::default::Default; -use std::fmt; -use std::iter::{IntoIterator, FromIterator}; -use std::ops::Deref; -use std::slice; -use std::vec; -use serialize::{Encodable, Decodable, Encoder, Decoder}; - -/// A non-growable owned slice. This is a separate type to allow the -/// representation to change. -#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct OwnedSlice { - data: Box<[T]> -} - -impl fmt::Debug for OwnedSlice { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - self.data.fmt(fmt) - } -} - -impl OwnedSlice { - pub fn empty() -> OwnedSlice { - OwnedSlice { data: Box::new([]) } - } - - #[inline(never)] - pub fn from_vec(v: Vec) -> OwnedSlice { - OwnedSlice { data: v.into_boxed_slice() } - } - - #[inline(never)] - pub fn into_vec(self) -> Vec { - self.data.into_vec() - } - - pub fn as_slice<'a>(&'a self) -> &'a [T] { - &*self.data - } - - pub fn move_iter(self) -> vec::IntoIter { - self.into_vec().into_iter() - } - - pub fn map U>(&self, f: F) -> OwnedSlice { - self.iter().map(f).collect() - } -} - -impl Deref for OwnedSlice { - type Target = [T]; - - fn deref(&self) -> &[T] { - self.as_slice() - } -} - -impl Default for OwnedSlice { - fn default() -> OwnedSlice { - OwnedSlice::empty() - } -} - -impl Clone for OwnedSlice { - fn clone(&self) -> OwnedSlice { - OwnedSlice::from_vec(self.to_vec()) - } -} - -impl FromIterator for OwnedSlice { - fn from_iter>(iter: I) -> OwnedSlice { - OwnedSlice::from_vec(iter.into_iter().collect()) - } -} - -impl<'a, T> IntoIterator for &'a OwnedSlice { - type Item = &'a T; - type IntoIter = slice::Iter<'a, T>; - fn into_iter(self) -> Self::IntoIter { - self.data.into_iter() - } -} - -impl Encodable for OwnedSlice { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - Encodable::encode(&**self, s) - } -} - -impl Decodable for OwnedSlice { - fn decode(d: &mut D) -> Result, D::Error> { - Ok(OwnedSlice::from_vec(match Decodable::decode(d) { - Ok(t) => t, - Err(e) => return Err(e) - })) - } -} +/// A non-growable owned slice. +pub type OwnedSlice = ::ptr::P<[T]>; diff --git a/src/libsyntax/ptr.rs b/src/libsyntax/ptr.rs index 83e321f110c..1be0b08086d 100644 --- a/src/libsyntax/ptr.rs +++ b/src/libsyntax/ptr.rs @@ -37,14 +37,15 @@ //! Moreover, a switch to, e.g. `P<'a, T>` would be easy and mostly automated. use std::fmt::{self, Display, Debug}; -use std::hash::{Hash, Hasher}; +use std::iter::FromIterator; use std::ops::Deref; -use std::ptr; +use std::{ptr, slice, vec}; use serialize::{Encodable, Decodable, Encoder, Decoder}; /// An owned smart pointer. -pub struct P { +#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)] +pub struct P { ptr: Box } @@ -92,14 +93,6 @@ impl Clone for P { } } -impl PartialEq for P { - fn eq(&self, other: &P) -> bool { - **self == **other - } -} - -impl Eq for P {} - impl Debug for P { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Debug::fmt(&**self, f) @@ -111,19 +104,12 @@ impl Display for P { } } -#[stable(feature = "rust1", since = "1.0.0")] impl fmt::Pointer for P { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Pointer::fmt(&self.ptr, f) } } -impl Hash for P { - fn hash(&self, state: &mut H) { - (**self).hash(state); - } -} - impl Decodable for P { fn decode(d: &mut D) -> Result, D::Error> { Decodable::decode(d).map(P) @@ -135,3 +121,87 @@ impl Encodable for P { (**self).encode(s) } } + + +impl fmt::Debug for P<[T]> { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + self.ptr.fmt(fmt) + } +} + +impl P<[T]> { + pub fn empty() -> P<[T]> { + P { ptr: Default::default() } + } + + #[inline(never)] + pub fn from_vec(v: Vec) -> P<[T]> { + P { ptr: v.into_boxed_slice() } + } + + #[inline(never)] + pub fn into_vec(self) -> Vec { + self.ptr.into_vec() + } + + pub fn as_slice<'a>(&'a self) -> &'a [T] { + &*self.ptr + } + + pub fn move_iter(self) -> vec::IntoIter { + self.into_vec().into_iter() + } + + pub fn map U>(&self, f: F) -> P<[U]> { + self.iter().map(f).collect() + } +} + +impl Deref for P<[T]> { + type Target = [T]; + + fn deref(&self) -> &[T] { + self.as_slice() + } +} + +impl Default for P<[T]> { + fn default() -> P<[T]> { + P::empty() + } +} + +impl Clone for P<[T]> { + fn clone(&self) -> P<[T]> { + P::from_vec(self.to_vec()) + } +} + +impl FromIterator for P<[T]> { + fn from_iter>(iter: I) -> P<[T]> { + P::from_vec(iter.into_iter().collect()) + } +} + +impl<'a, T> IntoIterator for &'a P<[T]> { + type Item = &'a T; + type IntoIter = slice::Iter<'a, T>; + fn into_iter(self) -> Self::IntoIter { + self.ptr.into_iter() + } +} + +impl Encodable for P<[T]> { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { + Encodable::encode(&**self, s) + } +} + +impl Decodable for P<[T]> { + fn decode(d: &mut D) -> Result, D::Error> { + Ok(P::from_vec(match Decodable::decode(d) { + Ok(t) => t, + Err(e) => return Err(e) + })) + } +}