diff --git a/luisa_compute/src/lang/control_flow.rs b/luisa_compute/src/lang/control_flow.rs index 25f0b14..ca39dfd 100644 --- a/luisa_compute/src/lang/control_flow.rs +++ b/luisa_compute/src/lang/control_flow.rs @@ -95,8 +95,8 @@ pub fn return_() { pub fn if_then_else( cond: Expr, - then: impl Fn() -> R, - else_: impl Fn() -> R, + then: impl FnOnce() -> R, + else_: impl FnOnce() -> R, ) -> R { let cond = cond.node().get(); with_recorder(|r| { @@ -199,11 +199,7 @@ pub fn select(mask: Expr, a: A, b: A) -> A { A::from_vec_nodes(ret) } -pub fn generic_loop( - mut cond: impl FnMut() -> Expr, - mut body: impl FnMut(), - mut update: impl FnMut(), -) { +pub fn generic_loop(cond: impl FnOnce() -> Expr, body: impl FnOnce(), update: impl FnOnce()) { with_recorder(|r| { let pools = r.pools.clone(); let s = &mut r.scopes; @@ -308,13 +304,13 @@ pub fn loop_(body: impl Fn()) { }); } -pub fn for_unrolled(iter: I, body: impl Fn(I::Item)) { +pub fn for_unrolled(iter: I, mut body: impl FnMut(I::Item)) { for i in iter { body(i); } } -pub fn for_range(r: R, body: impl Fn(Expr)) { +pub fn for_range(r: R, body: impl FnMut(Expr)) { let start = r.start().get(); let end = r.end().get(); let inc = |v: NodeRef| { diff --git a/luisa_compute/src/lang/ops/impls.rs b/luisa_compute/src/lang/ops/impls.rs index 0d8fd89..de4c786 100644 --- a/luisa_compute/src/lang/ops/impls.rs +++ b/luisa_compute/src/lang/ops/impls.rs @@ -403,7 +403,7 @@ impl> StoreMaybeExpr for Var { } impl SelectMaybeExpr for bool { - fn if_then_else(self, on: impl Fn() -> R, off: impl Fn() -> R) -> R { + fn if_then_else(self, on: impl FnOnce() -> R, off: impl FnOnce() -> R) -> R { if self { on() } else { @@ -419,7 +419,7 @@ impl SelectMaybeExpr for bool { } } impl SelectMaybeExpr for Expr { - fn if_then_else(self, on: impl Fn() -> R, off: impl Fn() -> R) -> R { + fn if_then_else(self, on: impl FnOnce() -> R, off: impl FnOnce() -> R) -> R { crate::lang::control_flow::if_then_else(self, on, off) } fn select(self, on: R, off: R) -> R { @@ -428,7 +428,7 @@ impl SelectMaybeExpr for Expr { } impl SelectMaybeExpr for Var { - fn if_then_else(self, on: impl Fn() -> R, off: impl Fn() -> R) -> R { + fn if_then_else(self, on: impl FnOnce() -> R, off: impl FnOnce() -> R) -> R { crate::lang::control_flow::if_then_else(**self, on, off) } fn select(self, on: R, off: R) -> R { @@ -436,20 +436,20 @@ impl SelectMaybeExpr for Var { } } impl ActivateMaybeExpr for bool { - fn activate(self, then: impl Fn()) { + fn activate(self, then: impl FnOnce()) { if self { then() } } } impl ActivateMaybeExpr for Expr { - fn activate(self, then: impl Fn()) { + fn activate(self, then: impl FnOnce()) { crate::lang::control_flow::if_then_else(self, then, || {}) } } impl ActivateMaybeExpr for Var { - fn activate(self, then: impl Fn()) { + fn activate(self, then: impl FnOnce()) { crate::lang::control_flow::if_then_else(self.load(), then, || {}) } } @@ -470,23 +470,23 @@ impl LoopMaybeExpr for Expr { impl LazyBoolMaybeExpr for bool { type Bool = bool; - fn and(self, other: impl Fn() -> bool) -> bool { + fn and(self, other: impl FnOnce() -> bool) -> bool { self && other() } - fn or(self, other: impl Fn() -> bool) -> bool { + fn or(self, other: impl FnOnce() -> bool) -> bool { self || other() } } impl LazyBoolMaybeExpr, ExprType> for bool { type Bool = Expr; - fn and(self, other: impl Fn() -> Expr) -> Self::Bool { + fn and(self, other: impl FnOnce() -> Expr) -> Self::Bool { if self { other() } else { false.expr() } } - fn or(self, other: impl Fn() -> Expr) -> Self::Bool { + fn or(self, other: impl FnOnce() -> Expr) -> Self::Bool { if self { true.expr() } else { @@ -496,21 +496,21 @@ impl LazyBoolMaybeExpr, ExprType> for bool { } impl LazyBoolMaybeExpr for Expr { type Bool = Expr; - fn and(self, other: impl Fn() -> bool) -> Self::Bool { + fn and(self, other: impl FnOnce() -> bool) -> Self::Bool { let other = other().expr(); select(self, other, false.expr()) } - fn or(self, other: impl Fn() -> bool) -> Self::Bool { + fn or(self, other: impl FnOnce() -> bool) -> Self::Bool { let other = other().expr(); select(self, true.expr(), other) } } impl LazyBoolMaybeExpr, ExprType> for Expr { type Bool = Expr; - fn and(self, other: impl Fn() -> Expr) -> Self::Bool { + fn and(self, other: impl FnOnce() -> Expr) -> Self::Bool { crate::lang::control_flow::if_then_else(self, other, || false.expr()) } - fn or(self, other: impl Fn() -> Expr) -> Self::Bool { + fn or(self, other: impl FnOnce() -> Expr) -> Self::Bool { crate::lang::control_flow::if_then_else(self, || true.expr(), other) } } diff --git a/luisa_compute/src/lang/ops/traits.rs b/luisa_compute/src/lang/ops/traits.rs index 5bb5323..216ec6c 100644 --- a/luisa_compute/src/lang/ops/traits.rs +++ b/luisa_compute/src/lang/ops/traits.rs @@ -353,12 +353,12 @@ pub trait StoreMaybeExpr { } pub trait SelectMaybeExpr { - fn if_then_else(self, on: impl Fn() -> R, off: impl Fn() -> R) -> R; + fn if_then_else(self, on: impl FnOnce() -> R, off: impl FnOnce() -> R) -> R; fn select(self, on: R, off: R) -> R; } pub trait ActivateMaybeExpr { - fn activate(self, then: impl Fn()); + fn activate(self, then: impl FnOnce()); } pub trait LoopMaybeExpr {