Skip to content

Commit fb72c47

Browse files
committed
auto merge of #15611 : brson/rust/pushpop, r=alexcrichton
This fixes naming conventions for `push`/`pop` from either end of a structure by partially implementing @erickt's suggestion from #10852 (comment), namely: * push/pop from the 'back' are called `push` and `pop`. * push/pop from the 'front' are called `push_front` and `pop_front`. * `push`/`pop` are declared on the `MutableSeq` trait. * Implement `MutableSeq` for `Vec`, `DList`, and `RingBuf`. * Add `MutableSeq` to the prelude. I did not make any further refactorings because there is some more extensive thought that needs to be put into the collections traits. This is an easy first step that should close #10852. I left the `push_back` and `pop_back` methods on `DList` and `RingBuf` deprecated. Because `MutableSeq` is in the prelude it shouldn't break many, but it is a breaking change.
2 parents b3a732a + 71a75cc commit fb72c47

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+212
-237
lines changed

src/compiletest/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ fn make_run_args(config: &Config, props: &TestProps, testfile: &Path) ->
11781178
// Add the arguments in the run_flags directive
11791179
args.push_all_move(split_maybe_args(&props.run_flags));
11801180

1181-
let prog = args.shift().unwrap();
1181+
let prog = args.remove(0).unwrap();
11821182
return ProcArgs {
11831183
prog: prog,
11841184
args: args,

src/etc/vim/syntax/rust.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ syn keyword rustTrait Clone
100100
syn keyword rustTrait PartialEq PartialOrd Eq Ord Equiv
101101
syn keyword rustEnum Ordering
102102
syn keyword rustEnumVariant Less Equal Greater
103-
syn keyword rustTrait Collection Mutable Map MutableMap
103+
syn keyword rustTrait Collection Mutable Map MutableMap MutableSeq
104104
syn keyword rustTrait Set MutableSet
105105
syn keyword rustTrait FromIterator Extendable ExactSize
106106
syn keyword rustTrait Iterator DoubleEndedIterator

src/liballoc/arc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ impl<T: Share + Send> Drop for Weak<T> {
268268
#[allow(experimental)]
269269
mod tests {
270270
use std::clone::Clone;
271+
use std::collections::MutableSeq;
271272
use std::comm::channel;
272273
use std::mem::drop;
273274
use std::ops::Drop;

src/libcollections/bitv.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use core::slice;
7272
use core::uint;
7373
use std::hash;
7474

75-
use {Collection, Mutable, Set, MutableSet};
75+
use {Collection, Mutable, Set, MutableSet, MutableSeq};
7676
use vec::Vec;
7777

7878

@@ -1574,7 +1574,7 @@ mod tests {
15741574
use std::rand::Rng;
15751575
use test::Bencher;
15761576

1577-
use {Set, Mutable, MutableSet};
1577+
use {Set, Mutable, MutableSet, MutableSeq};
15781578
use bitv::{Bitv, BitvSet, from_fn, from_bytes};
15791579
use bitv;
15801580
use vec::Vec;

src/libcollections/btree.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use alloc::boxed::Box;
2424
use core::fmt;
2525
use core::fmt::Show;
2626

27-
use Collection;
27+
use {Collection, MutableSeq};
2828
use vec::Vec;
2929

3030
#[allow(missing_doc)]
@@ -782,6 +782,8 @@ mod test_btree {
782782

783783
use super::{BTree, Node, LeafElt};
784784

785+
use MutableSeq;
786+
785787
//Tests the functionality of the insert methods (which are unfinished).
786788
#[test]
787789
fn insert_test_one() {

src/libcollections/dlist.rs

+29-34
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use core::iter;
3030
use core::mem;
3131
use core::ptr;
3232

33-
use {Collection, Mutable, Deque};
33+
use {Collection, Mutable, Deque, MutableSeq};
3434

3535
/// A doubly-linked list.
3636
pub struct DList<T> {
@@ -249,18 +249,13 @@ impl<T> Deque<T> for DList<T> {
249249
fn pop_front(&mut self) -> Option<T> {
250250
self.pop_front_node().map(|box Node{value, ..}| value)
251251
}
252+
}
252253

253-
/// Add an element last in the list
254-
///
255-
/// O(1)
256-
fn push_back(&mut self, elt: T) {
254+
impl<T> MutableSeq<T> for DList<T> {
255+
fn push(&mut self, elt: T) {
257256
self.push_back_node(box Node::new(elt))
258257
}
259-
260-
/// Remove the last element and return it, or None if the list is empty
261-
///
262-
/// O(1)
263-
fn pop_back(&mut self) -> Option<T> {
258+
fn pop(&mut self) -> Option<T> {
264259
self.pop_back_node().map(|box Node{value, ..}| value)
265260
}
266261
}
@@ -284,12 +279,12 @@ impl<T> DList<T> {
284279
/// # Example
285280
///
286281
/// ```rust
287-
/// use std::collections::{DList, Deque};
282+
/// use std::collections::DList;
288283
///
289284
/// let mut dl = DList::new();
290-
/// dl.push_back(1i);
291-
/// dl.push_back(2);
292-
/// dl.push_back(3);
285+
/// dl.push(1i);
286+
/// dl.push(2);
287+
/// dl.push(3);
293288
///
294289
/// dl.rotate_forward();
295290
///
@@ -311,12 +306,12 @@ impl<T> DList<T> {
311306
/// # Example
312307
///
313308
/// ```rust
314-
/// use std::collections::{DList, Deque};
309+
/// use std::collections::DList;
315310
///
316311
/// let mut dl = DList::new();
317-
/// dl.push_back(1i);
318-
/// dl.push_back(2);
319-
/// dl.push_back(3);
312+
/// dl.push(1i);
313+
/// dl.push(2);
314+
/// dl.push(3);
320315
///
321316
/// dl.rotate_backward();
322317
///
@@ -338,14 +333,14 @@ impl<T> DList<T> {
338333
/// # Example
339334
///
340335
/// ```rust
341-
/// use std::collections::{DList, Deque};
336+
/// use std::collections::DList;
342337
///
343338
/// let mut a = DList::new();
344339
/// let mut b = DList::new();
345-
/// a.push_back(1i);
346-
/// a.push_back(2);
347-
/// b.push_back(3i);
348-
/// b.push_back(4);
340+
/// a.push(1i);
341+
/// a.push(2);
342+
/// b.push(3i);
343+
/// b.push(4);
349344
///
350345
/// a.append(b);
351346
///
@@ -379,14 +374,14 @@ impl<T> DList<T> {
379374
/// # Example
380375
///
381376
/// ```rust
382-
/// use std::collections::{DList, Deque};
377+
/// use std::collections::DList;
383378
///
384379
/// let mut a = DList::new();
385380
/// let mut b = DList::new();
386-
/// a.push_back(1i);
387-
/// a.push_back(2);
388-
/// b.push_back(3i);
389-
/// b.push_back(4);
381+
/// a.push(1i);
382+
/// a.push(2);
383+
/// b.push(3i);
384+
/// b.push(4);
390385
///
391386
/// a.prepend(b);
392387
///
@@ -408,13 +403,13 @@ impl<T> DList<T> {
408403
/// # Example
409404
///
410405
/// ```rust
411-
/// use std::collections::{DList, Deque};
406+
/// use std::collections::DList;
412407
///
413408
/// let mut a: DList<int> = DList::new();
414-
/// a.push_back(2i);
415-
/// a.push_back(4);
416-
/// a.push_back(7);
417-
/// a.push_back(8);
409+
/// a.push(2i);
410+
/// a.push(4);
411+
/// a.push(7);
412+
/// a.push(8);
418413
///
419414
/// // insert 11 before the first odd number in the list
420415
/// a.insert_when(11, |&e, _| e % 2 == 1);
@@ -719,7 +714,7 @@ mod tests {
719714
use test::Bencher;
720715
use test;
721716

722-
use Deque;
717+
use {Deque, MutableSeq};
723718
use super::{DList, Node, ListInsertion};
724719
use vec::Vec;
725720

src/libcollections/enum_set.rs

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ mod test {
141141

142142
use enum_set::{EnumSet, CLike};
143143

144+
use MutableSeq;
145+
144146
#[deriving(PartialEq, Show)]
145147
#[repr(uint)]
146148
enum Foo {

src/libcollections/hash/sip.rs

+2
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ mod tests {
281281
use super::super::{Hash, Writer};
282282
use super::{SipState, hash, hash_with_keys};
283283

284+
use MutableSeq;
285+
284286
// Hash just the bytes of the slice, without length prefix
285287
struct Bytes<'a>(&'a [u8]);
286288

src/libcollections/lib.rs

+51-21
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,30 @@ pub trait MutableSet<T>: Set<T> + Mutable {
325325
fn remove(&mut self, value: &T) -> bool;
326326
}
327327

328+
pub trait MutableSeq<T>: Mutable {
329+
/// Append an element to the back of a collection.
330+
///
331+
/// # Example
332+
///
333+
/// ```rust
334+
/// let mut vec = vec!(1i, 2);
335+
/// vec.push(3);
336+
/// assert_eq!(vec, vec!(1, 2, 3));
337+
/// ```
338+
fn push(&mut self, t: T);
339+
/// Remove the last element from a collection and return it, or `None` if it is
340+
/// empty.
341+
///
342+
/// # Example
343+
///
344+
/// ```rust
345+
/// let mut vec = vec!(1i, 2, 3);
346+
/// assert_eq!(vec.pop(), Some(3));
347+
/// assert_eq!(vec, vec!(1, 2));
348+
/// ```
349+
fn pop(&mut self) -> Option<T>;
350+
}
351+
328352
/// A double-ended sequence that allows querying, insertion and deletion at both
329353
/// ends.
330354
///
@@ -336,9 +360,9 @@ pub trait MutableSet<T>: Set<T> + Mutable {
336360
/// use std::collections::{RingBuf, Deque};
337361
///
338362
/// let mut queue = RingBuf::new();
339-
/// queue.push_back(1i);
340-
/// queue.push_back(2i);
341-
/// queue.push_back(3i);
363+
/// queue.push(1i);
364+
/// queue.push(2i);
365+
/// queue.push(3i);
342366
///
343367
/// // Will print 1, 2, 3
344368
/// while !queue.is_empty() {
@@ -374,17 +398,17 @@ pub trait MutableSet<T>: Set<T> + Mutable {
374398
/// // Init deque with 1, 2, 3, 4
375399
/// deque.push_front(2i);
376400
/// deque.push_front(1i);
377-
/// deque.push_back(3i);
378-
/// deque.push_back(4i);
401+
/// deque.push(3i);
402+
/// deque.push(4i);
379403
///
380404
/// // Will print (1, 4) and (2, 3)
381405
/// while !deque.is_empty() {
382406
/// let f = deque.pop_front().unwrap();
383-
/// let b = deque.pop_back().unwrap();
407+
/// let b = deque.pop().unwrap();
384408
/// println!("{}", (f, b));
385409
/// }
386410
/// ```
387-
pub trait Deque<T> : Mutable {
411+
pub trait Deque<T> : MutableSeq<T> {
388412
/// Provide a reference to the front element, or `None` if the sequence is
389413
/// empty.
390414
///
@@ -396,8 +420,8 @@ pub trait Deque<T> : Mutable {
396420
/// let mut d = RingBuf::new();
397421
/// assert_eq!(d.front(), None);
398422
///
399-
/// d.push_back(1i);
400-
/// d.push_back(2i);
423+
/// d.push(1i);
424+
/// d.push(2i);
401425
/// assert_eq!(d.front(), Some(&1i));
402426
/// ```
403427
fn front<'a>(&'a self) -> Option<&'a T>;
@@ -413,8 +437,8 @@ pub trait Deque<T> : Mutable {
413437
/// let mut d = RingBuf::new();
414438
/// assert_eq!(d.front_mut(), None);
415439
///
416-
/// d.push_back(1i);
417-
/// d.push_back(2i);
440+
/// d.push(1i);
441+
/// d.push(2i);
418442
/// match d.front_mut() {
419443
/// Some(x) => *x = 9i,
420444
/// None => (),
@@ -434,8 +458,8 @@ pub trait Deque<T> : Mutable {
434458
/// let mut d = DList::new();
435459
/// assert_eq!(d.back(), None);
436460
///
437-
/// d.push_back(1i);
438-
/// d.push_back(2i);
461+
/// d.push(1i);
462+
/// d.push(2i);
439463
/// assert_eq!(d.back(), Some(&2i));
440464
/// ```
441465
fn back<'a>(&'a self) -> Option<&'a T>;
@@ -451,8 +475,8 @@ pub trait Deque<T> : Mutable {
451475
/// let mut d = DList::new();
452476
/// assert_eq!(d.back(), None);
453477
///
454-
/// d.push_back(1i);
455-
/// d.push_back(2i);
478+
/// d.push(1i);
479+
/// d.push(2i);
456480
/// match d.back_mut() {
457481
/// Some(x) => *x = 9i,
458482
/// None => (),
@@ -479,21 +503,22 @@ pub trait Deque<T> : Mutable {
479503
///
480504
/// # Example
481505
///
482-
/// ```
506+
/// ```ignore
483507
/// use std::collections::{DList, Deque};
484508
///
485509
/// let mut d = DList::new();
486510
/// d.push_back(1i);
487511
/// d.push_back(2i);
488512
/// assert_eq!(d.front(), Some(&1i));
489513
/// ```
490-
fn push_back(&mut self, elt: T);
514+
#[deprecated = "use the `push` method"]
515+
fn push_back(&mut self, elt: T) { self.push(elt) }
491516

492517
/// Remove the last element and return it, or `None` if the sequence is empty.
493518
///
494519
/// # Example
495520
///
496-
/// ```
521+
/// ```ignore
497522
/// use std::collections::{RingBuf, Deque};
498523
///
499524
/// let mut d = RingBuf::new();
@@ -504,7 +529,8 @@ pub trait Deque<T> : Mutable {
504529
/// assert_eq!(d.pop_back(), Some(1i));
505530
/// assert_eq!(d.pop_back(), None);
506531
/// ```
507-
fn pop_back(&mut self) -> Option<T>;
532+
#[deprecated = "use the `pop` method"]
533+
fn pop_back(&mut self) -> Option<T> { self.pop() }
508534

509535
/// Remove the first element and return it, or `None` if the sequence is empty.
510536
///
@@ -514,8 +540,8 @@ pub trait Deque<T> : Mutable {
514540
/// use std::collections::{RingBuf, Deque};
515541
///
516542
/// let mut d = RingBuf::new();
517-
/// d.push_back(1i);
518-
/// d.push_back(2i);
543+
/// d.push(1i);
544+
/// d.push(2i);
519545
///
520546
/// assert_eq!(d.pop_front(), Some(1i));
521547
/// assert_eq!(d.pop_front(), Some(2i));
@@ -535,4 +561,8 @@ mod std {
535561
pub use core::clone; // deriving(Clone)
536562
pub use core::cmp; // deriving(Eq, Ord, etc.)
537563
pub use hash; // deriving(Hash)
564+
565+
pub mod collections {
566+
pub use MutableSeq;
567+
}
538568
}

src/libcollections/priority_queue.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ use core::default::Default;
154154
use core::mem::{zeroed, replace, swap};
155155
use core::ptr;
156156

157-
use {Collection, Mutable};
157+
use {Collection, Mutable, MutableSeq};
158158
use slice;
159159
use vec::Vec;
160160

@@ -388,6 +388,7 @@ mod tests {
388388

389389
use priority_queue::PriorityQueue;
390390
use vec::Vec;
391+
use MutableSeq;
391392

392393
#[test]
393394
fn test_iterator() {

0 commit comments

Comments
 (0)