2014-03-19 09:51:08 -05:00
|
|
|
// 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.
|
|
|
|
|
2014-07-11 23:50:57 -05:00
|
|
|
use std::fmt;
|
2014-03-19 09:51:08 -05:00
|
|
|
use std::default::Default;
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::hash;
|
2014-09-13 11:06:01 -05:00
|
|
|
use std::{mem, raw, ptr, slice, vec};
|
2014-09-15 14:37:01 -05:00
|
|
|
use std::rt::heap::EMPTY;
|
2014-03-19 09:51:08 -05:00
|
|
|
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
|
|
|
|
|
|
|
/// A non-growable owned slice. This would preferably become `~[T]`
|
|
|
|
/// under DST.
|
|
|
|
#[unsafe_no_drop_flag] // data is set to null on destruction
|
|
|
|
pub struct OwnedSlice<T> {
|
|
|
|
/// null iff len == 0
|
2014-03-27 17:39:48 -05:00
|
|
|
data: *mut T,
|
|
|
|
len: uint,
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 23:50:57 -05:00
|
|
|
impl<T:fmt::Show> fmt::Show for OwnedSlice<T> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
try!("OwnedSlice {{".fmt(fmt));
|
|
|
|
for i in self.iter() {
|
|
|
|
try!(i.fmt(fmt));
|
|
|
|
}
|
|
|
|
try!("}}".fmt(fmt));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-19 09:51:08 -05:00
|
|
|
#[unsafe_destructor]
|
|
|
|
impl<T> Drop for OwnedSlice<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.data.is_null() { return }
|
|
|
|
|
|
|
|
// extract the vector
|
|
|
|
let v = mem::replace(self, OwnedSlice::empty());
|
|
|
|
// free via the Vec destructor
|
|
|
|
v.into_vec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> OwnedSlice<T> {
|
|
|
|
pub fn empty() -> OwnedSlice<T> {
|
2014-09-14 22:27:36 -05:00
|
|
|
OwnedSlice { data: ptr::null_mut(), len: 0 }
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
pub fn from_vec(mut v: Vec<T>) -> OwnedSlice<T> {
|
|
|
|
let len = v.len();
|
|
|
|
|
|
|
|
if len == 0 {
|
|
|
|
OwnedSlice::empty()
|
|
|
|
} else {
|
2014-09-05 05:19:15 -05:00
|
|
|
// drop excess capacity to avoid breaking sized deallocation
|
|
|
|
v.shrink_to_fit();
|
|
|
|
|
2014-03-19 09:51:08 -05:00
|
|
|
let p = v.as_mut_ptr();
|
|
|
|
// we own the allocation now
|
2014-09-05 05:19:15 -05:00
|
|
|
unsafe { mem::forget(v) }
|
2014-03-19 09:51:08 -05:00
|
|
|
|
|
|
|
OwnedSlice { data: p, len: len }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
pub fn into_vec(self) -> Vec<T> {
|
|
|
|
// null is ok, because len == 0 in that case, as required by Vec.
|
|
|
|
unsafe {
|
2014-10-24 21:31:17 -05:00
|
|
|
let ret = Vec::from_raw_parts(self.data, self.len, self.len);
|
2014-03-19 09:51:08 -05:00
|
|
|
// the vector owns the allocation now
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
mem::forget(self);
|
2014-03-19 09:51:08 -05:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_slice<'a>(&'a self) -> &'a [T] {
|
|
|
|
let ptr = if self.data.is_null() {
|
|
|
|
// length zero, i.e. this will never be read as a T.
|
2014-09-15 14:37:01 -05:00
|
|
|
EMPTY as *const T
|
2014-03-19 09:51:08 -05:00
|
|
|
} else {
|
2014-06-25 14:47:34 -05:00
|
|
|
self.data as *const T
|
2014-03-19 09:51:08 -05:00
|
|
|
};
|
|
|
|
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
let slice: &[T] = unsafe {mem::transmute(raw::Slice {
|
2014-03-19 09:51:08 -05:00
|
|
|
data: ptr,
|
|
|
|
len: self.len
|
|
|
|
})};
|
|
|
|
|
|
|
|
slice
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get<'a>(&'a self, i: uint) -> &'a T {
|
|
|
|
self.as_slice().get(i).expect("OwnedSlice: index out of bounds")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter<'r>(&'r self) -> slice::Items<'r, T> {
|
|
|
|
self.as_slice().iter()
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn move_iter(self) -> vec::MoveItems<T> {
|
2014-09-14 22:27:36 -05:00
|
|
|
self.into_vec().into_iter()
|
2014-09-13 11:06:01 -05:00
|
|
|
}
|
|
|
|
|
2014-03-19 09:51:08 -05:00
|
|
|
pub fn map<U>(&self, f: |&T| -> U) -> OwnedSlice<U> {
|
|
|
|
self.iter().map(f).collect()
|
|
|
|
}
|
2014-10-30 15:43:24 -05:00
|
|
|
|
|
|
|
pub fn len(&self) -> uint { self.len }
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool { self.len == 0 }
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Default for OwnedSlice<T> {
|
|
|
|
fn default() -> OwnedSlice<T> {
|
|
|
|
OwnedSlice::empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Clone> Clone for OwnedSlice<T> {
|
|
|
|
fn clone(&self) -> OwnedSlice<T> {
|
2014-10-15 01:05:01 -05:00
|
|
|
OwnedSlice::from_vec(self.as_slice().to_vec())
|
2014-03-19 09:51:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 21:03:06 -05:00
|
|
|
impl<S: hash::Writer, T: hash::Hash<S>> hash::Hash<S> for OwnedSlice<T> {
|
2014-03-19 09:51:08 -05:00
|
|
|
fn hash(&self, state: &mut S) {
|
|
|
|
self.as_slice().hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
impl<T: PartialEq> PartialEq for OwnedSlice<T> {
|
2014-03-19 09:51:08 -05:00
|
|
|
fn eq(&self, other: &OwnedSlice<T>) -> bool {
|
|
|
|
self.as_slice() == other.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 12:43:52 -05:00
|
|
|
impl<T: Eq> Eq for OwnedSlice<T> {}
|
2014-03-22 15:30:45 -05:00
|
|
|
|
2014-03-19 09:51:08 -05:00
|
|
|
impl<T> FromIterator<T> for OwnedSlice<T> {
|
2014-03-30 23:45:55 -05:00
|
|
|
fn from_iter<I: Iterator<T>>(mut iter: I) -> OwnedSlice<T> {
|
2014-03-19 09:51:08 -05:00
|
|
|
OwnedSlice::from_vec(iter.collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 12:58:26 -05:00
|
|
|
impl<S: Encoder<E>, T: Encodable<S, E>, E> Encodable<S, E> for OwnedSlice<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), E> {
|
|
|
|
self.as_slice().encode(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D: Decoder<E>, T: Decodable<D, E>, E> Decodable<D, E> for OwnedSlice<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<OwnedSlice<T>, E> {
|
|
|
|
Ok(OwnedSlice::from_vec(match Decodable::decode(d) {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => return Err(e)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|