Skip to content

Commit

Permalink
Auto merge of #23104 - japaric:inherent, r=nikomatsakis
Browse files Browse the repository at this point in the history
- Allow inherent implementations on `char`, `str`, `[T]`, `*const T`, `*mut T` and all the numeric primitives.
- copy `unicode::char::CharExt` methods into `impl char`
- remove `unicode::char::CharExt`, its re-export `std::char::CharExt` and `CharExt` from the prelude
- copy `collections::str::StrExt` methods into `impl str`
- remove `collections::str::StrExt` its re-export `std::str::StrExt`, and `StrExt` from the prelude
- copy `collections::slice::SliceExt` methods into `impl<T> [T]`
- remove `collections::slice::SliceExt` its re-export `std::slice::SliceExt`, and `SliceExt` from the prelude
- copy `core::ptr::PtrExt` methods into `impl<T> *const T`
- remove `core::ptr::PtrExt` its re-export `std::ptr::PtrExt`, and `PtrExt` from the prelude
- copy `core::ptr::PtrExt` and `core::ptr::MutPtrExt` methods into `impl<T> *mut T`
- remove `core::ptr::MutPtrExt` its re-export `std::ptr::MutPtrExt`, and `MutPtrExt` from the prelude
- copy `core::num::Int` and `core::num::SignedInt` methods into `impl i{8,16,32,64,size}`
- copy `core::num::Int` and `core::num::UnsignedInt` methods into `impl u{8,16,32,64,size}`
- remove `core::num::UnsignedInt` and its re-export `std::num::UnsignedInt`
- move `collections` tests into its own crate: `collectionstest`
- copy `core::num::Float` methods into `impl f{32,64}`

Because this PR removes several traits, this is a [breaking-change], however functionality remains unchanged and breakage due to unresolved imports should be minimal. If you encounter an error due to an unresolved import, simply remove the import:

``` diff
  fn main() {
-     use std::num::UnsignedInt;  //~ error: unresolved import `std::num::UnsignedInt`.
-
      println!("{}", 8_usize.is_power_of_two());
  }
```

---

