Skip to content

Commit 78d4bf8

Browse files
committed
auto merge of #12253 : pcwalton/rust/more-vec-ng, r=alexcrichton
r? @brson
2 parents b5995b4 + 03b7910 commit 78d4bf8

File tree

5 files changed

+221
-9
lines changed

5 files changed

+221
-9
lines changed

src/libserialize/serialize.rs

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::hashmap::{HashMap, HashSet};
1818
use std::rc::Rc;
1919
use std::trie::{TrieMap, TrieSet};
2020
use std::vec;
21+
use std::vec_ng::Vec;
2122

2223
pub trait Encoder {
2324
// Primitive types:
@@ -435,6 +436,26 @@ impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~[T] {
435436
}
436437
}
437438

439+
impl<S:Encoder,T:Encodable<S>> Encodable<S> for Vec<T> {
440+
fn encode(&self, s: &mut S) {
441+
s.emit_seq(self.len(), |s| {
442+
for (i, e) in self.iter().enumerate() {
443+
s.emit_seq_elt(i, |s| e.encode(s))
444+
}
445+
})
446+
}
447+
}
448+
449+
impl<D:Decoder,T:Decodable<D>> Decodable<D> for Vec<T> {
450+
fn decode(d: &mut D) -> Vec<T> {
451+
d.read_seq(|d, len| {
452+
Vec::from_fn(len, |i| {
453+
d.read_seq_elt(i, |d| Decodable::decode(d))
454+
})
455+
})
456+
}
457+
}
458+
438459
impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> {
439460
fn encode(&self, s: &mut S) {
440461
s.emit_option(|s| {

src/libstd/macros.rs

+10
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,13 @@ macro_rules! local_data_key(
358358
macro_rules! try(
359359
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
360360
)
361+
362+
#[macro_export]
363+
macro_rules! vec(
364+
($($e:expr),*) => ({
365+
let mut temp = ::std::vec_ng::Vec::new();
366+
$(temp.push($e);)*
367+
temp
368+
})
369+
)
370+

src/libstd/str.rs

+13
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ use to_str::ToStr;
101101
use from_str::FromStr;
102102
use vec;
103103
use vec::{OwnedVector, OwnedCloneableVector, ImmutableVector, MutableVector};
104+
use vec_ng::Vec;
104105
use default::Default;
105106
use to_bytes::{IterBytes, Cb};
106107
use unstable::raw::Repr;
@@ -222,6 +223,18 @@ impl<'a, S: Str> StrVector for &'a [S] {
222223
}
223224
}
224225

226+
impl<'a, S: Str> StrVector for Vec<S> {
227+
#[inline]
228+
fn concat(&self) -> ~str {
229+
self.as_slice().concat()
230+
}
231+
232+
#[inline]
233+
fn connect(&self, sep: &str) -> ~str {
234+
self.as_slice().connect(sep)
235+
}
236+
}
237+
225238
/// Something that can be used to compare against a character
226239
pub trait CharEq {
227240
/// Determine if the splitter should split at the given character

src/libstd/to_bytes.rs

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use option::{None, Option, Some};
2121
use rc::Rc;
2222
use str::{Str, StrSlice};
2323
use vec::{Vector, ImmutableVector};
24+
use vec_ng::Vec;
2425

2526
pub type Cb<'a> = 'a |buf: &[u8]| -> bool;
2627

@@ -266,6 +267,13 @@ impl<A:IterBytes> IterBytes for ~[A] {
266267
}
267268
}
268269

270+
impl<A:IterBytes> IterBytes for Vec<A> {
271+
#[inline]
272+
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
273+
self.as_slice().iter_bytes(lsb0, f)
274+
}
275+
}
276+
269277
impl<'a> IterBytes for &'a str {
270278
#[inline]
271279
fn iter_bytes(&self, _lsb0: bool, f: Cb) -> bool {

src/libstd/vec_ng.rs

+169-9
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111
// Migrate documentation over from `std::vec` when it is removed.
1212
#[doc(hidden)];
1313

14-
use ops::Drop;
15-
use option::{None, Option, Some};
14+
use cast::{forget, transmute};
1615
use clone::Clone;
17-
use iter::{DoubleEndedIterator, Iterator};
18-
use num::CheckedMul;
16+
use cmp::{Eq, Ordering, TotalEq, TotalOrd};
1917
use container::Container;
18+
use iter::{DoubleEndedIterator, FromIterator, Iterator};
19+
use libc::{free, c_void};
2020
use mem::{size_of, move_val_init};
21-
use cast::{forget, transmute};
21+
use num::CheckedMul;
22+
use ops::Drop;
23+
use option::{None, Option, Some};
24+
use ptr::RawPtr;
25+
use ptr;
2226
use rt::global_heap::{malloc_raw, realloc_raw};
23-
use vec::{ImmutableVector, Items, MutableVector};
2427
use unstable::raw::Slice;
25-
use ptr;
26-
use ptr::RawPtr;
27-
use libc::{free, c_void};
28+
use vec::{ImmutableVector, Items, MutItems, MutableVector, RevItems};
2829

2930
pub struct Vec<T> {
3031
priv len: uint,
@@ -71,6 +72,55 @@ impl<T: Clone> Vec<T> {
7172
xs
7273
}
7374
}
75+
76+
#[inline]
77+
pub fn push_all(&mut self, other: &[T]) {
78+
for element in other.iter() {
79+
self.push((*element).clone())
80+
}
81+
}
82+
}
83+
84+
impl<T:Clone> Clone for Vec<T> {
85+
fn clone(&self) -> Vec<T> {
86+
let mut vector = Vec::with_capacity(self.len());
87+
for element in self.iter() {
88+
vector.push((*element).clone())
89+
}
90+
vector
91+
}
92+
}
93+
94+
impl<T> FromIterator<T> for Vec<T> {
95+
fn from_iterator<I:Iterator<T>>(iterator: &mut I) -> Vec<T> {
96+
let (lower, _) = iterator.size_hint();
97+
let mut vector = Vec::with_capacity(lower);
98+
for element in *iterator {
99+
vector.push(element)
100+
}
101+
vector
102+
}
103+
}
104+
105+
impl<T:Eq> Eq for Vec<T> {
106+
#[inline]
107+
fn eq(&self, other: &Vec<T>) -> bool {
108+
self.as_slice() == other.as_slice()
109+
}
110+
}
111+
112+
impl<T:TotalEq> TotalEq for Vec<T> {
113+
#[inline]
114+
fn equals(&self, other: &Vec<T>) -> bool {
115+
self.as_slice().equals(&other.as_slice())
116+
}
117+
}
118+
119+
impl<T:TotalOrd> TotalOrd for Vec<T> {
120+
#[inline]
121+
fn cmp(&self, other: &Vec<T>) -> Ordering {
122+
self.as_slice().cmp(&other.as_slice())
123+
}
74124
}
75125

76126
impl<T> Container for Vec<T> {
@@ -180,8 +230,117 @@ impl<T> Vec<T> {
180230
pub unsafe fn set_len(&mut self, len: uint) {
181231
self.len = len;
182232
}
233+
234+
#[inline]
235+
pub fn get<'a>(&'a self, index: uint) -> &'a T {
236+
&self.as_slice()[index]
237+
}
238+
239+
#[inline]
240+
pub fn get_mut<'a>(&'a mut self, index: uint) -> &'a mut T {
241+
&mut self.as_mut_slice()[index]
242+
}
243+
244+
#[inline]
245+
pub fn iter<'a>(&'a self) -> Items<'a,T> {
246+
self.as_slice().iter()
247+
}
248+
249+
#[inline]
250+
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a,T> {
251+
self.as_mut_slice().mut_iter()
252+
}
253+
254+
#[inline]
255+
pub fn sort_by(&mut self, compare: |&T, &T| -> Ordering) {
256+
self.as_mut_slice().sort_by(compare)
257+
}
258+
259+
#[inline]
260+
pub fn slice<'a>(&'a self, start: uint, end: uint) -> &'a [T] {
261+
self.as_slice().slice(start, end)
262+
}
263+
264+
#[inline]
265+
pub fn tail<'a>(&'a self) -> &'a [T] {
266+
self.as_slice().tail()
267+
}
268+
269+
#[inline]
270+
pub fn last<'a>(&'a self) -> Option<&'a T> {
271+
self.as_slice().last()
272+
}
273+
274+
#[inline]
275+
pub fn mut_last<'a>(&'a mut self) -> Option<&'a mut T> {
276+
self.as_mut_slice().mut_last()
277+
}
278+
279+
#[inline]
280+
pub fn swap_remove(&mut self, index: uint) -> T {
281+
let length = self.len();
282+
if index >= length {
283+
fail!("Vec::swap_remove - index {} >= length {}", index, length);
284+
}
285+
if index < length - 1 {
286+
self.as_mut_slice().swap(index, length - 1);
287+
}
288+
self.pop().unwrap()
289+
}
290+
291+
#[inline]
292+
pub fn unshift(&mut self, element: T) {
293+
self.insert(0, element)
294+
}
295+
296+
pub fn insert(&mut self, index: uint, element: T) {
297+
let len = self.len();
298+
assert!(index <= len);
299+
// space for the new element
300+
self.reserve_exact(len + 1);
301+
302+
unsafe { // infallible
303+
// The spot to put the new value
304+
{
305+
let slice = self.as_mut_slice();
306+
let p = slice.as_mut_ptr().offset(index as int);
307+
// Shift everything over to make space. (Duplicating the
308+
// `index`th element into two consecutive places.)
309+
ptr::copy_memory(p.offset(1), &*p, len - index);
310+
// Write it in, overwriting the first copy of the `index`th
311+
// element.
312+
move_val_init(&mut *p, element);
313+
}
314+
self.set_len(len + 1);
315+
}
316+
}
317+
318+
#[inline]
319+
pub fn rev_iter<'a>(&'a self) -> RevItems<'a,T> {
320+
self.as_slice().rev_iter()
321+
}
322+
323+
#[inline]
324+
pub fn map<U>(&self, f: |t: &T| -> U) -> Vec<U> {
325+
self.iter().map(f).collect()
326+
}
327+
328+
pub fn push_all_move(&mut self, other: Vec<T>) {
329+
for element in other.move_iter() {
330+
self.push(element)
331+
}
332+
}
333+
334+
pub fn slice_from<'a>(&'a self, start: uint) -> &'a [T] {
335+
self.as_slice().slice_from(start)
336+
}
183337
}
184338

339+
#[inline]
340+
pub fn append<T:Clone>(mut first: Vec<T>, second: &[T]) -> Vec<T> {
341+
first.push_all(second);
342+
first
343+
}
185344

186345
#[unsafe_destructor]
187346
impl<T> Drop for Vec<T> {
@@ -233,3 +392,4 @@ impl<T> Drop for MoveItems<T> {
233392
}
234393
}
235394
}
395+

0 commit comments

Comments
 (0)