Commit Graph

38 Commits

Author SHA1 Message Date
kennytm
bebd2fbfc8
Rollup merge of #48156 - Centril:feature/iterator_repeat_with, r=alexcrichton
Add std/core::iter::repeat_with

Adds an iterator primitive `repeat_with` which is the "lazy" version of `repeat` but also more flexible since you can build up state with the `FnMut`. The design is mostly taken from `repeat`.

r? @rust-lang/libs
cc @withoutboats, @scottmcm
2018-02-14 18:25:22 +08:00
kennytm
bd3674e4de
Rollup merge of #48087 - scottmcm:range_is_empty, r=kennytm,alexcrichton
Add Range[Inclusive]::is_empty

During https://github.com/rust-lang/rfcs/pull/1980, it was discussed that figuring out whether a range is empty was subtle, and thus there should be a clear and obvious way to do it.  It can't just be ExactSizeIterator::is_empty (also unstable) because not all ranges are ExactSize -- such as `Range<i64>` and `RangeInclusive<usize>`.

Things to ponder:
- Unless this is stabilized first, this makes stabilizing ExactSizeIterator::is_empty more icky, since this hides that.
- This is only on `Range` and `RangeInclusive`, as those are the only ones where it's interesting.  But one could argue that it should be on more for consistency, or on RangeArgument instead.
- The bound on this is PartialOrd, since that works ok (see tests for float examples) and is consistent with `contains`.  But ranges like `NAN..=NAN`_are_ kinda weird.
- [x] ~~There's not a real issue number on this yet~~
2018-02-14 16:14:33 +08:00
Mazdak Farrokhzad
f025eff21d core::iter::repeat_with: fix tests 2018-02-12 09:13:47 +01:00
Mark Simulacrum
1335b3da5a Add fetch_nand.
cc #13226 (the tracking issue)
2018-02-09 16:04:41 -07:00
Scott McMurray
4f8049a2b0 Add Range[Inclusive]::is_empty
During the RFC, it was discussed that figuring out whether a range is empty was subtle, and thus there should be a clear and obvious way to do it.  It can't just be ExactSizeIterator::is_empty (also unstable) because not all ranges are ExactSize -- not even Range<i32> or RangeInclusive<usize>.
2018-02-09 01:47:18 -08:00
Simon Sapin
55c50cd8ac Stabilize std::ptr::NonNull 2018-01-20 11:09:23 +01:00
Simon Sapin
c97c1f7dc3 Mark Unique as perma-unstable, with the feature renamed to ptr_internals. 2018-01-20 11:09:23 +01:00
Sebastian Dröge
5f4fc82142 Add unit tests for exact_chunks/exact_chunks_mut
These are basically modified copies of the chunks/chunks_mut tests.
2018-01-13 12:19:01 +02:00
Manish Goregaokar
efcc447ebf Add simple test for pattern API 2017-12-18 03:47:21 -08:00
Simon Sapin
6c5f53e65e Stabilize const-calling existing const-fns in std
Fixes #46038
2017-11-26 23:43:44 +01:00
bors
41e03c3c46 Auto merge of #45905 - alexcrichton:add-wasm-target, r=aturon
std: Add a new wasm32-unknown-unknown target

This commit adds a new target to the compiler: wasm32-unknown-unknown. This target is a reimagining of what it looks like to generate WebAssembly code from Rust. Instead of using Emscripten which can bring with it a weighty runtime this instead is a target which uses only the LLVM backend for WebAssembly and a "custom linker" for now which will hopefully one day be direct calls to lld.

Notable features of this target include:

* There is zero runtime footprint. The target assumes nothing exists other than the wasm32 instruction set.
* There is zero toolchain footprint beyond adding the target. No custom linker is needed, rustc contains everything.
* Very small wasm modules can be generated directly from Rust code using this target.
* Most of the standard library is stubbed out to return an error, but anything related to allocation works (aka `HashMap`, `Vec`, etc).
* Naturally, any `#[no_std]` crate should be 100% compatible with this new target.

This target is currently somewhat janky due to how linking works. The "linking" is currently unconditional whole program LTO (aka LLVM is being used as a linker). Naturally that means compiling programs is pretty slow! Eventually though this target should have a linker.

This target is also intended to be quite experimental. I'm hoping that this can act as a catalyst for further experimentation in Rust with WebAssembly. Breaking changes are very likely to land to this target, so it's not recommended to rely on it in any critical capacity yet. We'll let you know when it's "production ready".

### Building yourself

First you'll need to configure the build of LLVM and enable this target

```
$ ./configure --target=wasm32-unknown-unknown --set llvm.experimental-targets=WebAssembly
```

Next you'll want to remove any previously compiled LLVM as it needs to be rebuilt with WebAssembly support. You can do that with:

```
$ rm -rf build
```

And then you're good to go! A `./x.py build` should give you a rustc with the appropriate libstd target.

### Test support

Currently testing-wise this target is looking pretty good but isn't complete. I've got almost the entire `run-pass` test suite working with this target (lots of tests ignored, but many passing as well). The `core` test suite is [still getting LLVM bugs fixed](https://reviews.llvm.org/D39866) to get that working and will take some time. Relatively simple programs all seem to work though!

In general I've only tested this with a local fork that makes use of LLVM 5 rather than our current LLVM 4 on master. The LLVM 4 WebAssembly backend AFAIK isn't broken per se but is likely missing bug fixes available on LLVM 5. I'm hoping though that we can decouple the LLVM 5 upgrade and adding this wasm target!

### But the modules generated are huge!

It's worth nothing that you may not immediately see the "smallest possible wasm module" for the input you feed to rustc. For various reasons it's very difficult to get rid of the final "bloat" in vanilla rustc (again, a real linker should fix all this). For now what you'll have to do is:

    cargo install --git https://github.com/alexcrichton/wasm-gc
    wasm-gc foo.wasm bar.wasm

And then `bar.wasm` should be the smallest we can get it!

---

In any case for now I'd love feedback on this, particularly on the various integration points if you've got better ideas of how to approach them!
2017-11-20 08:29:46 +00:00
Alex Crichton
80ff0f74b0 std: Add a new wasm32-unknown-unknown target
This commit adds a new target to the compiler: wasm32-unknown-unknown. This
target is a reimagining of what it looks like to generate WebAssembly code from
Rust. Instead of using Emscripten which can bring with it a weighty runtime this
instead is a target which uses only the LLVM backend for WebAssembly and a
"custom linker" for now which will hopefully one day be direct calls to lld.

Notable features of this target include:

* There is zero runtime footprint. The target assumes nothing exists other than
  the wasm32 instruction set.
* There is zero toolchain footprint beyond adding the target. No custom linker
  is needed, rustc contains everything.
* Very small wasm modules can be generated directly from Rust code using this
  target.
* Most of the standard library is stubbed out to return an error, but anything
  related to allocation works (aka `HashMap`, `Vec`, etc).
* Naturally, any `#[no_std]` crate should be 100% compatible with this new
  target.

This target is currently somewhat janky due to how linking works. The "linking"
is currently unconditional whole program LTO (aka LLVM is being used as a
linker). Naturally that means compiling programs is pretty slow! Eventually
though this target should have a linker.

This target is also intended to be quite experimental. I'm hoping that this can
act as a catalyst for further experimentation in Rust with WebAssembly. Breaking
changes are very likely to land to this target, so it's not recommended to rely
on it in any critical capacity yet. We'll let you know when it's "production
ready".

---

Currently testing-wise this target is looking pretty good but isn't complete.
I've got almost the entire `run-pass` test suite working with this target (lots
of tests ignored, but many passing as well). The `core` test suite is still
getting LLVM bugs fixed to get that working and will take some time. Relatively
simple programs all seem to work though!

---

It's worth nothing that you may not immediately see the "smallest possible wasm
module" for the input you feed to rustc. For various reasons it's very difficult
to get rid of the final "bloat" in vanilla rustc (again, a real linker should
fix all this). For now what you'll have to do is:

    cargo install --git https://github.com/alexcrichton/wasm-gc
    wasm-gc foo.wasm bar.wasm

And then `bar.wasm` should be the smallest we can get it!

---

In any case for now I'd love feedback on this, particularly on the various
integration points if you've got better ideas of how to approach them!
2017-11-19 21:07:41 -08:00
Scott McMurray
eef4d42a3f Fundamental internal iteration with try_fold
This is the core method in terms of which the other methods (fold, all, any, find, position, nth, ...) can be implemented, allowing Iterator implementors to get the full goodness of internal iteration by only overriding one method (per direction).
2017-10-29 15:45:20 -07:00
bors
6f87d20a7c Auto merge of #42526 - huntiep:try_opt, r=nikomatsakis
Impl Try for Option

This is part of #31436.
2017-09-29 20:09:35 +00:00
Hunter Praska
f098d7be29 Add tests for Option and Result Try impl 2017-09-27 17:56:40 -04:00
Josh Stone
13724fafdc Add more custom folding to core::iter adaptors
Many of the iterator adaptors will perform faster folds if they forward
to their inner iterator's folds, especially for inner types like `Chain`
which are optimized too.  The following types are newly specialized:

| Type        | `fold` | `rfold` |
| ----------- | ------ | ------- |
| `Enumerate` | ✓      | ✓       |
| `Filter`    | ✓      | ✓       |
| `FilterMap` | ✓      | ✓       |
| `FlatMap`   | exists | ✓       |
| `Fuse`      | ✓      | ✓       |
| `Inspect`   | ✓      | ✓       |
| `Peekable`  | ✓      | N/A¹    |
| `Skip`      | ✓      | N/A²    |
| `SkipWhile` | ✓      | N/A¹    |

