-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Tracking Issue for slice::array_windows #75027
Comments
Updated issue to rust-lang#75027 Update to rm oob access And hopefully fix docs as well Fixed naming conflict in test Fix test which used 1-indexing Nth starts from 0, woops Fix a bunch of off by 1 errors See https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=757b311987e3fae1ca47122969acda5a Add even more off by 1 errors And also write `next` and `next_back` in terms of `nth` and `nth_back`. Run fmt Fix forgetting to change fn name in test add nth_back test & document unsafe Remove as_ref().unwrap() Documented occurrences of unsafe, noting what invariants are maintained
Add array_windows fn This mimicks the functionality added by array_chunks, and implements a const-generic form of `windows`. It makes egregious use of `unsafe`, but by necessity because the array must be re-interpreted as a slice of arrays, and unlike array_chunks this cannot be done by casting the original array once, since each time the index is advanced it needs to move one element, not `N`. I'm planning on adding more tests, but this should be good enough as a premise for the functionality. Notably: should there be more functions overwritten for the iterator implementation/in general? ~~I've marked the issue as rust-lang#74985 as there is no corresponding exact issue for `array_windows`, but it's based of off `array_chunks`.~~ Edit: See Issue rust-lang#75027 created by @lcnr for tracking issue ~~Do not merge until I add more tests, please.~~ r? @lcnr
In the docs it says:
I'm a bit confused what that refers to; it's the first use of "array" in that section of the docs. Perhaps there's a way to phrase that more clearly? |
@yoshuawuyts see #76827 😆 |
Is it now possible to forbid N==0 like this? #![feature(
const_evaluatable_checked,
const_generics,
const_panic,
)]
#![allow(incomplete_features)]
const fn nonzero(n: usize) -> usize {
assert!(n > 0);
n
}
fn foo<const N: usize>() -> [u8; nonzero(N)] {
[0; nonzero(N)]
}
fn main() {
foo::<5>();
} |
yes, this is possible, it does require us to use `const_evaluatable_checked' in the standard library though. That's not yet something I feel comfortable with |
Bikeshed: Would |
@the8472 While it's true that Additionally, if there are any better name suggestions for these const generic equivalents, we need change |
Is compile time check of N == 0 a real problem ? I expect LLVM to remove this check for free. The only thing I could think of is we consider it's breaking change to add the compile time check in the future but I hardly can imagine break someone workflow with that, cause it's a hard panic anyway, user code would be considered totally bogus ? But we could decide this stay an assert forever if LLVM optimize it. Maybe replace the panic by an abort ? Would be amazing to be able to write: |
That's beside the point. The point is spotting at compile-time some wrong code. |
I know, of course it would be better, but my point is how many time we need to wait to be able to have this on stable ? My point is, could we have the panic (or abort) version and then have the compile time check later ? would we consider it breaking change (I don't know any guideline about breaking change on const generic) ? I just ask open question, I don't really have a strong need or opinion for this anyway. And As I said, could we just... live with it ? The current main problem of windows is the slice forcing to use try_from or use index, the array_windows is already way better than |
I used It'd be neat if once we merge ExamplesUsage example of fn parse1(input: &str) -> usize {
let lines: Vec<u16> = input.lines().map(|line| line.parse().unwrap()).collect();
lines.array_windows().filter(|[n1, n2]| n2 > n1).count()
} Usage example of fn parse1(input: &str) -> usize {
input
.lines()
.map(|line| line.parse::<u16>().unwrap())
.array_windows()
.filter(|[n1, n2]| n2 > n1)
.count()
} |
I'm noticing some missing |
I'm getting the following error. I'm wondering if it's related to @steffahn's comment, since I can drop-in my own windowing iterator and everything compiles fine.
|
can't we have a mut version ? |
Not with the To do this properly, we'd need an iterator which enforces that the references returned by |
Indeed this need GATs.
That a bold statement, personally I recently used |
Methods updated: * `array_windows` * `array_chunks` * `array_chunks_mut` * `as_chunks` * `as_chunks_mut` * `as_chunks_unchecked` * `as_rchunks` * `as_rchunks_mut` * `as_chunks_unchecked_mut` I implemented this using compile time assertions. Example compilation error: ``` > rustc +stage1 .\improper_array_windows.rs --crate-type=rlib error[E0080]: evaluation of `core::slice::<impl [u32]>::array_windows::<0>::{constant#0}` failed --> J:\rust_lang\library\core\src\slice\mod.rs:1381:13 | 1381 | assert!(N != 0, "window size must be non-zero"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'window size must be non-zero', J:\rust_lang\library\core\src\slice\mod.rs:1381:13 | = note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) note: the above error was encountered while instantiating `fn core::slice::<impl [u32]>::array_windows::<0>` --> .\improper_array_windows.rs:4:14 | 4 | for _ in s.array_windows::<0>(){ | ^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to previous error ``` I also added doctests and adjusted documentation. Relates: rust-lang#74985 rust-lang#75027
The feature gate for the issue is
#![feature(array_windows)]
.About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also uses as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Steps
N
to values greater than 0 at compile time instead of using a runtime panic.Unresolved Questions
Implementation history
The text was updated successfully, but these errors were encountered: