Skip to content

Commit 29b0649

Browse files
committed
Convert vec::{push, push_all, push_all_move} to methods.
1 parent 1cb0a56 commit 29b0649

File tree

6 files changed

+82
-94
lines changed

6 files changed

+82
-94
lines changed

doc/rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2620,7 +2620,7 @@ assert!(b != "world");
26202620

26212621
The vector type constructor represents a homogeneous array of values of a given type.
26222622
A vector has a fixed size.
2623-
(Operations like `vec::push` operate solely on owned vectors.)
2623+
(Operations like `vec.push` operate solely on owned vectors.)
26242624
A vector type can be annotated with a _definite_ size,
26252625
written with a trailing asterisk and integer literal, such as `[int * 10]`.
26262626
Such a definite-sized vector type is a first-class type, since its size is known statically.

src/librustc/metadata/decoder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,7 @@ pub fn get_provided_trait_methods(intr: @ident_interner, cdata: cmd,
763763

764764
if item_method_sort(mth) != 'p' { loop; }
765765

766-
vec::push(&mut result,
767-
@get_method(intr, cdata, did.node, tcx));
766+
result.push(@get_method(intr, cdata, did.node, tcx));
768767
}
769768

770769
return result;

src/librustc/middle/resolve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4862,8 +4862,8 @@ impl Resolver {
48624862
while j != 0 {
48634863
j -= 1;
48644864
for this.value_ribs[j].bindings.each_key |&k| {
4865-
vec::push(&mut maybes, this.session.str_of(k));
4866-
vec::push(&mut values, uint::max_value);
4865+
maybes.push(this.session.str_of(k));
4866+
values.push(uint::max_value);
48674867
}
48684868
}
48694869

src/librustc/middle/typeck/coherence.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ impl CoherenceChecker {
788788
`%s` to impl",
789789
provided_method.method_info
790790
.ident.repr(self.crate_context.tcx));
791-
vec::push(all_methods, provided_method.method_info);
791+
all_methods.push(provided_method.method_info);
792792
}
793793
}
794794
}

src/libstd/os.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ pub fn set_exit_status(code: int) {
11401140
unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] {
11411141
let mut args = ~[];
11421142
for uint::range(0, argc as uint) |i| {
1143-
vec::push(&mut args, str::raw::from_c_str(*argv.offset(i)));
1143+
args.push(str::raw::from_c_str(*argv.offset(i)));
11441144
}
11451145
args
11461146
}
@@ -1186,8 +1186,7 @@ pub fn real_args() -> ~[~str] {
11861186
while *ptr.offset(len) != 0 { len += 1; }
11871187

11881188
// Push it onto the list.
1189-
vec::push(&mut args,
1190-
vec::raw::buf_as_slice(ptr, len,
1189+
args.push(vec::raw::buf_as_slice(ptr, len,
11911190
str::from_utf16));
11921191
}
11931192
}

src/libstd/vec.rs

Lines changed: 75 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -438,86 +438,6 @@ pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
438438
}
439439
}
440440