cc  #16862
[preview docs](http://japaric.github.io/inherent/std/index.html)
[unicode::char](http://japaric.github.io/inherent/unicode/primitive.char.html)
[collections::str](http://japaric.github.io/inherent/collections/primitive.str.html)
[std::f32](http://japaric.github.io/inherent/std/primitive.f32.html)
  • Loading branch information
bors committed Mar 17, 2015
2 parents a257288 + b65ebc4 commit e466109
Show file tree
Hide file tree
Showing 90 changed files with 15,384 additions and 8,830 deletions.
1 change: 1 addition & 0 deletions mk/dist.mk
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ PKG_FILES := \
driver \
etc \
$(foreach crate,$(CRATES),lib$(crate)) \
libcollectionstest \
libcoretest \
libbacktrace \
rt \
Expand Down
5 changes: 4 additions & 1 deletion mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
DEPS_coretest :=
$(eval $(call RUST_CRATE,coretest))

TEST_TARGET_CRATES = $(filter-out core unicode,$(TARGET_CRATES)) coretest
DEPS_collectionstest :=
$(eval $(call RUST_CRATE,collectionstest))

TEST_TARGET_CRATES = $(filter-out core unicode,$(TARGET_CRATES)) collectionstest coretest
TEST_DOC_CRATES = $(DOC_CRATES)
TEST_HOST_CRATES = $(filter-out rustc_typeck rustc_borrowck rustc_resolve rustc_trans rustc_lint,\
$(HOST_CRATES))
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[cfg(stage0)]
#[cfg(not(test))]
use core::ptr::PtrExt;

Expand Down Expand Up @@ -387,7 +388,6 @@ mod imp {
mod test {
extern crate test;
use self::test::Bencher;
use core::ptr::PtrExt;
use boxed::Box;
use heap;

Expand Down
3 changes: 3 additions & 0 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ use core::nonzero::NonZero;
use core::ops::{Deref, Drop};
use core::option::Option;
use core::option::Option::{Some, None};
#[cfg(stage0)]
use core::ptr::{self, PtrExt};
#[cfg(not(stage0))]
use core::ptr;
use core::result::Result;
use core::result::Result::{Ok, Err};
use core::intrinsics::assume;
Expand Down
1 change: 1 addition & 0 deletions src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use std::intrinsics::{TyDesc, get_tydesc};
use std::intrinsics;
use std::marker;
use std::mem;
#[cfg(stage0)]
use std::num::{Int, UnsignedInt};
use std::ptr;
use std::rc::Rc;
Expand Down
215 changes: 0 additions & 215 deletions src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,218 +693,3 @@ impl<T: Ord> Extend<T> for BinaryHeap<T> {
}
}
}

#[cfg(test)]
mod tests {
use prelude::*;

use super::BinaryHeap;

#[test]
fn test_iterator() {
let data = vec![5, 9, 3];
let iterout = [9, 5, 3];
let heap = BinaryHeap::from_vec(data);
let mut i = 0;
for el in &heap {
assert_eq!(*el, iterout[i]);
i += 1;
}
}

#[test]
fn test_iterator_reverse() {
let data = vec![5, 9, 3];
let iterout = vec![3, 5, 9];
let pq = BinaryHeap::from_vec(data);

let v: Vec<_> = pq.iter().rev().cloned().collect();
assert_eq!(v, iterout);
}

#[test]
fn test_move_iter() {
let data = vec![5, 9, 3];
let iterout = vec![9, 5, 3];
let pq = BinaryHeap::from_vec(data);

let v: Vec<_> = pq.into_iter().collect();
assert_eq!(v, iterout);
}

#[test]
fn test_move_iter_size_hint() {
let data = vec![5, 9];
let pq = BinaryHeap::from_vec(data);

let mut it = pq.into_iter();

assert_eq!(it.size_hint(), (2, Some(2)));
assert_eq!(it.next(), Some(9));

assert_eq!(it.size_hint(), (1, Some(1)));
assert_eq!(it.next(), Some(5));

assert_eq!(it.size_hint(), (0, Some(0)));
assert_eq!(it.next(), None);
}

#[test]
fn test_move_iter_reverse() {
let data = vec![5, 9, 3];
let iterout = vec![3, 5, 9];
let pq = BinaryHeap::from_vec(data);

let v: Vec<_> = pq.into_iter().rev().collect();
assert_eq!(v, iterout);
}

#[test]
fn test_peek_and_pop() {
let data = vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];
let mut sorted = data.clone();
sorted.sort();
let mut heap = BinaryHeap::from_vec(data);
while !heap.is_empty() {
assert_eq!(heap.peek().unwrap(), sorted.last().unwrap());
assert_eq!(heap.pop().unwrap(), sorted.pop().unwrap());
}
}

#[test]
fn test_push() {
let mut heap = BinaryHeap::from_vec(vec![2, 4, 9]);
assert_eq!(heap.len(), 3);
assert!(*heap.peek().unwrap() == 9);
heap.push(11);
assert_eq!(heap.len(), 4);
assert!(*heap.peek().unwrap() == 11);
heap.push(5);
assert_eq!(heap.len(), 5);
assert!(*heap.peek().unwrap() == 11);
heap.push(27);
assert_eq!(heap.len(), 6);
assert!(*heap.peek().unwrap() == 27);
heap.push(3);
assert_eq!(heap.len(), 7);
assert!(*heap.peek().unwrap() == 27);
heap.push(103);
assert_eq!(heap.len(), 8);
assert!(*heap.peek().unwrap() == 103);
}

#[test]
fn test_push_unique() {
let mut heap = BinaryHeap::<Box<_>>::from_vec(vec![box 2, box 4, box 9]);
assert_eq!(heap.len(), 3);
assert!(*heap.peek().unwrap() == box 9);
heap.push(box 11);
assert_eq!(heap.len(), 4);
assert!(*heap.peek().unwrap() == box 11);
heap.push(box 5);
assert_eq!(heap.len(), 5);
assert!(*heap.peek().unwrap() == box 11);
heap.push(box 27);
assert_eq!(heap.len(), 6);
assert!(*heap.peek().unwrap() == box 27);
heap.push(box 3);
assert_eq!(heap.len(), 7);
assert!(*heap.peek().unwrap() == box 27);
heap.push(box 103);
assert_eq!(heap.len(), 8);
assert!(*heap.peek().unwrap() == box 103);
}

#[test]
fn test_push_pop() {
let mut heap = BinaryHeap::from_vec(vec![5, 5, 2, 1, 3]);
assert_eq!(heap.len(), 5);
assert_eq!(heap.push_pop(6), 6);
assert_eq!(heap.len(), 5);
assert_eq!(heap.push_pop(0), 5);
assert_eq!(heap.len(), 5);
assert_eq!(heap.push_pop(4), 5);
assert_eq!(heap.len(), 5);
assert_eq!(heap.push_pop(1), 4);
assert_eq!(heap.len(), 5);
}

#[test]
fn test_replace() {
let mut heap = BinaryHeap::from_vec(vec![5, 5, 2, 1, 3]);
assert_eq!(heap.len(), 5);
assert_eq!(heap.replace(6).unwrap(), 5);
assert_eq!(heap.len(), 5);
assert_eq!(heap.replace(0).unwrap(), 6);
assert_eq!(heap.len(), 5);
assert_eq!(heap.replace(4).unwrap(), 5);
assert_eq!(heap.len(), 5);
assert_eq!(heap.replace(1).unwrap(), 4);
assert_eq!(heap.len(), 5);
}

fn check_to_vec(mut data: Vec<i32>) {
let heap = BinaryHeap::from_vec(data.clone());
let mut v = heap.clone().into_vec();
v.sort();
data.sort();

assert_eq!(v, data);
assert_eq!(heap.into_sorted_vec(), data);
}

#[test]
fn test_to_vec() {
check_to_vec(vec![]);
check_to_vec(vec![5]);
check_to_vec(vec![3, 2]);
check_to_vec(vec![2, 3]);
check_to_vec(vec![5, 1, 2]);
check_to_vec(vec![1, 100, 2, 3]);
check_to_vec(vec![1, 3, 5, 7, 9, 2, 4, 6, 8, 0]);
check_to_vec(vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1]);
check_to_vec(vec![9, 11, 9, 9, 9, 9, 11, 2, 3, 4, 11, 9, 0, 0, 0, 0]);
check_to_vec(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
check_to_vec(vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]);
check_to_vec(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 1, 2]);
check_to_vec(vec![5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1]);
}

#[test]
fn test_empty_pop() {
let mut heap = BinaryHeap::<i32>::new();
assert!(heap.pop().is_none());
}

#[test]
fn test_empty_peek() {
let empty = BinaryHeap::<i32>::new();
assert!(empty.peek().is_none());
}

#[test]
fn test_empty_replace() {
let mut heap = BinaryHeap::new();
assert!(heap.replace(5).is_none());
}

#[test]
fn test_from_iter() {
let xs = vec![9, 8, 7, 6, 5, 4, 3, 2, 1];

let mut q: BinaryHeap<_> = xs.iter().rev().cloned().collect();

for &x in &xs {
assert_eq!(q.pop().unwrap(), x);
}
}

#[test]
fn test_drain() {
let mut q: BinaryHeap<_> = [9, 8, 7, 6, 5, 4, 3, 2, 1].iter().cloned().collect();

assert_eq!(q.drain().take(5).count(), 5);

assert!(q.is_empty());
}
}
Loading

0 comments on commit e466109

Please sign in to comment.