rust/src/libsyntax/owned_slice.rs

99 lines
2.5 KiB
Rust
Raw Normal View History

// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// 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::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<T> {
data: Box<[T]>
}
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 17:45:07 -06:00
impl<T:fmt::Debug> fmt::Debug for OwnedSlice<T> {
2014-07-11 23:50:57 -05:00
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.data.fmt(fmt)
}
}
impl<T> OwnedSlice<T> {
pub fn empty() -> OwnedSlice<T> {
OwnedSlice { data: Box::new([]) }
}
#[inline(never)]
pub fn from_vec(v: Vec<T>) -> OwnedSlice<T> {
OwnedSlice { data: v.into_boxed_slice() }
}
#[inline(never)]
pub fn into_vec(self) -> Vec<T> {
self.data.into_vec()
}
pub fn as_slice<'a>(&'a self) -> &'a [T] {
&*self.data
}
pub fn move_iter(self) -> vec::IntoIter<T> {
2014-09-14 22:27:36 -05:00
self.into_vec().into_iter()
2014-09-13 11:06:01 -05:00
}
2014-12-02 13:46:01 -06:00
pub fn map<U, F: FnMut(&T) -> U>(&self, f: F) -> OwnedSlice<U> {
self.iter().map(f).collect()
}
}
2015-01-01 13:53:20 -06:00
impl<T> Deref for OwnedSlice<T> {
type Target = [T];
fn deref(&self) -> &[T] {
self.as_slice()
}
}
impl<T> Default for OwnedSlice<T> {
fn default() -> OwnedSlice<T> {
OwnedSlice::empty()
}
}
impl<T: Clone> Clone for OwnedSlice<T> {
fn clone(&self) -> OwnedSlice<T> {
OwnedSlice::from_vec(self.to_vec())
}
}
impl<T> FromIterator<T> for OwnedSlice<T> {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> OwnedSlice<T> {
OwnedSlice::from_vec(iter.into_iter().collect())
}
}
impl<T: Encodable> Encodable for OwnedSlice<T> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
Encodable::encode(&**self, s)
}
}
impl<T: Decodable> Decodable for OwnedSlice<T> {
fn decode<D: Decoder>(d: &mut D) -> Result<OwnedSlice<T>, D::Error> {
Ok(OwnedSlice::from_vec(match Decodable::decode(d) {
Ok(t) => t,
Err(e) => return Err(e)
}))
}
}