Skip to content

libcore: Add range_step and range_rev functions. #4313

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

huonw
Copy link
Member

@huonw huonw commented Dec 30, 2012

Per #1817

Counting-down range is named range_rev rather than rrange to distinguish it from plain range more (obviously this can change).

@graydon
Copy link
Contributor

graydon commented Jan 2, 2013

r+, though could probably use a handful of tests to guard against the ever-present "whoops, got my boundary conditions slightly wrong" bugs. I can write those if you don't feel like it though.

Thanks!

@huonw
Copy link
Member Author

huonw commented Jan 2, 2013

Yeah, I wasn't sure where they should go (i.e. at the bottom of the file or in the tests directory), are there any rules/conventions about this?

(Also, I'm away for a week or so, and won't be able to do anything, so feel free to add the tests.)

Graydon Hoare [email protected] wrote:

r+, though could probably use a handful of tests to guard against the
ever-present "whoops, got my boundary conditions slightly wrong" bugs.
I can write those if you don't feel like it though.

Thanks!


Reply to this email directly or view it on GitHub:
#4313 (comment)

@brson
Copy link
Contributor

brson commented Jan 9, 2013

@dbaupp library tests go at the bottom of the file, language tests go in the tests directory.

@huonw
Copy link
Member Author

huonw commented Jan 10, 2013

@graydon Added some tests, and found that I had met the "'negative' unsigned numbers act strangely" bug.

Previously, calling for uint::range_step(10, 0, -1) |_| { .. } didn't iterate at all (the expected behaviour is iterating over 10,9,...,1), because -1 > 0 for unsigned numbers and so it didn't detect that it should be iterating downwards. Fixed by breaking out the single range_step function into 2 functions iterating in a specific direction.

I'm slightly uncomfortable about having a different API for signed vs unsigned (i.e. range_step vs range_step_up & range_step_down), but I'm not sure there is a better resolution.

…unctions.

Splits the range_step function into the two directions (up, low -> high,
and down, high -> low) for the uint types, since there is no way to have
`step < 0` for a backwards range.
@brson
Copy link
Contributor

brson commented Jan 14, 2013

@dbaupp Thanks.

@graydon What do you think of these API changes? Is it ok to have two functions here? We could also just use int for all the step values.

@graydon
Copy link
Contributor

graydon commented Jan 15, 2013

Yeah, I'd just take an int for the step and have one variant.

@huonw
Copy link
Member Author

huonw commented Jan 17, 2013

I'm unsure if it should be int (or i64?) or the equivalently sized signed type (i.e. i8 for u8). The former leads to either inexpressible increments for u64 (or inefficiencies, for i64) on 32-bit platforms (is this a concern?) and the latter ends up cluttering up the inst modules with a T_SIGNED type for just this purpose.

(Unless there is already a technique to get the type iN from uN already?)

@bstrie
Copy link
Contributor

bstrie commented Jan 22, 2013

A whole new function for range_rev feels unnecessary. Can't range just count down rather than up when its first argument is greater than its second? i.e. have it count over the range [first..second) rather than [lo..hi).

@brson
Copy link
Contributor

brson commented Jan 23, 2013

I vote for adding T_SIGNED and making the step value a signed type of the equivalent size. I'm not sure about range_rev. #1817 suggested to make range also iterate backwards but notes that python's range function doesn't automatically count backwards without a step of -1.

@bstrie
Copy link
Contributor

bstrie commented Jan 23, 2013

Considering Python's precedent, I vote for the following functions:

int::range(lo: int, high: int);
int::range_step(start: int, end: int, step: int);
uint::range(lo: uint, high: uint);
uint::range_step(start: uint, end: uint, step: int);

Where reverse is just achieved by providing a negative step value to range_step, and the final element is always omitted.

@catamorphism
Copy link
Contributor

I'm adding the T_SIGNED type and making the simplification @brson suggested. Waiting for tests to run.

@graydon
Copy link
Contributor

graydon commented Jan 24, 2013

Yeah. +1, these are convenience functions to be used casually; step will almost always be 1, 2, -1, -2 or a similarly small number. Iterating by MAXINT-ish steps is rare and probably best done manually or by a user-written loop / iterator.

@catamorphism
Copy link
Contributor

Merged at last! 982cf90

@catamorphism
Copy link
Contributor

(Oh, and thanks to @dbaupp :-)

@huonw huonw deleted the range_rev_and_step branch March 24, 2013 10:43
bors pushed a commit to rust-lang-ci/rust that referenced this pull request May 15, 2021
A code like

```rust
extern "C" {
    fn f() {
        fn g() {}
    }
}
```

is incorrect and does not compile. Today rustfmt formats this in a way
that is correct:

```rust
extern "C" {
    fn f();
}
```

But this loses information, and doesn't have to be done because we know
the content of the block if it is present. During development I don't
think rustfmt should drop the block in this context.

Closes rust-lang#4313
RalfJung added a commit to RalfJung/rust that referenced this pull request May 17, 2025
test suite: use CARGO_TARGET_TMPDIR for temporary build artifacts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants