Struct shades::LoopScope[][src]

pub struct LoopScope<R>(_);

A special kind of EscapeScope that can also break loops.

Implementations

impl<R> LoopScope<R> where
    Return: From<R>, 
[src]

pub fn loop_continue(&mut self)[src]

Break the current iteration of the nearest loop and continue to the next iteration.

Examples

use shades::CanEscape as _;

s.loop_while(true, |s| {
  s.loop_continue();
});

pub fn loop_break(&mut self)[src]

Break the nearest loop.

Examples

use shades::CanEscape as _;

s.loop_while(true, |s| {
  s.loop_break();
});

Methods from Deref<Target = EscapeScope<R>>

pub fn leave(&mut self, ret: impl Into<R>)[src]

Early-return the current function with an expression.

Examples

use shades::{CanEscape as _, Expr, Scope};

let _fun = s.fun(|s: &mut Scope<Expr<i32>>, arg: Expr<i32>| {
  // if arg is less than 10, early-return with 0
  s.when(arg.lt(10), |s| {
    s.leave(0);
  });

  arg
});

pub fn abort(&mut self)[src]

Early-abort the current function.

Examples

use shades::{CanEscape as _, Expr, Scope};

let _fun = s.fun(|s: &mut Scope<()>, arg: Expr<i32>| {
  // if arg is less than 10, early-return with 0
  s.when(arg.lt(10), |s| {
    s.abort();
  });

  // do something else…
});

Methods from Deref<Target = Scope<R>>

pub fn var<T>(&mut self, init_value: impl Into<Expr<T>>) -> Var<T> where
    T: ToType
[src]

Bind an expression to a variable in the current scope.

let v = s.var(e); binds the e expression to v in the s Scope<T>, and e must have type Expr<T> and v must be a Var<T>, with T: ToType.

Return

The resulting Var<T> contains the representation of the binding in the EDSL and the actual binding is recorded in the current scope.

Examples

let v = s.var(3.1415); // assign the literal 3.1415 to v
let q = s.var(v * 2.); // assign v * 2. to q

pub fn loop_for<T>(
    &mut self,
    init_value: impl Into<Expr<T>>,
    condition: impl FnOnce(&Expr<T>) -> Expr<bool>,
    iter_fold: impl FnOnce(&Expr<T>) -> Expr<T>,
    body: impl FnOnce(&mut LoopScope<R>, &Expr<T>)
) where
    T: ToType
[src]

For looping statement — for.

s.loop_for(i, |i| /* cond */, |i| /* fold */, |i| /* body */ ) inserts a looping statement into the EDSL representing a typical “for” loop. i is an Expr<T> satisfying T: ToType and is used as initial value.

In all the following closures, i refers to the initial value.

The first cond closure must return an Expr<bool>, representing the condition that is held until the loop exits. The second fold closure is a pure computation that must return an Expr<T> and that will be evaluated at the end of each iteration before the next check on cond. The last and third body closure is the body of the loop.

The behaviors of the first two closures is important to understand. Those are akin to filtering and folding. The closure returning the Expr<bool> is given the Expr<T> at each iteration and the second closure creates the new Expr<T> for the next iteration. Normally, people are used to write this pattern as i++, for instance, but in the case of our EDSL, it is more akin go i + 1, and this value is affected to a local accumulator hidden from the user.

The LoopScope<R> argument to the body closure is a specialization of Scope<R> that allows breaking out of loops.

Examples

use shades::{CanEscape as _, LoopScope, Scope, ShaderBuilder};

ShaderBuilder::new_vertex_shader(|mut s, vertex| {
  s.main_fun(|s: &mut Scope<()>| {
    s.loop_for(0, |i| i.lt(10), |i| i + 1, |s: &mut LoopScope<()>, i| {
      s.when(i.eq(5), |s: &mut LoopScope<()>| {
        // when i == 5, abort from the main function
        s.abort();
      });
    });
  })
});

pub fn loop_while(
    &mut self,
    condition: impl Into<Expr<bool>>,
    body: impl FnOnce(&mut LoopScope<R>)
)
[src]

While looping statement — while.

s.loop_while(cond, body) inserts a looping statement into the EDSL representing a typical “while” loop.

cond is an Expr<bool>, representing the condition that is held until the loop exits. body is the content the loop will execute at each iteration.

The LoopScope<R> argument to the body closure is a specialization of Scope<R> that allows breaking out of loops.

Examples

use shades::{CanEscape as _, LoopScope, Scope, ShaderBuilder};

ShaderBuilder::new_vertex_shader(|mut s, vertex| {
  s.main_fun(|s: &mut Scope<()>| {
    let i = s.var(10);

    s.loop_while(i.lt(10), |s| {
      s.set(&i, &i + 1);
    });
  })
});

pub fn set<T>(&mut self, var: impl Into<Var<T>>, value: impl Into<Expr<T>>)[src]

Mutate a variable in the current scope.

Examples

let v = s.var(1); // v = 1
s.set(&v, 10); // v = 10

Trait Implementations

impl<R> CanEscape<R> for LoopScope<R> where
    Return: From<R>, 
[src]

type InnerScope = LoopScope<R>

Scope type inside the scope of the conditional.

impl<R: Debug> Debug for LoopScope<R>[src]

impl<R> Deref for LoopScope<R>[src]

type Target = EscapeScope<R>

The resulting type after dereferencing.

impl<R> DerefMut for LoopScope<R>[src]

impl<R> From<LoopScope<R>> for Scope<R>[src]

Auto Trait Implementations

impl<R> RefUnwindSafe for LoopScope<R> where
    R: RefUnwindSafe
[src]

impl<R> Send for LoopScope<R> where
    R: Send
[src]

impl<R> Sync for LoopScope<R> where
    R: Sync
[src]

impl<R> Unpin for LoopScope<R> where
    R: Unpin
[src]

impl<R> UnwindSafe for LoopScope<R> where
    R: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.