441-
/// Append an element to a vector
442-
#[inline]
443-
pub fn push<T>(v: &mut ~[T], initval: T) {
444-
unsafe {
445-
let repr: **raw::VecRepr = transmute(&mut *v);
446-
let fill = (**repr).unboxed.fill;
447-
if (**repr).unboxed.alloc > fill {
448-
push_fast(v, initval);
449-
}
450-
else {
451-
push_slow(v, initval);
452-
}
453-
}
454-
}
455-
456-
// This doesn't bother to make sure we have space.
457-
#[inline] // really pretty please
458-
unsafe fn push_fast<T>(v: &mut ~[T], initval: T) {
459-
let repr: **mut raw::VecRepr = transmute(v);
460-
let fill = (**repr).unboxed.fill;
461-
(**repr).unboxed.fill += sys::nonzero_size_of::<T>();
462-
let p = to_unsafe_ptr(&((**repr).unboxed.data));
463-
let p = ptr::offset(p, fill) as *mut T;
464-
intrinsics::move_val_init(&mut(*p), initval);
465-
}
466-
467-
#[inline(never)]
468-
fn push_slow<T>(v: &mut ~[T], initval: T) {
469-
let new_len = v.len() + 1;
470-
reserve_at_least(&mut *v, new_len);
471-
unsafe { push_fast(v, initval) }
472-
}
473-
474-
/// Iterates over the slice `rhs`, copies each element, and then appends it to
475-
/// the vector provided `v`. The `rhs` vector is traversed in-order.
476-
///
477-
/// # Example
478-
///
479-
/// ~~~ {.rust}
480-
/// let mut a = ~[1];
481-
/// vec::push_all(&mut a, [2, 3, 4]);
482-
/// assert!(a == ~[1, 2, 3, 4]);
483-
/// ~~~
484-
#[inline]
485-
pub fn push_all<T:Copy>(v: &mut ~[T], rhs: &const [T]) {
486-
let new_len = v.len() + rhs.len();
487-
reserve(&mut *v, new_len);
488-
489-
for uint::range(0u, rhs.len()) |i| {
490-
push(&mut *v, unsafe { raw::get(rhs, i) })
491-
}
492-
}
493-
494-
/// Takes ownership of the vector `rhs`, moving all elements into the specified
495-
/// vector `v`. This does not copy any elements, and it is illegal to use the
496-
/// `rhs` vector after calling this method (because it is moved here).
497-
///
498-
/// # Example
499-
///
500-
/// ~~~ {.rust}
501-
/// let mut a = ~[~1];
502-
/// vec::push_all_move(&mut a, ~[~2, ~3, ~4]);
503-
/// assert!(a == ~[~1, ~2, ~3, ~4]);
504-
/// ~~~
505-
#[inline]
506-
pub fn push_all_move<T>(v: &mut ~[T], mut rhs: ~[T]) {
507-
let new_len = v.len() + rhs.len();
508-
reserve(&mut *v, new_len);
509-
unsafe {
510-
do as_mut_buf(rhs) |p, len| {
511-
for uint::range(0, len) |i| {
512-
let x = ptr::replace_ptr(ptr::mut_offset(p, i),
513-
intrinsics::uninit());
514-
push(&mut *v, x);
515-
}
516-
}
517-
raw::set_len(&mut rhs, 0);
518-
}
519-
}
520-
521441
/// Shorten a vector, dropping excess elements.
522442
pub fn truncate<T>(v: &mut ~[T], newlen: uint) {
523443
do as_mut_buf(*v) |p, oldlen| {
@@ -1699,6 +1619,8 @@ impl<'self,T:Copy> ImmutableCopyableVector<T> for &'self [T] {
16991619
#[allow(missing_doc)]
17001620
pub trait OwnedVector<T> {
17011621
fn push(&mut self, t: T);
1622+
unsafe fn push_fast(&mut self, t: T);
1623+
17021624
fn push_all_move(&mut self, rhs: ~[T]);
17031625
fn pop(&mut self) -> T;
17041626
fn shift(&mut self) -> T;
@@ -1716,14 +1638,67 @@ pub trait OwnedVector<T> {
17161638
}
17171639

17181640
impl<T> OwnedVector<T> for ~[T] {
1641+
/// Append an element to a vector
17191642
#[inline]
17201643
fn push(&mut self, t: T) {
1721-
push(self, t);
1644+
unsafe {
1645+
let repr: **raw::VecRepr = transmute(&mut *self);
1646+
let fill = (**repr).unboxed.fill;
1647+
if (**repr).unboxed.alloc <= fill {
1648+
// need more space
1649+
reserve_no_inline(self);
1650+
}
1651+
1652+
self.push_fast(t);
1653+
}
1654+
1655+
// this peculiar function is because reserve_at_least is very
1656+
// large (because of reserve), and will be inlined, which
1657+
// makes push too large.
1658+
#[inline(never)]
1659+
fn reserve_no_inline<T>(v: &mut ~[T]) {
1660+
let new_len = v.len() + 1;
1661+
reserve_at_least(v, new_len);
1662+
}
17221663
}
17231664

1724-
#[inline]
1725-
fn push_all_move(&mut self, rhs: ~[T]) {
1726-
push_all_move(self, rhs);
1665+
// This doesn't bother to make sure we have space.
1666+
#[inline] // really pretty please
1667+
unsafe fn push_fast(&mut self, t: T) {
1668+
let repr: **mut raw::VecRepr = transmute(self);
1669+
let fill = (**repr).unboxed.fill;
1670+
(**repr).unboxed.fill += sys::nonzero_size_of::<T>();
1671+
let p = to_unsafe_ptr(&((**repr).unboxed.data));
1672+
let p = ptr::offset(p, fill) as *mut T;
1673+
intrinsics::move_val_init(&mut(*p), t);
1674+
}
1675+
1676+
/// Takes ownership of the vector `rhs`, moving all elements into
1677+
/// the current vector. This does not copy any elements, and it is
1678+
/// illegal to use the `rhs` vector after calling this method
1679+
/// (because it is moved here).
1680+
///
1681+
/// # Example
1682+
///
1683+
/// ~~~ {.rust}
1684+
/// let mut a = ~[~1];
1685+
/// a.push_all_move(~[~2, ~3, ~4]);
1686+
/// assert!(a == ~[~1, ~2, ~3, ~4]);
1687+
/// ~~~
1688+
#[inline]
1689+
fn push_all_move(&mut self, mut rhs: ~[T]) {
1690+
let new_len = self.len() + rhs.len();
1691+
reserve(self, new_len);
1692+
unsafe {
1693+
do as_mut_buf(rhs) |p, len| {
1694+
for uint::range(0, len) |i| {
1695+
let x = ptr::replace_ptr(ptr::mut_offset(p, i),
1696+
intrinsics::uninit());
1697+
self.push(x);
1698+
}
1699+
}
1700+
raw::set_len(&mut rhs, 0);
1701+
}
17271702
}
17281703

17291704
/// Remove the last element from a vector and return it
@@ -1898,9 +1873,24 @@ pub trait OwnedCopyableVector<T:Copy> {
18981873
}
18991874

19001875
impl<T:Copy> OwnedCopyableVector<T> for ~[T] {
1876+
/// Iterates over the slice `rhs`, copies each element, and then appends it to
1877+
/// the vector provided `v`. The `rhs` vector is traversed in-order.
1878+
///
1879+
/// # Example
1880+
///
1881+
/// ~~~ {.rust}
1882+
/// let mut a = ~[1];
1883+
/// a.push_all([2, 3, 4]);
1884+
/// assert!(a == ~[1, 2, 3, 4]);
1885+
/// ~~~
19011886
#[inline]
19021887
fn push_all(&mut self, rhs: &const [T]) {
1903-
push_all(self, rhs);
1888+
let new_len = self.len() + rhs.len();
1889+
reserve(self, new_len);
1890+
1891+
for uint::range(0u, rhs.len()) |i| {
1892+
self.push(unsafe { raw::get(rhs, i) })
1893+
}
19041894
}
19051895

19061896
#[inline]

0 commit comments

Comments
 (0)