├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── appveyor.yml ├── benches └── step_by_comparison.rs ├── ci ├── run_linux_and_mac.sh └── run_windows.bat ├── examples └── main_example.rs ├── rustfmt.toml ├── src ├── lib.rs └── utils.rs └── tests └── test_staticstep.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "staticstep" 3 | description = "Provides truly zero-cost alternatives to `Iterator::step_by` for both incrementing and decrementing any type that satisfies `RangeBounds`." 4 | repository = "https://github.com/slightlyoutofphase/staticstep" 5 | documentation = "https://docs.rs/staticstep/" 6 | license = "MIT OR Apache-2.0" 7 | readme = "README.md" 8 | version = "0.4.2" 9 | keywords = ["iterator", "iterators", "step", "range", "ranges"] 10 | categories = ["data-structures", "algorithms"] 11 | edition = "2021" 12 | include = ["Cargo.toml", "rustfmt.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"] 13 | 14 | [badges.maintenance] 15 | status = "actively-developed" 16 | 17 | [badges.appveyor] 18 | repository = "slightlyoutofphase/staticstep" 19 | 20 | [profile.release] 21 | opt-level = 3 22 | debug = false 23 | rpath = false 24 | lto = true 25 | debug-assertions = false 26 | codegen-units = 1 27 | panic = 'abort' 28 | incremental = false 29 | overflow-checks = false 30 | 31 | [profile.dev] 32 | opt-level = 0 33 | debug = true 34 | rpath = false 35 | lto = false 36 | debug-assertions = true 37 | codegen-units = 16 38 | panic = 'unwind' 39 | incremental = true 40 | overflow-checks = true 41 | 42 | [profile.bench] 43 | opt-level = 3 44 | debug = false 45 | rpath = false 46 | lto = true 47 | debug-assertions = false 48 | codegen-units = 1 49 | incremental = false 50 | overflow-checks = false 51 | 52 | [dependencies] 53 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any 2 | person obtaining a copy of this software and associated 3 | documentation files (the "Software"), to deal in the 4 | Software without restriction, including without 5 | limitation the rights to use, copy, modify, merge, 6 | publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software 8 | is furnished to do so, subject to the following 9 | conditions: 10 | 11 | The above copyright notice and this permission notice 12 | shall be included in all copies or substantial portions 13 | of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 16 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 17 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 18 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 19 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 22 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Version]][crates.io] ![Rustc Version nightly] 2 | 3 | [Latest Version]: https://img.shields.io/crates/v/staticstep.svg 4 | [crates.io]: https://crates.io/crates/staticstep 5 | [Rustc Version nightly]: https://img.shields.io/badge/rustc-nightly-lightgray.svg 6 | [![Build status](https://ci.appveyor.com/api/projects/status/dw7nt480aewaux76/branch/master?svg=true)](https://ci.appveyor.com/project/slightlyoutofphase/staticstep/branch/master) 7 | [![docs.rs](https://docs.rs/staticstep/badge.svg)](https://docs.rs/staticstep/) 8 | 9 | Provides truly zero-cost alternatives to `Iterator::step_by` for both incrementing and decrementing any type that satisfies `RangeBounds`. 10 | 11 | The assembly code generated for the two trait methods this crate implements, `inc_by` and `dec_by`, should in the overwhelming majority of cases be nearly or completely identical 12 | to the assembly code that would be generated for an incrementing "step"-based `while` loop or decrementing "step"-based `while` loop, respectively. If you come across a scenario where 13 | it is not, please feel free to open an issue about it. 14 | 15 | **Minimum supported Rust version:** this is a nightly-only crate at the moment due to the use of 16 | the `Step` trait, which has not yet been stabilized. 17 | 18 | **`#![no_std]` compatibility:** this crate is fully `#![no_std]` compatible by default. 19 | 20 | A basic usage example: 21 | 22 | ```rust 23 | use staticstep::*; 24 | 25 | // Apart from aiming to provide a properly-optimized Rust equivalent to the sort of C-style for-loop 26 | // that ends in `i += number` or `i -= number` as opposed to `i++` or `i--`, this crate also aims to 27 | // (and does) support backwards ranges in a meaningful way that's logically equivalent to how 28 | // forwards ranges are generally dealt with in Rust. 29 | 30 | fn main() { 31 | // Exclusive, so 48 is the last number printed. 32 | for i in (0..64).inc_by::<16>() { 33 | print!("{} ", i); 34 | } 35 | println!(""); 36 | 37 | // Inclusive, so 64 is the last number printed. 38 | for i in (0..=64).inc_by::<16>() { 39 | print!("{} ", i); 40 | } 41 | println!(""); 42 | 43 | // Exclusive, so 16 is the last number printed. 44 | for i in (64..0).dec_by::<16>() { 45 | print!("{} ", i); 46 | } 47 | println!(""); 48 | 49 | // Inclusive, so 0 is the last number printed. 50 | for i in (64..=0).dec_by::<16>() { 51 | print!("{} ", i); 52 | } 53 | 54 | // Note that `inc_by` will always immediately return `None` if given a reverse range, while 55 | // `dec_by` will always immediately return `None` if given a "normal" forwards range. 56 | } 57 | ``` 58 | 59 | **License:** 60 | 61 | Licensed under either the MIT license or version 2.0 of the Apache License. Your choice as to which! 62 | Any source code contributions will be dual-licensed in the same fashion. 63 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | image: 2 | - Visual Studio 2022 3 | - Ubuntu2004 4 | - macOS 5 | environment: 6 | APPVEYOR_YML_DISABLE_PS_LINUX: true 7 | matrix: 8 | - TARGET: x86_64-pc-windows-msvc 9 | - TARGET: i686-pc-windows-msvc 10 | - TARGET: x86_64-unknown-linux-gnu 11 | - TARGET: x86_64-apple-darwin 12 | matrix: 13 | exclude: 14 | - image: Ubuntu2004 15 | TARGET: x86_64-pc-windows-msvc 16 | - image: Ubuntu2004 17 | TARGET: i686-pc-windows-msvc 18 | - image: Ubuntu2004 19 | TARGET: x86_64-apple-darwin 20 | - image: macOS 21 | TARGET: x86_64-pc-windows-msvc 22 | - image: macOS 23 | TARGET: i686-pc-windows-msvc 24 | - image: macOS 25 | TARGET: x86_64-unknown-linux-gnu 26 | - image: Visual Studio 2022 27 | TARGET: x86_64-unknown-linux-gnu 28 | - image: Visual Studio 2022 29 | TARGET: x86_64-apple-darwin 30 | skip_tags: true 31 | branches: 32 | only: 33 | - master 34 | for: 35 | - 36 | matrix: 37 | only: 38 | - image: Visual Studio 2022 39 | install: 40 | - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe 41 | - cmd: rustup-init.exe -yv --default-toolchain nightly --default-host %TARGET% --profile minimal 42 | - cmd: set PATH=%PATH%;C:\Users\appveyor\.cargo\bin 43 | cache: 44 | #- '%USERPROFILE%\.cargo' 45 | - 46 | matrix: 47 | only: 48 | - image: Ubuntu2004 49 | install: 50 | - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -yv --default-toolchain nightly --default-host x86_64-unknown-linux-gnu --profile minimal 51 | cache: 52 | #- '/home/appveyor/.cargo' 53 | - 54 | matrix: 55 | only: 56 | - image: macOS 57 | install: 58 | - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -yv --default-toolchain nightly --default-host x86_64-apple-darwin --profile minimal 59 | cache: 60 | #- '$HOME/.cargo' 61 | build: false 62 | test_script: 63 | # We run the suite once with all functionality enabled, and then once without 64 | # the default features to make sure `no_std` support has not been broken 65 | - cmd: ./ci/run_windows.bat 66 | - sh: sudo sh ./ci/run_linux_and_mac.sh 67 | -------------------------------------------------------------------------------- /benches/step_by_comparison.rs: -------------------------------------------------------------------------------- 1 | #![feature(test)] 2 | 3 | extern crate test; 4 | 5 | use test::{black_box, Bencher}; 6 | 7 | use staticstep::*; 8 | 9 | // We use explicit `usize` variables everywhere here just to 10 | // make sure the compiler doesn't do any integer-type 11 | // optimizations that would make the benchmark less fair. 12 | 13 | #[bench] 14 | fn inc_by_exclusive(b: &mut Bencher) { 15 | b.iter(|| { 16 | let mut j = 0usize; 17 | for i in (0usize..32768usize).inc_by::<16usize>() { 18 | j += black_box(i); 19 | } 20 | let _ = black_box(j); 21 | }); 22 | } 23 | 24 | #[bench] 25 | fn inc_by_inclusive(b: &mut Bencher) { 26 | b.iter(|| { 27 | let mut j = 0usize; 28 | for i in (0usize..=32768usize).inc_by::<16usize>() { 29 | j += black_box(i); 30 | } 31 | let _ = black_box(j); 32 | }); 33 | } 34 | 35 | #[bench] 36 | fn dec_by_exclusive(b: &mut Bencher) { 37 | b.iter(|| { 38 | let mut j = 0usize; 39 | for i in (32768usize..0usize).dec_by::<16usize>() { 40 | j += black_box(i); 41 | } 42 | let _ = black_box(j); 43 | }); 44 | } 45 | 46 | #[bench] 47 | fn dec_by_inclusive(b: &mut Bencher) { 48 | b.iter(|| { 49 | let mut j = 0usize; 50 | for i in (32768usize..=0usize).dec_by::<16usize>() { 51 | j += black_box(i); 52 | } 53 | let _ = black_box(j); 54 | }); 55 | } 56 | 57 | #[bench] 58 | fn while_loop_inc_exclusive(b: &mut Bencher) { 59 | b.iter(|| { 60 | let mut i = 0usize; 61 | let mut j = 0usize; 62 | while i < 32768usize { 63 | j += black_box(i); 64 | i += 16usize; 65 | } 66 | let _ = black_box(j); 67 | }); 68 | } 69 | 70 | #[bench] 71 | fn while_loop_inc_inclusive(b: &mut Bencher) { 72 | b.iter(|| { 73 | let mut i = 0usize; 74 | let mut j = 0usize; 75 | while i <= 32768usize { 76 | j += black_box(i); 77 | i += 16usize; 78 | } 79 | let _ = black_box(j); 80 | }); 81 | } 82 | 83 | #[bench] 84 | fn while_loop_dec_exclusive(b: &mut Bencher) { 85 | b.iter(|| { 86 | let mut j = 0usize; 87 | let mut i = 32768isize; 88 | while i > 0isize { 89 | j += black_box(i as usize); 90 | i -= 16isize; 91 | } 92 | let _ = black_box(j); 93 | }); 94 | } 95 | 96 | #[bench] 97 | fn while_loop_dec_inclusive(b: &mut Bencher) { 98 | b.iter(|| { 99 | let mut j = 0usize; 100 | let mut i = 32768isize; 101 | while i >= 0isize { 102 | j += black_box(i as usize); 103 | i -= 16isize; 104 | } 105 | let _ = black_box(j); 106 | }); 107 | } 108 | 109 | #[bench] 110 | fn step_by_inc_exclusive(b: &mut Bencher) { 111 | b.iter(|| { 112 | let mut j = 0usize; 113 | for i in (0usize..32768usize).step_by(16usize) { 114 | j += black_box(i); 115 | } 116 | let _ = black_box(j); 117 | }); 118 | } 119 | 120 | #[bench] 121 | fn step_by_inc_inclusive(b: &mut Bencher) { 122 | b.iter(|| { 123 | let mut j = 0usize; 124 | for i in (0usize..=32768usize).step_by(16usize) { 125 | j += black_box(i); 126 | } 127 | let _ = black_box(j); 128 | }); 129 | } 130 | 131 | #[bench] 132 | fn step_by_dec_exclusive(b: &mut Bencher) { 133 | b.iter(|| { 134 | // There's no other way to write this such that it goes 135 | // over the exact same numbers as the other versions and 136 | // also stops in the same place. 137 | let mut j = 0usize; 138 | for i in (16usize..32784usize).step_by(16usize).rev() { 139 | j += black_box(i); 140 | } 141 | let _ = black_box(j); 142 | }); 143 | } 144 | 145 | #[bench] 146 | fn step_by_dec_inclusive(b: &mut Bencher) { 147 | b.iter(|| { 148 | let mut j = 0usize; 149 | for i in (0usize..=32768usize).rev().step_by(16usize) { 150 | j += black_box(i); 151 | } 152 | let _ = black_box(j); 153 | }); 154 | } 155 | 156 | #[bench] 157 | fn inc_by_zip(b: &mut Bencher) { 158 | b.iter(|| { 159 | let mut k = 0usize; 160 | for (i, j) in (0usize..=8192usize) 161 | .inc_by::<27usize>() 162 | .zip((8192usize..=0usize).dec_by::<27usize>()) 163 | { 164 | k += black_box(i + j); 165 | } 166 | let _ = black_box(k); 167 | }); 168 | } 169 | 170 | #[bench] 171 | fn step_by_zip(b: &mut Bencher) { 172 | b.iter(|| { 173 | let mut k = 0usize; 174 | for (i, j) in (0usize..=8192usize) 175 | .step_by(27usize) 176 | .zip((0usize..=8192usize).rev().step_by(27usize)) 177 | { 178 | k += black_box(i + j); 179 | } 180 | let _ = black_box(k); 181 | }); 182 | } 183 | -------------------------------------------------------------------------------- /ci/run_linux_and_mac.sh: -------------------------------------------------------------------------------- 1 | case $(uname | tr '[:upper:]' '[:lower:]') in 2 | linux*) 3 | export APPVEYOR_OS_NAME=linux 4 | ;; 5 | darwin*) 6 | export APPVEYOR_OS_NAME=osx 7 | ;; 8 | esac 9 | if [ "$APPVEYOR_OS_NAME" = "linux" ]; then export PATH="/home/appveyor/.cargo/bin:$PATH"; fi 10 | if [ "$APPVEYOR_OS_NAME" = "osx" ]; then export PATH="$HOME/.cargo/bin:$PATH"; fi 11 | if [ "$APPVEYOR_OS_NAME" = "linux" ]; then MIRI_NIGHTLY=nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri); fi 12 | if [ "$APPVEYOR_OS_NAME" = "osx" ]; then MIRI_NIGHTLY=nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/x86_64-apple-darwin/miri); fi 13 | echo "Installing latest nightly with Miri: $MIRI_NIGHTLY" 14 | rustup set profile minimal 15 | rustup default "$MIRI_NIGHTLY" 16 | rustup component add miri 17 | # This crate doesn't even use unsafe, but hey, why not. 18 | cargo miri test 19 | cargo clean 20 | -------------------------------------------------------------------------------- /ci/run_windows.bat: -------------------------------------------------------------------------------- 1 | cargo clean 2 | cargo test 3 | -------------------------------------------------------------------------------- /examples/main_example.rs: -------------------------------------------------------------------------------- 1 | #![feature(bench_black_box)] 2 | 3 | use core::hint::black_box; 4 | use staticstep::*; 5 | 6 | // You can run this with "--emit asm" as an easy way of comparing the assembly output from this 7 | // crate's functions to the assembly output from a traditional loop. 8 | 9 | #[inline(never)] 10 | fn inc_by_exclusive() { 11 | let mut j = 0; 12 | for i in (0..32768).inc_by::<16>() { 13 | j += black_box(i); 14 | } 15 | println!("{}", j); 16 | } 17 | 18 | #[inline(never)] 19 | fn inc_by_inclusive() { 20 | let mut j = 0; 21 | for i in (0..=32768).inc_by::<16>() { 22 | j += black_box(i); 23 | } 24 | println!("{}", j); 25 | } 26 | 27 | #[inline(never)] 28 | fn dec_by_exclusive() { 29 | let mut j = 0; 30 | for i in (32768..0).dec_by::<16>() { 31 | j += black_box(i); 32 | } 33 | println!("{}", j); 34 | } 35 | 36 | #[inline(never)] 37 | fn dec_by_inclusive() { 38 | let mut j = 0; 39 | for i in (32768..=0).dec_by::<16>() { 40 | j += black_box(i); 41 | } 42 | println!("{}", j); 43 | } 44 | 45 | #[inline(never)] 46 | fn while_loop_inc_exclusive() { 47 | let mut j = 0; 48 | let mut i = 0; 49 | while i < 32768 { 50 | j += black_box(i); 51 | i += 16; 52 | } 53 | println!("{}", j); 54 | } 55 | 56 | #[inline(never)] 57 | fn while_loop_inc_inclusive() { 58 | let mut j = 0; 59 | let mut i = 0; 60 | while i <= 32768 { 61 | j += black_box(i); 62 | i += 16; 63 | } 64 | println!("{}", j); 65 | } 66 | 67 | #[inline(never)] 68 | fn while_loop_dec_exclusive() { 69 | let mut j = 0usize; 70 | let mut i = 32768isize; 71 | while i > 0 { 72 | j += black_box(i as usize); 73 | i -= 16; 74 | } 75 | println!("{}", j); 76 | } 77 | 78 | #[inline(never)] 79 | fn while_loop_dec_inclusive() { 80 | let mut j = 0usize; 81 | let mut i = 32768isize; 82 | while i >= 0 { 83 | j += black_box(i as usize); 84 | i -= 16; 85 | } 86 | println!("{}", j); 87 | } 88 | 89 | fn main() { 90 | inc_by_exclusive(); 91 | inc_by_inclusive(); 92 | dec_by_exclusive(); 93 | dec_by_inclusive(); 94 | while_loop_inc_exclusive(); 95 | while_loop_inc_inclusive(); 96 | while_loop_dec_exclusive(); 97 | while_loop_dec_inclusive(); 98 | } 99 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | tab_spaces = 2 2 | edition = "2018" 3 | where_single_line = true 4 | brace_style = "PreferSameLine" 5 | control_brace_style = "AlwaysSameLine" 6 | format_macro_matchers = true 7 | comment_width = 100 8 | wrap_comments = true 9 | max_width = 100 -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![feature(core_intrinsics, step_trait)] 3 | 4 | use core::intrinsics::{likely, unlikely}; 5 | use core::iter::Step; 6 | use core::ops::{ 7 | Bound::{Excluded, Included, Unbounded}, 8 | RangeBounds, 9 | }; 10 | 11 | use crate::utils::max_value; 12 | 13 | mod utils; 14 | 15 | /// The [`Iterator`](core::iter::Iterator)-implementing struct through which the functionality of 16 | /// [`inc_by`](crate::IntoIncBy::inc_by) actually operates. Not useful by itself. 17 | pub struct IncBy { 18 | start: T, 19 | end: T, 20 | had_overflow: bool, 21 | was_unbound: bool, 22 | } 23 | 24 | /// The [`Iterator`](core::iter::Iterator)-implementing struct through which the functionality of 25 | /// [`dec_by`](crate::IntoDecBy::dec_by) actually operates. Not useful by itself. 26 | pub struct DecBy { 27 | start: T, 28 | end: T, 29 | had_overflow: bool, 30 | } 31 | 32 | impl IncBy { 33 | #[inline(always)] 34 | fn new>(bounds: R) -> IncBy { 35 | let start = match bounds.start_bound() { 36 | Included(&idx) => idx, 37 | Excluded(&idx) => idx, 38 | Unbounded => Default::default(), 39 | }; 40 | let mut had_overflow = false; 41 | let mut was_unbound = false; 42 | let end = match bounds.end_bound() { 43 | Included(&idx) => { 44 | if let Some(res) = Step::forward_checked(idx, STEP) { 45 | res 46 | } else { 47 | had_overflow = true; 48 | idx 49 | } 50 | } 51 | Excluded(&idx) => { 52 | if let Some(res) = Step::forward_checked(idx, STEP - 1) { 53 | res 54 | } else { 55 | had_overflow = true; 56 | idx 57 | } 58 | } 59 | Unbounded => { 60 | was_unbound = true; 61 | Step::forward(Default::default(), max_value(&start)) 62 | } 63 | }; 64 | IncBy { 65 | start, 66 | end, 67 | had_overflow, 68 | was_unbound, 69 | } 70 | } 71 | } 72 | 73 | impl DecBy { 74 | #[inline(always)] 75 | fn new>(bounds: R) -> DecBy { 76 | let start = match bounds.end_bound() { 77 | Included(&idx) => idx, 78 | Excluded(&idx) => Step::forward_checked(idx, 1).unwrap_or(idx), 79 | Unbounded => Step::forward(Default::default(), 1), 80 | }; 81 | let mut had_overflow = false; 82 | let end = match bounds.start_bound() { 83 | Included(&idx) => { 84 | if let Some(res) = Step::forward_checked(idx, STEP) { 85 | res 86 | } else { 87 | had_overflow = true; 88 | idx 89 | } 90 | } 91 | Excluded(&idx) => { 92 | if let Some(res) = Step::forward_checked(idx, STEP + 1) { 93 | res 94 | } else { 95 | had_overflow = true; 96 | idx 97 | } 98 | } 99 | Unbounded => Default::default(), 100 | }; 101 | DecBy { 102 | start, 103 | end, 104 | had_overflow, 105 | } 106 | } 107 | } 108 | 109 | impl Iterator for IncBy { 110 | type Item = T; 111 | 112 | #[inline(always)] 113 | fn next(&mut self) -> Option { 114 | if unlikely(self.had_overflow) { 115 | self.had_overflow = false; 116 | let res = Some(self.start); 117 | self.start = Step::forward_checked(self.start, STEP).unwrap_or(self.start); 118 | res 119 | } else if let Some(end_back) = Step::backward_checked(self.end, STEP) { 120 | if likely(self.start <= end_back) { 121 | let res = Some(self.start); 122 | self.start = Step::forward(self.start, STEP); 123 | res 124 | } else if unlikely(self.was_unbound) { 125 | self.was_unbound = false; 126 | Some(self.start) 127 | } else { 128 | None 129 | } 130 | } else { 131 | None 132 | } 133 | } 134 | } 135 | 136 | impl Iterator for DecBy { 137 | type Item = T; 138 | 139 | #[inline(always)] 140 | fn next(&mut self) -> Option { 141 | if unlikely(self.had_overflow) { 142 | self.had_overflow = false; 143 | Some(self.end) 144 | } else if let Some(start_forward) = Step::forward_checked(self.start, STEP) { 145 | if likely(start_forward <= self.end) { 146 | self.end = Step::backward(self.end, STEP); 147 | Some(self.end) 148 | } else { 149 | None 150 | } 151 | } else { 152 | None 153 | } 154 | } 155 | } 156 | 157 | /// A subtrait of [`RangeBounds`](core::ops::RangeBounds) where `T` is `Copy + Default + Step` 158 | /// that turns implementers of it into an instance of [`IncBy`][crate::IncBy] when 159 | /// [`inc_by`](crate::IntoIncBy::inc_by) is called. Currently, the blanket implementation of this 160 | /// trait exported from the crate for `RangeBounds` is the only possible useful implementation, but 161 | /// it was decided not to make it a "sealed" trait in case additional methods are added in the 162 | /// future that would be implementable by end-user code in a meaningfully varied way. 163 | pub trait IntoIncBy: RangeBounds { 164 | /// Functionally equivalent to what [`step_by`](core::iter::Iterator::step_by) does when it is 165 | /// called through a primitive range, but written specifically with primitive ranges in mind such 166 | /// that it optimizes identically to a `while` loop in every case the author of this crate has 167 | /// examined so far. 168 | /// 169 | /// # Example usage: 170 | /// ``` 171 | /// # use staticstep::*; 172 | /// // Exclusive, so prints: 'A C E' 173 | /// for i in ('A'..'G').inc_by::<2>() { 174 | /// print!("{} ", i); 175 | /// } 176 | /// 177 | /// // Inclusive, so prints: '0 4 8 12' 178 | /// for i in (0isize..=12isize).inc_by::<4>() { 179 | /// print!("{} ", i); 180 | /// } 181 | fn inc_by(self) -> IncBy; 182 | } 183 | 184 | /// A subtrait of [`RangeBounds`](core::ops::RangeBounds) where `T` is `Copy + Default + Step` 185 | /// that turns implementers of it into an instance of [`DecBy`][crate::DecBy] when 186 | /// [`dec_by`](crate::IntoDecBy::dec_by) is called. Currently, the blanket implementation of this 187 | /// trait exported from the crate for `RangeBounds` is the only possible useful implementation, but 188 | /// it was decided not to make it a "sealed" trait in case additional methods are added in the 189 | /// future that would be implementable by end-user code in a meaningfully varied way. 190 | pub trait IntoDecBy: RangeBounds { 191 | /// Functionally equivalent to what [`step_by`](core::iter::Iterator::step_by) does when it is 192 | /// called through a primitive range, but specifically in reverse and operating directly on 193 | /// ranges that are themselves syntactically "backwards", while offering the same level of 194 | /// optimizability as [`inc_by`](crate::IntoIncBy::inc_by). 195 | /// 196 | /// # Example usage: 197 | /// ``` 198 | /// # use staticstep::*; 199 | /// // Exclusive, so prints: 'G E C' 200 | /// for i in ('G'..'A').dec_by::<2>() { 201 | /// print!("{} ", i); 202 | /// } 203 | /// 204 | /// // Inclusive, so prints: '4 1 -2' 205 | /// for i in (4isize..=-4isize).dec_by::<3>() { 206 | /// print!("{} ", i); 207 | /// } 208 | fn dec_by(self) -> DecBy; 209 | } 210 | 211 | /// The actual implementation of [`IntoIncBy`](crate::IntoIncBy) for 212 | /// [`RangeBounds`](core::ops::RangeBounds). 213 | impl> IntoIncBy for R { 214 | #[inline(always)] 215 | fn inc_by(self) -> IncBy { 216 | IncBy::::new(self) 217 | } 218 | } 219 | 220 | /// The actual implementation of [`IntoDecBy`](crate::IntoDecBy) for 221 | /// [`RangeBounds`](core::ops::RangeBounds). 222 | impl> IntoDecBy for R { 223 | #[inline(always)] 224 | fn dec_by(self) -> DecBy { 225 | DecBy::::new(self) 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /src/utils.rs: -------------------------------------------------------------------------------- 1 | use core::iter::Step; 2 | 3 | // We use this specifically to handle the case of `RangeFrom` for `IncBy`. 4 | // Note that the compiler is able to trivially evaluate it down to a constant, 5 | // so it has zero impact on performance. 6 | #[inline(always)] 7 | pub(crate) fn max_value(_val: &T) -> usize { 8 | if Step::forward_checked(T::default(), usize::MAX).is_some() { 9 | usize::MAX 10 | // Best we can do here currently, as `Step` itself isn't really designed in a way that accounts 11 | // for `u128` and `i128`. 12 | } else if Step::forward_checked(T::default(), u128::MAX as usize).is_some() { 13 | u128::MAX as usize 14 | } else if Step::forward_checked(T::default(), u64::MAX as usize).is_some() { 15 | u64::MAX as usize 16 | } else if Step::forward_checked(T::default(), u32::MAX as usize).is_some() { 17 | u32::MAX as usize 18 | } else if Step::forward_checked(T::default(), u16::MAX as usize).is_some() { 19 | u16::MAX as usize 20 | } else if Step::forward_checked(T::default(), u8::MAX as usize).is_some() { 21 | u8::MAX as usize 22 | } else if Step::forward_checked(T::default(), isize::MAX as usize).is_some() { 23 | isize::MAX as usize 24 | // Best we can do here currently, as `Step` itself isn't really designed in a way that accounts 25 | // for `u128` and `i128`. 26 | } else if Step::forward_checked(T::default(), i128::MAX as usize).is_some() { 27 | i128::MAX as usize 28 | } else if Step::forward_checked(T::default(), i64::MAX as usize).is_some() { 29 | i64::MAX as usize 30 | } else if Step::forward_checked(T::default(), i32::MAX as usize).is_some() { 31 | i32::MAX as usize 32 | } else if Step::forward_checked(T::default(), i16::MAX as usize).is_some() { 33 | i16::MAX as usize 34 | } else if Step::forward_checked(T::default(), i8::MAX as usize).is_some() { 35 | i8::MAX as usize 36 | } else { 37 | // No better way to handle this at the moment. Not a huge deal IMO, as you'd have to be using 38 | // explicit `RangeFrom` syntax on some kind of struct or enum with a custom `RangeBounds` 39 | // implementation to get here anyways, and that seems like a really niche / unlikely scenario. 40 | Default::default() 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tests/test_staticstep.rs: -------------------------------------------------------------------------------- 1 | use staticstep::*; 2 | 3 | #[test] 4 | fn inc_by_exclusive() { 5 | let mut r = (0..64).inc_by::<16>(); 6 | assert_eq!(r.next(), Some(0)); 7 | assert_eq!(r.next(), Some(16)); 8 | assert_eq!(r.next(), Some(32)); 9 | assert_eq!(r.next(), Some(48)); 10 | assert_eq!(r.next(), None); 11 | } 12 | 13 | #[test] 14 | fn inc_by_exclusive_negative_start_even_step() { 15 | let mut r = (-64isize..64isize).step_by(16); 16 | assert_eq!(r.next(), Some(-64)); 17 | assert_eq!(r.next(), Some(-48)); 18 | assert_eq!(r.next(), Some(-32)); 19 | assert_eq!(r.next(), Some(-16)); 20 | assert_eq!(r.next(), Some(0)); 21 | assert_eq!(r.next(), Some(16)); 22 | assert_eq!(r.next(), Some(32)); 23 | assert_eq!(r.next(), Some(48)); 24 | assert_eq!(r.next(), None); 25 | } 26 | 27 | #[test] 28 | fn inc_by_exclusive_negative_start_uneven_step() { 29 | let mut r = (-64isize..64isize).step_by(27); 30 | assert_eq!(r.next(), Some(-64)); 31 | assert_eq!(r.next(), Some(-37)); 32 | assert_eq!(r.next(), Some(-10)); 33 | assert_eq!(r.next(), Some(17)); 34 | assert_eq!(r.next(), Some(44)); 35 | assert_eq!(r.next(), None); 36 | } 37 | 38 | #[test] 39 | fn inc_by_exclusive_unbound_start() { 40 | let mut r = (..64).inc_by::<16>(); 41 | assert_eq!(r.next(), Some(0)); 42 | assert_eq!(r.next(), Some(16)); 43 | assert_eq!(r.next(), Some(32)); 44 | assert_eq!(r.next(), Some(48)); 45 | assert_eq!(r.next(), None); 46 | } 47 | 48 | #[test] 49 | fn inc_by_unbound_end_u8() { 50 | let mut r = (0u8..).inc_by::<16>(); 51 | assert_eq!(r.next(), Some(0)); 52 | assert_eq!(r.next(), Some(16)); 53 | assert_eq!(r.next(), Some(32)); 54 | assert_eq!(r.next(), Some(48)); 55 | assert_eq!(r.next(), Some(64)); 56 | assert_eq!(r.next(), Some(80)); 57 | assert_eq!(r.next(), Some(96)); 58 | assert_eq!(r.next(), Some(112)); 59 | assert_eq!(r.next(), Some(128)); 60 | assert_eq!(r.next(), Some(144)); 61 | assert_eq!(r.next(), Some(160)); 62 | assert_eq!(r.next(), Some(176)); 63 | assert_eq!(r.next(), Some(192)); 64 | assert_eq!(r.next(), Some(208)); 65 | assert_eq!(r.next(), Some(224)); 66 | assert_eq!(r.next(), Some(240)); 67 | assert_eq!(r.next(), None); 68 | } 69 | 70 | #[test] 71 | fn inc_by_unbound_end_usize() { 72 | let mut r = (0usize..).inc_by::<16>(); 73 | assert_eq!(r.next(), Some(0)); 74 | assert_eq!(r.next(), Some(16)); 75 | assert_eq!(r.next(), Some(32)); 76 | assert_eq!(r.next(), Some(48)); 77 | assert_eq!(r.next(), Some(64)); 78 | assert_eq!(r.next(), Some(80)); 79 | assert_eq!(r.next(), Some(96)); 80 | assert_eq!(r.next(), Some(112)); 81 | assert_eq!(r.next(), Some(128)); 82 | assert_eq!(r.next(), Some(144)); 83 | assert_eq!(r.next(), Some(160)); 84 | assert_eq!(r.next(), Some(176)); 85 | assert_eq!(r.next(), Some(192)); 86 | assert_eq!(r.next(), Some(208)); 87 | assert_eq!(r.next(), Some(224)); 88 | assert_eq!(r.next(), Some(240)); 89 | // We just want to make sure this isn't `None`, since that would only make sense for `u8` as in 90 | // the above test. 91 | assert_eq!(r.next(), Some(256)); 92 | } 93 | 94 | #[test] 95 | fn inc_by_inclusive() { 96 | let mut r = (0..=64).inc_by::<16>(); 97 | assert_eq!(r.next(), Some(0)); 98 | assert_eq!(r.next(), Some(16)); 99 | assert_eq!(r.next(), Some(32)); 100 | assert_eq!(r.next(), Some(48)); 101 | assert_eq!(r.next(), Some(64)); 102 | assert_eq!(r.next(), None); 103 | } 104 | 105 | #[test] 106 | fn inc_by_inclusive_negative_start_even_step() { 107 | let mut r = (-64isize..=64isize).step_by(16); 108 | assert_eq!(r.next(), Some(-64)); 109 | assert_eq!(r.next(), Some(-48)); 110 | assert_eq!(r.next(), Some(-32)); 111 | assert_eq!(r.next(), Some(-16)); 112 | assert_eq!(r.next(), Some(0)); 113 | assert_eq!(r.next(), Some(16)); 114 | assert_eq!(r.next(), Some(32)); 115 | assert_eq!(r.next(), Some(48)); 116 | assert_eq!(r.next(), Some(64)); 117 | assert_eq!(r.next(), None); 118 | } 119 | 120 | #[test] 121 | fn inc_by_inclusive_negative_start_uneven_step() { 122 | let mut r = (-64isize..=64isize).step_by(27); 123 | assert_eq!(r.next(), Some(-64)); 124 | assert_eq!(r.next(), Some(-37)); 125 | assert_eq!(r.next(), Some(-10)); 126 | assert_eq!(r.next(), Some(17)); 127 | assert_eq!(r.next(), Some(44)); 128 | assert_eq!(r.next(), None); 129 | } 130 | 131 | #[test] 132 | fn inc_by_inclusive_unbound_start() { 133 | let mut r = (..=64).inc_by::<16>(); 134 | assert_eq!(r.next(), Some(0)); 135 | assert_eq!(r.next(), Some(16)); 136 | assert_eq!(r.next(), Some(32)); 137 | assert_eq!(r.next(), Some(48)); 138 | assert_eq!(r.next(), Some(64)); 139 | assert_eq!(r.next(), None); 140 | } 141 | 142 | #[test] 143 | fn dec_by_exclusive_usize() { 144 | let mut r = (64usize..0usize).dec_by::<16>(); 145 | assert_eq!(r.next(), Some(64)); 146 | assert_eq!(r.next(), Some(48)); 147 | assert_eq!(r.next(), Some(32)); 148 | assert_eq!(r.next(), Some(16)); 149 | assert_eq!(r.next(), None); 150 | } 151 | 152 | #[test] 153 | fn dec_by_inclusive_usize() { 154 | let mut r = (64usize..=0usize).dec_by::<16>(); 155 | assert_eq!(r.next(), Some(64)); 156 | assert_eq!(r.next(), Some(48)); 157 | assert_eq!(r.next(), Some(32)); 158 | assert_eq!(r.next(), Some(16)); 159 | assert_eq!(r.next(), Some(0)); 160 | assert_eq!(r.next(), None); 161 | } 162 | 163 | #[test] 164 | fn dec_by_exclusive_isize() { 165 | let mut r = (64isize..0isize).dec_by::<16>(); 166 | assert_eq!(r.next(), Some(64)); 167 | assert_eq!(r.next(), Some(48)); 168 | assert_eq!(r.next(), Some(32)); 169 | assert_eq!(r.next(), Some(16)); 170 | assert_eq!(r.next(), None); 171 | } 172 | 173 | #[test] 174 | fn dec_by_inclusive_isize() { 175 | let mut r = (64isize..=0isize).dec_by::<16>(); 176 | assert_eq!(r.next(), Some(64)); 177 | assert_eq!(r.next(), Some(48)); 178 | assert_eq!(r.next(), Some(32)); 179 | assert_eq!(r.next(), Some(16)); 180 | assert_eq!(r.next(), Some(0)); 181 | assert_eq!(r.next(), None); 182 | } 183 | 184 | #[test] 185 | fn dec_by_exclusive_unbound_end() { 186 | let mut r = (64isize..).dec_by::<16>(); 187 | assert_eq!(r.next(), Some(64)); 188 | assert_eq!(r.next(), Some(48)); 189 | assert_eq!(r.next(), Some(32)); 190 | assert_eq!(r.next(), Some(16)); 191 | assert_eq!(r.next(), None); 192 | } 193 | 194 | #[test] 195 | fn inc_by_exclusive_wrong() { 196 | let mut r = (64..0).inc_by::<16>(); 197 | assert_eq!(r.next(), None); 198 | } 199 | 200 | #[test] 201 | fn inc_by_inclusive_wrong() { 202 | let mut r = (64..=0).inc_by::<16>(); 203 | assert_eq!(r.next(), None); 204 | } 205 | 206 | #[test] 207 | fn dec_by_exclusive_wrong() { 208 | let mut r = (0..64).dec_by::<16>(); 209 | assert_eq!(r.next(), None); 210 | } 211 | 212 | #[test] 213 | fn dec_by_inclusive_wrong() { 214 | let mut r = (0..=64).dec_by::<16>(); 215 | assert_eq!(r.next(), None); 216 | } 217 | 218 | #[test] 219 | fn inc_by_exclusive_step_too_big() { 220 | let mut r = (0..64).inc_by::<256>(); 221 | assert_eq!(r.next(), Some(0)); 222 | assert_eq!(r.next(), None); 223 | } 224 | 225 | #[test] 226 | fn inc_by_inclusive_step_too_big() { 227 | let mut r = (0..=64).inc_by::<256>(); 228 | assert_eq!(r.next(), Some(0)); 229 | assert_eq!(r.next(), None); 230 | } 231 | 232 | #[test] 233 | fn dec_by_exclusive_step_too_big() { 234 | let mut r = (64..0).dec_by::<256>(); 235 | assert_eq!(r.next(), Some(64)); 236 | assert_eq!(r.next(), None); 237 | } 238 | 239 | #[test] 240 | fn dec_by_inclusive_step_too_big() { 241 | let mut r = (64..=0).dec_by::<256>(); 242 | assert_eq!(r.next(), Some(64)); 243 | assert_eq!(r.next(), None); 244 | } 245 | 246 | #[test] 247 | fn dec_by_exclusive_negative_end_even_step() { 248 | let mut r = (64isize..-64isize).dec_by::<16>(); 249 | assert_eq!(r.next(), Some(64)); 250 | assert_eq!(r.next(), Some(48)); 251 | assert_eq!(r.next(), Some(32)); 252 | assert_eq!(r.next(), Some(16)); 253 | assert_eq!(r.next(), Some(0)); 254 | assert_eq!(r.next(), Some(-16)); 255 | assert_eq!(r.next(), Some(-32)); 256 | assert_eq!(r.next(), Some(-48)); 257 | assert_eq!(r.next(), None); 258 | } 259 | 260 | #[test] 261 | fn dec_by_inclusive_negative_end_even_step() { 262 | let mut r = (64isize..=-64isize).dec_by::<16>(); 263 | assert_eq!(r.next(), Some(64)); 264 | assert_eq!(r.next(), Some(48)); 265 | assert_eq!(r.next(), Some(32)); 266 | assert_eq!(r.next(), Some(16)); 267 | assert_eq!(r.next(), Some(0)); 268 | assert_eq!(r.next(), Some(-16)); 269 | assert_eq!(r.next(), Some(-32)); 270 | assert_eq!(r.next(), Some(-48)); 271 | assert_eq!(r.next(), Some(-64)); 272 | assert_eq!(r.next(), None); 273 | } 274 | 275 | #[test] 276 | fn dec_by_exclusive_negative_end_uneven_step() { 277 | // This should be the same as the inclusive version below, based on how many times 278 | // the value of `STEP` fits between `self.start` and `self.end` in this case. 279 | let mut r = (64isize..-64isize).dec_by::<27>(); 280 | assert_eq!(r.next(), Some(64)); 281 | assert_eq!(r.next(), Some(37)); 282 | assert_eq!(r.next(), Some(10)); 283 | assert_eq!(r.next(), Some(-17)); 284 | assert_eq!(r.next(), Some(-44)); 285 | assert_eq!(r.next(), None); 286 | } 287 | 288 | #[test] 289 | fn dec_by_inclusive_negative_end_uneven_step() { 290 | let mut r = (64isize..=-64isize).dec_by::<27>(); 291 | assert_eq!(r.next(), Some(64)); 292 | assert_eq!(r.next(), Some(37)); 293 | assert_eq!(r.next(), Some(10)); 294 | assert_eq!(r.next(), Some(-17)); 295 | assert_eq!(r.next(), Some(-44)); 296 | assert_eq!(r.next(), None); 297 | } 298 | 299 | #[test] 300 | fn inc_by_exclusive_char_range() { 301 | let mut r = ('A'..'G').inc_by::<2>(); 302 | assert_eq!(r.next(), Some('A')); 303 | assert_eq!(r.next(), Some('C')); 304 | assert_eq!(r.next(), Some('E')); 305 | assert_eq!(r.next(), None); 306 | } 307 | 308 | #[test] 309 | fn inc_by_inclusive_char_range() { 310 | let mut r = ('A'..='G').inc_by::<2>(); 311 | assert_eq!(r.next(), Some('A')); 312 | assert_eq!(r.next(), Some('C')); 313 | assert_eq!(r.next(), Some('E')); 314 | assert_eq!(r.next(), Some('G')); 315 | assert_eq!(r.next(), None); 316 | } 317 | 318 | #[test] 319 | fn dec_by_exclusive_char_range() { 320 | let mut r = ('G'..'A').dec_by::<2>(); 321 | assert_eq!(r.next(), Some('G')); 322 | assert_eq!(r.next(), Some('E')); 323 | assert_eq!(r.next(), Some('C')); 324 | assert_eq!(r.next(), None); 325 | } 326 | 327 | #[test] 328 | fn dec_by_inclusive_char_range() { 329 | let mut r = ('G'..='A').dec_by::<2>(); 330 | assert_eq!(r.next(), Some('G')); 331 | assert_eq!(r.next(), Some('E')); 332 | assert_eq!(r.next(), Some('C')); 333 | assert_eq!(r.next(), Some('A')); 334 | assert_eq!(r.next(), None); 335 | } 336 | 337 | #[test] 338 | fn inc_by_exclusive_type_dependent_overflow() { 339 | let mut r1 = (248u8..255u8).inc_by::<50>(); 340 | assert_eq!(r1.next(), Some(248)); 341 | assert_eq!(r1.next(), None); 342 | let mut r2 = (248u32..255u32).inc_by::<{ usize::MAX }>(); 343 | assert_eq!(r2.next(), Some(248)); 344 | assert_eq!(r2.next(), None); 345 | let mut r3 = (248i32..255i32).inc_by::<{ u32::MAX as usize }>(); 346 | assert_eq!(r3.next(), Some(248)); 347 | assert_eq!(r3.next(), None); 348 | } 349 | 350 | #[test] 351 | fn inc_by_inclusive_type_dependent_overflow() { 352 | let mut r4 = (248u8..=255u8).inc_by::<50>(); 353 | assert_eq!(r4.next(), Some(248)); 354 | assert_eq!(r4.next(), None); 355 | let mut r5 = (248u32..=255u32).inc_by::<{ usize::MAX }>(); 356 | assert_eq!(r5.next(), Some(248)); 357 | assert_eq!(r5.next(), None); 358 | let mut r6 = (248i32..=255i32).inc_by::<{ u32::MAX as usize }>(); 359 | assert_eq!(r6.next(), Some(248)); 360 | assert_eq!(r6.next(), None); 361 | } 362 | 363 | #[test] 364 | fn dec_by_exclusive_type_dependent_overflow() { 365 | let mut r7 = (255u8..248u8).dec_by::<50>(); 366 | assert_eq!(r7.next(), Some(255)); 367 | assert_eq!(r7.next(), None); 368 | let mut r8 = (255u32..248u32).dec_by::<{ usize::MAX }>(); 369 | assert_eq!(r8.next(), Some(255)); 370 | assert_eq!(r8.next(), None); 371 | let mut r9 = (255i32..248i32).dec_by::<{ u32::MAX as usize }>(); 372 | assert_eq!(r9.next(), Some(255)); 373 | assert_eq!(r9.next(), None); 374 | let mut r10 = (i16::MAX..i16::MAX).dec_by::<1>(); 375 | assert_eq!(r10.next(), Some(32767)); 376 | assert_eq!(r10.next(), None); 377 | } 378 | 379 | #[test] 380 | fn dec_by_inclusive_type_dependent_overflow() { 381 | let mut r11 = (255u8..=248u8).dec_by::<50>(); 382 | assert_eq!(r11.next(), Some(255)); 383 | assert_eq!(r11.next(), None); 384 | let mut r12 = (255u32..=248u32).dec_by::<{ usize::MAX }>(); 385 | assert_eq!(r12.next(), Some(255)); 386 | assert_eq!(r12.next(), None); 387 | let mut r13 = (255i32..=248i32).dec_by::<{ u32::MAX as usize }>(); 388 | assert_eq!(r13.next(), Some(255)); 389 | assert_eq!(r13.next(), None); 390 | let mut r14 = (i16::MAX..=i16::MAX).dec_by::<1>(); 391 | assert_eq!(r14.next(), Some(32767)); 392 | assert_eq!(r14.next(), None); 393 | } 394 | --------------------------------------------------------------------------------