From 0384952a657e49d1f1cbab7c21dee91cd2c4f585 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 20 Mar 2014 01:51:08 +1100 Subject: syntax: add the OwnedSlice vector wrapper. This is a stand-in until we have a saner `~[T]` type (i.e. a proper owned slice). It's a library version of what `~[T]` will be, i.e. an owned pointer and a length. --- src/libsyntax/lib.rs | 1 + src/libsyntax/owned_slice.rs | 142 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/libsyntax/owned_slice.rs (limited to 'src/libsyntax') diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index cea531ee3b3..d0608ac2e30 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -49,6 +49,7 @@ pub mod syntax { pub use parse; } +pub mod owned_slice; pub mod opt_vec; pub mod attr; pub mod diagnostic; diff --git a/src/libsyntax/owned_slice.rs b/src/libsyntax/owned_slice.rs new file mode 100644 index 00000000000..df38945f198 --- /dev/null +++ b/src/libsyntax/owned_slice.rs @@ -0,0 +1,142 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::default::Default; +use std::hash::Hash; +use std::{cast, mem, raw, ptr, slice}; +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 { + /// null iff len == 0 + priv data: *mut T, + priv len: uint, +} + +#[unsafe_destructor] +impl Drop for OwnedSlice { + 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 OwnedSlice { + pub fn empty() -> OwnedSlice { + OwnedSlice { data: ptr::mut_null(), len: 0 } + } + + #[inline(never)] + pub fn from_vec(mut v: Vec) -> OwnedSlice { + let len = v.len(); + + if len == 0 { + OwnedSlice::empty() + } else { + let p = v.as_mut_ptr(); + // we own the allocation now + unsafe {cast::forget(v)} + + OwnedSlice { data: p, len: len } + } + } + + #[inline(never)] + pub fn into_vec(self) -> Vec { + // null is ok, because len == 0 in that case, as required by Vec. + unsafe { + let ret = Vec::from_raw_parts(self.len, self.len, self.data); + // the vector owns the allocation now + cast::forget(self); + ret + } + } + + pub fn as_slice<'a>(&'a self) -> &'a [T] { + static PTR_MARKER: u8 = 0; + let ptr = if self.data.is_null() { + // length zero, i.e. this will never be read as a T. + &PTR_MARKER as *u8 as *T + } else { + self.data as *T + }; + + let slice: &[T] = unsafe {cast::transmute(raw::Slice { + 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() + } + + pub fn map(&self, f: |&T| -> U) -> OwnedSlice { + self.iter().map(f).collect() + } +} + +impl Default for OwnedSlice { + fn default() -> OwnedSlice { + OwnedSlice::empty() + } +} + +impl Clone for OwnedSlice { + fn clone(&self) -> OwnedSlice { + OwnedSlice::from_vec(Vec::from_slice(self.as_slice())) + } +} + +impl> Hash for OwnedSlice { + fn hash(&self, state: &mut S) { + self.as_slice().hash(state) + } +} + +impl Eq for OwnedSlice { + fn eq(&self, other: &OwnedSlice) -> bool { + self.as_slice() == other.as_slice() + } +} + +impl Container for OwnedSlice { + fn len(&self) -> uint { self.len } +} + +impl FromIterator for OwnedSlice { + fn from_iterator>(iter: &mut I) -> OwnedSlice { + OwnedSlice::from_vec(iter.collect()) + } +} + +impl> Encodable for OwnedSlice { + fn encode(&self, s: &mut S) { + self.as_slice().encode(s) + } +} + +impl> Decodable for OwnedSlice { + fn decode(d: &mut D) -> OwnedSlice { + OwnedSlice::from_vec(Decodable::decode(d)) + } +} -- cgit 1.4.1-3-g733a5