Skip to content

Commit

Permalink
Fuse iterators that call next multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
jongiddy committed Apr 27, 2024
1 parent 95cd0ec commit 98f5a57
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ pub fn evaluate_bool(expr: &Expr) -> Result<bool> {
pub fn evaluate_range(expr: &Expr) -> Option<&syn::ExprRange> {
match expr {
Expr::Block(block) => {
if block.block.stmts.len() == 1 {
if let Stmt::Expr(expr, _) = &block.block.stmts[0] {
return evaluate_range(expr);
}
let mut stmts = block.block.stmts.iter().fuse();
if let (Some(Stmt::Expr(expr, None)), None) = (stmts.next(), stmts.next()) {
return evaluate_range(expr);
}
}
Expr::Range(range) => {
Expand Down
8 changes: 4 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ impl<'a> TypleContext<'a> {
self.replace_expr(&mut index.expr, state)?;
if let Expr::Array(array) = &mut *index.index {
// t[[0]]
let mut iter = array.elems.iter_mut();
let mut iter = array.elems.iter_mut().fuse();
let (Some(field), None) = (iter.next(), iter.next()) else {
abort!(index.index, "unsupported tuple index");
};
Expand Down Expand Up @@ -751,7 +751,7 @@ impl<'a> TypleContext<'a> {
Expr::Path(path) => {
self.replace_attrs(&mut path.attrs)?;
if path.qself.is_none() {
let mut segments = path.path.segments.iter();
let mut segments = path.path.segments.iter().fuse();
if let Some(syn::PathSegment {
ident: ident1,
arguments: PathArguments::None,
Expand Down Expand Up @@ -936,7 +936,7 @@ impl<'a> TypleContext<'a> {
if let PathArguments::AngleBracketed(arguments) =
&first.arguments
{
let mut iter = arguments.args.iter();
let mut iter = arguments.args.iter().fuse();
let (Some(arg), None) = (iter.next(), iter.next()) else {
abort!(arguments, "expected constant expression");
};
Expand Down Expand Up @@ -1119,7 +1119,7 @@ impl<'a> TypleContext<'a> {
&mut first.arguments,
segments.next(),
) {
let mut iter = arguments.args.iter_mut();
let mut iter = arguments.args.iter_mut().fuse();
if let (Some(GenericArgument::Const(ref mut expr)), None) =
(iter.next(), iter.next())
{
Expand Down
4 changes: 3 additions & 1 deletion tests/compile/unzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ where

// A solution that does not modify the original trait is to add a new trait with
// an associated method, implement it on the tuple type, and call it from an
// impl for the original trait:
// impl for the original trait. Note that the typle impl uses the input
// types as generic parameters to select the implementation and uses associated
// types for any interface types constructed using typle macros.

pub trait TryUnzipTuple<T, E> {
type Output;
Expand Down

0 comments on commit 98f5a57

Please sign in to comment.