mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-23 22:05:31 +03:00
cbcc4c4f05
Add Peekable::next_if Prior art: `rust_analyzer` uses [`Parser::eat`](https://github.com/rust-analyzer/rust-analyzer/blob/50f4ae798b7c54d417ee88455b87fd0477473150/crates/ra_parser/src/parser.rs#L94), which is `next_if` specialized to `|y| self.next_if(|x| x == y)`. Basically every other parser I've run into in Rust has an equivalent of `Parser::eat`; see for example - [cranelift](https://github.com/bytecodealliance/wasmtime/blob/94190d57244b26baf36629c88104b0ba516510cf/cranelift/reader/src/parser.rs#L498) - [rcc](https://github.com/jyn514/rcc/blob/a8159c3904a0c950fbba817bf9109023fad69033/src/parse/mod.rs#L231) - [crunch](https://github.com/Kixiron/crunch-lang/blob/8521874fab8a7d62bfa7dea8bd1da94b63e31be8/crates/crunch-parser/src/parser/mod.rs#L213-L241) Possible extensions: A specialization of `next_if` to using `Eq::eq`. The only difficulty here is the naming - maybe `next_if_eq`? Alternatives: - Instead of `func: impl FnOnce(&I::Item) -> bool`, use `func: impl FnOnce(I::Item) -> Option<I::Item>`. This has the advantage that `func` can move the value if necessary, but means that there is no guarantee `func` will return the same value it was given. - Instead of `fn next_if(...) -> Option<I::Item>`, use `fn next_if(...) -> bool`. This makes the common case of `iter.next_if(f).is_some()` easier, but makes the unusual case impossible. Bikeshedding on naming: - `next_if` could be renamed to `consume_if` (to match `eat`, but a little more formally) - `next_if_eq` could be renamed to `consume`. This is more concise but less self-explanatory if you haven't written a lot of parsers. - Both of the above, but with `consume` replaced by `eat`.
78 lines
1.6 KiB
Rust
78 lines
1.6 KiB
Rust
#![feature(alloc_layout_extra)]
|
|
#![feature(bool_to_option)]
|
|
#![feature(bound_cloned)]
|
|
#![feature(box_syntax)]
|
|
#![feature(cell_update)]
|
|
#![feature(core_private_bignum)]
|
|
#![feature(core_private_diy_float)]
|
|
#![feature(debug_non_exhaustive)]
|
|
#![feature(dec2flt)]
|
|
#![feature(exact_size_is_empty)]
|
|
#![feature(fixed_size_array)]
|
|
#![feature(flt2dec)]
|
|
#![feature(fmt_internals)]
|
|
#![feature(hashmap_internals)]
|
|
#![feature(try_find)]
|
|
#![feature(is_sorted)]
|
|
#![feature(pattern)]
|
|
#![feature(range_is_empty)]
|
|
#![feature(raw)]
|
|
#![feature(sort_internals)]
|
|
#![feature(slice_partition_at_index)]
|
|
#![feature(specialization)]
|
|
#![feature(step_trait)]
|
|
#![feature(step_trait_ext)]
|
|
#![feature(str_internals)]
|
|
#![feature(test)]
|
|
#![feature(trusted_len)]
|
|
#![feature(try_trait)]
|
|
#![feature(inner_deref)]
|
|
#![feature(slice_internals)]
|
|
#![feature(slice_partition_dedup)]
|
|
#![feature(int_error_matching)]
|
|
#![feature(array_value_iter)]
|
|
#![feature(iter_partition_in_place)]
|
|
#![feature(iter_is_partitioned)]
|
|
#![feature(iter_order_by)]
|
|
#![feature(cmp_min_max_by)]
|
|
#![feature(iter_map_while)]
|
|
#![feature(const_slice_from_raw_parts)]
|
|
#![feature(const_raw_ptr_deref)]
|
|
#![feature(never_type)]
|
|
#![feature(unwrap_infallible)]
|
|
#![feature(leading_trailing_ones)]
|
|
#![feature(const_forget)]
|
|
#![feature(option_unwrap_none)]
|
|
#![feature(peekable_next_if)]
|
|
|
|
extern crate test;
|
|
|
|
mod alloc;
|
|
mod any;
|
|
mod array;
|
|
mod ascii;
|
|
mod atomic;
|
|
mod bool;
|
|
mod cell;
|
|
mod char;
|
|
mod clone;
|
|
mod cmp;
|
|
mod fmt;
|
|
mod hash;
|
|
mod intrinsics;
|
|
mod iter;
|
|
mod manually_drop;
|
|
mod mem;
|
|
mod nonzero;
|
|
mod num;
|
|
mod ops;
|
|
mod option;
|
|
mod pattern;
|
|
mod ptr;
|
|
mod result;
|
|
mod slice;
|
|
mod str;
|
|
mod str_lossy;
|
|
mod time;
|
|
mod tuple;
|