¹ not a `DoubleEndedIterator`

² `Skip::next_back` doesn't pull skipped items at all, but this couldn't
be avoided if `Skip::rfold` were to call its inner iterator's `rfold`.

Benchmarks
----------

In the following results, plain `_sum` computes the sum of a million
integers -- note that `sum()` is implemented with `fold()`.  The
`_ref_sum` variants do the same on a `by_ref()` iterator, which is
limited to calling `next()` one by one, without specialized `fold`.

The `chain` variants perform the same tests on two iterators chained
together, to show a greater benefit of forwarding `fold` internally.

    test iter::bench_enumerate_chain_ref_sum  ... bench:   2,216,264 ns/iter (+/- 29,228)
    test iter::bench_enumerate_chain_sum      ... bench:     922,380 ns/iter (+/- 2,676)
    test iter::bench_enumerate_ref_sum        ... bench:     476,094 ns/iter (+/- 7,110)
    test iter::bench_enumerate_sum            ... bench:     476,438 ns/iter (+/- 3,334)

    test iter::bench_filter_chain_ref_sum     ... bench:   2,266,095 ns/iter (+/- 6,051)
    test iter::bench_filter_chain_sum         ... bench:     745,594 ns/iter (+/- 2,013)
    test iter::bench_filter_ref_sum           ... bench:     889,696 ns/iter (+/- 1,188)
    test iter::bench_filter_sum               ... bench:     667,325 ns/iter (+/- 1,894)

    test iter::bench_filter_map_chain_ref_sum ... bench:   2,259,195 ns/iter (+/- 353,440)
    test iter::bench_filter_map_chain_sum     ... bench:   1,223,280 ns/iter (+/- 1,972)
    test iter::bench_filter_map_ref_sum       ... bench:     611,607 ns/iter (+/- 2,507)
    test iter::bench_filter_map_sum           ... bench:     611,610 ns/iter (+/- 472)

    test iter::bench_fuse_chain_ref_sum       ... bench:   2,246,106 ns/iter (+/- 22,395)
    test iter::bench_fuse_chain_sum           ... bench:     634,887 ns/iter (+/- 1,341)
    test iter::bench_fuse_ref_sum             ... bench:     444,816 ns/iter (+/- 1,748)
    test iter::bench_fuse_sum                 ... bench:     316,954 ns/iter (+/- 2,616)

    test iter::bench_inspect_chain_ref_sum    ... bench:   2,245,431 ns/iter (+/- 21,371)
    test iter::bench_inspect_chain_sum        ... bench:     631,645 ns/iter (+/- 4,928)
    test iter::bench_inspect_ref_sum          ... bench:     317,437 ns/iter (+/- 702)
    test iter::bench_inspect_sum              ... bench:     315,942 ns/iter (+/- 4,320)

    test iter::bench_peekable_chain_ref_sum   ... bench:   2,243,585 ns/iter (+/- 12,186)
    test iter::bench_peekable_chain_sum       ... bench:     634,848 ns/iter (+/- 1,712)
    test iter::bench_peekable_ref_sum         ... bench:     444,808 ns/iter (+/- 480)
    test iter::bench_peekable_sum             ... bench:     317,133 ns/iter (+/- 3,309)

    test iter::bench_skip_chain_ref_sum       ... bench:   1,778,734 ns/iter (+/- 2,198)
    test iter::bench_skip_chain_sum           ... bench:     761,850 ns/iter (+/- 1,645)
    test iter::bench_skip_ref_sum             ... bench:     478,207 ns/iter (+/- 119,252)
    test iter::bench_skip_sum                 ... bench:     315,614 ns/iter (+/- 3,054)

    test iter::bench_skip_while_chain_ref_sum ... bench:   2,486,370 ns/iter (+/- 4,845)
    test iter::bench_skip_while_chain_sum     ... bench:     633,915 ns/iter (+/- 5,892)
    test iter::bench_skip_while_ref_sum       ... bench:     666,926 ns/iter (+/- 804)
    test iter::bench_skip_while_sum           ... bench:     444,405 ns/iter (+/- 571)
2017-09-25 20:53:08 -07:00
Alex Crichton
3cc135afa3 Rollup merge of #44593 - budziq:stabilize_ord_max_min, r=alexcrichton
stabilized ord_max_min (fixes #25663)
2017-09-16 17:09:40 -07:00
Alex Burka
681e5da61e change #![feature(const_fn)] to specific gates 2017-09-16 15:53:02 +00:00
Michal Budzynski
5398e03704 stabilized ord_max_min (fixes #25663) 2017-09-15 12:54:03 +02:00
Tatsuyuki Ishi
611b111139 Move unused-extern-crate to late pass 2017-08-27 19:02:24 +09:00
Michael Howell
03acea646c Implement RefCell::replace and RefCell::swap 2017-07-31 21:19:09 +00:00
Alex Crichton
4c9c6e824b std: Stabilize char_escape_debug
Stabilizes:

* `<char>::escape_debug`
* `std::char::EscapeDebug`

Closes #35068
2017-07-25 07:09:31 -07:00
Simon Sapin
7a40307a7c Add tests for Range*::nth 2017-07-08 08:55:55 +02:00
bors
7a75d2bec4 Auto merge of #43012 - scottmcm:delete-range-step-by, r=alexcrichton
Delete deprecated & unstable range-specific `step_by`

Using the new one is annoying while this one exists, since the inherent method hides the one on iterator.

Tracking issue: #27741
Replacement: #41439
Deprecation: #42310 for 1.19
Fixes #41477
2017-07-04 04:49:59 +00:00
Stjepan Glavina
66f8cddae5 Remove the remaining feature gates 2017-07-02 21:29:39 +02:00
Scott McMurray
dcd332ed94 Delete deprecated & unstable range-specific step_by
Replacement: 41439
Deprecation: 42310 for 1.19
Fixes 41477
2017-07-01 19:18:02 -07:00
bors
fe7227f6c8 Auto merge of #42430 - nagisa:core-float, r=alexcrichton
Re-implement float min/max in rust

This also adds the relevant implementations into libcore.

See #42423
2017-06-16 17:52:11 +00:00
Simonas Kazlauskas
ba6cf1d80a Re-implement float min/max in rust
See #42423
2017-06-14 21:58:17 +03:00
Nick Whitney
2cadc32b66 Add max and min default fns to Ord trait
Pursuant to issue #25663, this commit adds the max and min functions to the Ord trait, enabling items that implement Ord to use UFCS (ex. 1.max(2)) instead of the longer std::cmp::max(1,2) format.
2017-06-06 22:42:48 -04:00
bors
558cd1e393 Auto merge of #41670 - scottmcm:slice-rotate, r=alexcrichton
Add an in-place rotate method for slices to libcore

A helpful primitive for moving chunks of data around inside a slice.

For example, if you have a range selected and are drag-and-dropping it somewhere else (Example from [Sean Parent's talk](https://youtu.be/qH6sSOr-yk8?t=560)).

(If this should be an RFC instead of a PR, please let me know.)

Edit: changed example
2017-06-02 07:51:20 +00:00
bors
bcf95067e4 Auto merge of #42167 - scottmcm:iter-stepby-sizehint, r=alexcrichton
Override size_hint and propagate ExactSizeIterator for iter::StepBy

Generally useful, but also a prerequisite for moving a bunch of unit tests off `Range*::step_by`.

A small non-breaking subset of https://github.com/rust-lang/rust/pull/42110 (which I closed).

Includes two small documentation changes @ivandardi requested on that PR.

r? @alexcrichton
2017-05-28 14:26:52 +00:00
Scott McMurray
57f260d418 Override size_hint and propagate ExactSizeIterator for iter::StepBy
Generally useful, but also a prerequisite for moving a bunch of unit tests off Range::step_by.
2017-05-23 02:24:25 -07:00
Scott McMurray
c05676b97f Add an in-place rotate method for slices to libcore
A helpful primitive for moving chunks of data around inside a slice.
In particular, adding elements to the end of a Vec then moving them
somewhere else, as a way to do efficient multiple-insert.  (There's
drain for efficient block-remove, but no easy way to block-insert.)

Talk with another example: <https://youtu.be/qH6sSOr-yk8?t=560>
2017-05-21 01:55:43 -07:00
Scott McMurray
f166bd9857 Make RangeInclusive just a two-field struct
Not being an enum improves ergonomics, especially since NonEmpty could be Empty.  It can still be iterable without an extra "done" bit by making the range have !(start <= end), which is even possible without changing the Step trait.

Implements RFC 1980
2017-05-21 01:48:03 -07:00
Ivan Dardi
fa01372555 Fix commit derp and update implementations and documentation 2017-05-12 15:11:15 -03:00
Scott McMurray
f8c6436173 Step::replace_one should put a one, not a zero (Issue #41492)
Turns out all six of these impls are incorrect.
2017-04-23 21:47:09 -07:00
Ulrik Sverdrup
5d2f270395 slice: Implement .rfind() for slice iterators Iter and IterMut
Just like the forward case find, implement rfind explicitly
2017-04-08 03:45:48 +02:00
Stjepan Glavina
13c744f30d Move libXtest into libX/tests
This change moves:

1. `libcoretest` into `libcore/tests`
2. `libcollectionstest` into `libcollections/tests`

This is a follow-up to #39561.
2017-04-03 20:49:39 +02:00