Skip to content

Remove a pile of (mainly) internal ~[] uses #13425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
std,serialize: remove some internal uses of ~[].
These are all private uses of ~[], so can easily & non-controversially
be replaced with Vec.
  • Loading branch information
huonw committed Apr 9, 2014
commit 95d919b6f2b37f2a8eef4660e4e8d60a26d8d6fd
5 changes: 2 additions & 3 deletions src/libserialize/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ pub mod writer {
// ebml writing
pub struct Encoder<'a, W> {
pub writer: &'a mut W,
size_positions: ~[uint],
size_positions: Vec<uint>,
}

fn write_sized_vuint<W: Writer>(w: &mut W, n: uint, size: uint) -> EncodeResult {
Expand Down Expand Up @@ -668,10 +668,9 @@ pub mod writer {
}

pub fn Encoder<'a, W: Writer + Seek>(w: &'a mut W) -> Encoder<'a, W> {
let size_positions: ~[uint] = ~[];
Encoder {
writer: w,
size_positions: size_positions,
size_positions: vec!(),
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/libstd/io/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use option::{Option, Some, None};
use result::{Ok, Err};
use io;
use io::{IoError, IoResult, Reader};
use slice::{OwnedVector, ImmutableVector};
use slice::{OwnedVector, ImmutableVector, Vector};
use ptr::RawPtr;

/// An iterator that reads a single byte on each iteration,
Expand Down Expand Up @@ -88,15 +88,15 @@ pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
8u => f(unsafe { transmute::<i64, [u8, ..8]>(to_le64(n as i64)) }),
_ => {

let mut bytes: ~[u8] = ~[];
let mut bytes = vec!();
let mut i = size;
let mut n = n;
while i > 0u {
bytes.push((n & 255_u64) as u8);
n >>= 8_u64;
i -= 1u;
}
f(bytes)
f(bytes.as_slice())
}
}
}
Expand Down Expand Up @@ -127,14 +127,14 @@ pub fn u64_to_be_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
4u => f(unsafe { transmute::<i32, [u8, ..4]>(to_be32(n as i32)) }),
8u => f(unsafe { transmute::<i64, [u8, ..8]>(to_be64(n as i64)) }),
_ => {
let mut bytes: ~[u8] = ~[];
let mut bytes = vec!();
let mut i = size;
while i > 0u {
let shift = ((i - 1u) * 8u) as u64;
bytes.push((n >> shift) as u8);
i -= 1u;
}
f(bytes)
f(bytes.as_slice())
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/io/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use option::{Some, None};
use result::{Ok, Err};
use rt::rtio::{IoFactory, LocalIo, RtioSignal};
use slice::{ImmutableVector, OwnedVector};
use vec::Vec;

/// Signals that can be sent and received
#[repr(int)]
Expand Down Expand Up @@ -80,7 +81,7 @@ pub enum Signum {
/// ```
pub struct Listener {
/// A map from signums to handles to keep the handles in memory
handles: ~[(Signum, ~RtioSignal:Send)],
handles: Vec<(Signum, ~RtioSignal:Send)>,
/// This is where all the handles send signums, which are received by
/// the clients from the receiver.
tx: Sender<Signum>,
Expand All @@ -99,7 +100,7 @@ impl Listener {
Listener {
tx: tx,
rx: rx,
handles: ~[],
handles: vec!(),
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/libstd/local_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use mem::replace;
use option::{None, Option, Some};
use rt::task::{Task, LocalStorage};
use slice::{ImmutableVector, MutableVector, OwnedVector};
use vec::Vec;

/**
* Indexes a task-local data slot. This pointer is used for comparison to
Expand Down Expand Up @@ -89,7 +90,7 @@ impl<T: 'static> LocalData for T {}
// n.b. If TLS is used heavily in future, this could be made more efficient with
// a proper map.
#[doc(hidden)]
pub type Map = ~[Option<(*u8, TLSValue, LoanState)>];
pub type Map = Vec<Option<(*u8, TLSValue, LoanState)>>;
type TLSValue = ~LocalData:Send;

// Gets the map from the runtime. Lazily initialises if not done so already.
Expand All @@ -106,7 +107,7 @@ unsafe fn get_local_map() -> &mut Map {
// If this is the first time we've accessed TLS, perform similar
// actions to the oldsched way of doing things.
&LocalStorage(ref mut slot) => {
*slot = Some(~[]);
*slot = Some(vec!());
match *slot {
Some(ref mut map_ptr) => { return map_ptr }
None => abort()
Expand Down Expand Up @@ -237,7 +238,7 @@ fn get_with<T:'static,
Some(i) => {
let ret;
let mut return_loan = false;
match map[i] {
match *map.get_mut(i) {
Some((_, ref data, ref mut loan)) => {
match (state, *loan) {
(_, NoLoan) => {
Expand Down Expand Up @@ -271,7 +272,7 @@ fn get_with<T:'static,
// in turn relocated the vector. Hence we do another lookup here to
// fixup the loans.
if return_loan {
match map[i] {
match *map.get_mut(i) {
Some((_, _, ref mut loan)) => { *loan = NoLoan; }
None => abort()
}
Expand Down Expand Up @@ -331,7 +332,7 @@ pub fn set<T: 'static>(key: Key<T>, data: T) {
// we're not actually sending it to other schedulers or anything.
let data: ~LocalData:Send = unsafe { cast::transmute(data) };
match insertion_position(map, keyval) {
Some(i) => { map[i] = Some((keyval, data, NoLoan)); }
Some(i) => { *map.get_mut(i) = Some((keyval, data, NoLoan)); }
None => { map.push(Some((keyval, data, NoLoan))); }
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/libstd/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use to_str::ToStr;
use slice::{Vector, OwnedVector};
use intrinsics::{Disr, Opaque, TyDesc, TyVisitor, get_tydesc, visit_tydesc};
use raw;
use vec::Vec;

macro_rules! try( ($me:expr, $e:expr) => (
match $e {
Expand Down Expand Up @@ -102,8 +103,8 @@ enum VariantState {

pub struct ReprVisitor<'a> {
ptr: *u8,
ptr_stk: ~[*u8],
var_stk: ~[VariantState],
ptr_stk: Vec<*u8>,
var_stk: Vec<VariantState>,
writer: &'a mut io::Writer,
last_err: Option<io::IoError>,
}
Expand All @@ -112,8 +113,8 @@ pub fn ReprVisitor<'a>(ptr: *u8,
writer: &'a mut io::Writer) -> ReprVisitor<'a> {
ReprVisitor {
ptr: ptr,
ptr_stk: ~[],
var_stk: ~[],
ptr_stk: vec!(),
var_stk: vec!(),
writer: writer,
last_err: None,
}
Expand Down Expand Up @@ -154,8 +155,8 @@ impl<'a> ReprVisitor<'a> {
// issues we have to recreate it here.
let u = ReprVisitor {
ptr: ptr,
ptr_stk: ~[],
var_stk: ~[],
ptr_stk: vec!(),
var_stk: vec!(),
writer: ::cast::transmute_copy(&self.writer),
last_err: None,
};
Expand Down Expand Up @@ -505,7 +506,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
_offset: uint,
inner: *TyDesc)
-> bool {
match self.var_stk[self.var_stk.len() - 1] {
match *self.var_stk.get(self.var_stk.len() - 1) {
Matched => {
if i != 0 {
try!(self, self.writer.write(", ".as_bytes()));
Expand All @@ -523,7 +524,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
_disr_val: Disr,
n_fields: uint,
_name: &str) -> bool {
match self.var_stk[self.var_stk.len() - 1] {
match *self.var_stk.get(self.var_stk.len() - 1) {
Matched => {
if n_fields > 0 {
try!(self, self.writer.write([')' as u8]));
Expand Down
7 changes: 4 additions & 3 deletions src/libstd/rt/at_exit_imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ use option::{Some, None};
use ptr::RawPtr;
use unstable::sync::Exclusive;
use slice::OwnedVector;
use vec::Vec;

type Queue = Exclusive<~[proc():Send]>;
type Queue = Exclusive<Vec<proc():Send>>;

// You'll note that these variables are *not* atomic, and this is done on
// purpose. This module is designed to have init() called *once* in a
Expand All @@ -35,7 +36,7 @@ pub fn init() {
unsafe {
rtassert!(!RUNNING);
rtassert!(QUEUE.is_null());
let state: ~Queue = ~Exclusive::new(~[]);
let state: ~Queue = ~Exclusive::new(vec!());
QUEUE = cast::transmute(state);
}
}
Expand All @@ -61,7 +62,7 @@ pub fn run() {
QUEUE = 0 as *mut Queue;
let mut vec = None;
state.with(|arr| {
vec = Some(mem::replace(arr, ~[]));
vec = Some(mem::replace(arr, vec!()));
});
vec.take_unwrap()
};
Expand Down
16 changes: 8 additions & 8 deletions src/libstd/sync/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use sync::arc::UnsafeArc;
use sync::atomics::{AtomicInt, AtomicPtr, SeqCst};
use unstable::sync::Exclusive;
use slice::{OwnedVector, ImmutableVector};
use vec::Vec;

// Once the queue is less than 1/K full, then it will be downsized. Note that
// the deque requires that this number be less than 2.
Expand Down Expand Up @@ -116,14 +117,14 @@ pub enum Stolen<T> {
/// will only use this structure when allocating a new buffer or deallocating a
/// previous one.
pub struct BufferPool<T> {
pool: Exclusive<~[~Buffer<T>]>,
pool: Exclusive<Vec<~Buffer<T>>>,
}

/// An internal buffer used by the chase-lev deque. This structure is actually
/// implemented as a circular buffer, and is used as the intermediate storage of
/// the data in the deque.
///
/// This type is implemented with *T instead of ~[T] for two reasons:
/// This type is implemented with *T instead of Vec<T> for two reasons:
///
/// 1. There is nothing safe about using this buffer. This easily allows the
/// same value to be read twice in to rust, and there is nothing to
Expand All @@ -132,7 +133,7 @@ pub struct BufferPool<T> {
/// destructors for values in this buffer (on drop) because the bounds
/// are defined by the deque it's owned by.
///
/// 2. We can certainly avoid bounds checks using *T instead of ~[T], although
/// 2. We can certainly avoid bounds checks using *T instead of Vec<T>, although
/// LLVM is probably pretty good at doing this already.
struct Buffer<T> {
storage: *T,
Expand All @@ -143,7 +144,7 @@ impl<T: Send> BufferPool<T> {
/// Allocates a new buffer pool which in turn can be used to allocate new
/// deques.
pub fn new() -> BufferPool<T> {
BufferPool { pool: Exclusive::new(~[]) }
BufferPool { pool: Exclusive::new(vec!()) }
}

/// Allocates a new work-stealing deque which will send/receiving memory to
Expand Down Expand Up @@ -494,7 +495,7 @@ mod tests {
}
}
})
}).collect::<~[Thread<()>]>();
}).collect::<Vec<Thread<()>>>();

while remaining.load(SeqCst) > 0 {
match w.pop() {
Expand Down Expand Up @@ -525,7 +526,7 @@ mod tests {
Thread::start(proc() {
stampede(w, s, 4, 10000);
})
}).collect::<~[Thread<()>]>();
}).collect::<Vec<Thread<()>>>();

for thread in threads.move_iter() {
thread.join();
Expand Down Expand Up @@ -556,7 +557,7 @@ mod tests {
}
}
})
}).collect::<~[Thread<()>]>();
}).collect::<Vec<Thread<()>>>();

let mut rng = rand::task_rng();
let mut expected = 0;
Expand Down Expand Up @@ -658,4 +659,3 @@ mod tests {
}
}
}

12 changes: 6 additions & 6 deletions src/libstd/sync/mpmc_bounded_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use num::next_power_of_two;
use option::{Option, Some, None};
use sync::arc::UnsafeArc;
use sync::atomics::{AtomicUint,Relaxed,Release,Acquire};
use slice;
use vec::Vec;

struct Node<T> {
sequence: AtomicUint,
Expand All @@ -44,7 +44,7 @@ struct Node<T> {

struct State<T> {
pad0: [u8, ..64],
buffer: ~[Node<T>],
buffer: Vec<Node<T>>,
mask: uint,
pad1: [u8, ..64],
enqueue_pos: AtomicUint,
Expand All @@ -69,7 +69,7 @@ impl<T: Send> State<T> {
} else {
capacity
};
let buffer = slice::from_fn(capacity, |i| {
let buffer = Vec::from_fn(capacity, |i| {
Node { sequence:AtomicUint::new(i), value: None }
});
State{
Expand All @@ -88,7 +88,7 @@ impl<T: Send> State<T> {
let mask = self.mask;
let mut pos = self.enqueue_pos.load(Relaxed);
loop {
let node = &mut self.buffer[pos & mask];
let node = self.buffer.get_mut(pos & mask);
let seq = node.sequence.load(Acquire);
let diff: int = seq as int - pos as int;

Expand All @@ -114,7 +114,7 @@ impl<T: Send> State<T> {
let mask = self.mask;
let mut pos = self.dequeue_pos.load(Relaxed);
loop {
let node = &mut self.buffer[pos & mask];
let node = self.buffer.get_mut(pos & mask);
let seq = node.sequence.load(Acquire);
let diff: int = seq as int - (pos + 1) as int;
if diff == 0 {
Expand Down Expand Up @@ -186,7 +186,7 @@ mod tests {
});
}

let mut completion_rxs = ~[];
let mut completion_rxs = vec![];
for _ in range(0, nthreads) {
let (tx, rx) = channel();
completion_rxs.push(rx);
Expand Down