Trait shades::CanEscape[][src]

pub trait CanEscape<R> where
    Return: From<R>, 
{ type InnerScope; fn when<'a>(
        &'a mut self,
        condition: impl Into<Expr<bool>>,
        body: impl FnOnce(&mut Self::InnerScope)
    ) -> When<'a, R>; fn unless<'a>(
        &'a mut self,
        condition: impl Into<Expr<bool>>,
        body: impl FnOnce(&mut Self::InnerScope)
    ) -> When<'a, R> { ... } }

Scopes allowing to enter conditional scopes.

Conditional scopes allow to break out of a function by early-return / aborting the function.

Associated Types

type InnerScope[src]

Scope type inside the scope of the conditional.

Loading content...

Required methods

fn when<'a>(
    &'a mut self,
    condition: impl Into<Expr<bool>>,
    body: impl FnOnce(&mut Self::InnerScope)
) -> When<'a, R>
[src]

Conditional statement — if.

s.when(cond, |s: &mut EscapeScope<R>| { /* body */ }) inserts a conditional branch in the EDSL using the cond expression as truth and the passed closure as body to run when the represented condition is true. The EscapeScope<R> provides you with the possibility to escape and leave the function earlier, either by returning an expression or by aborting the function, depending on the value of R: Expr<_> allows for early-returns and () allows for aborting.

Return

A When<R>, authorizing the same escape rules with R. This object allows you to chain other conditional statements, commonly referred to as else if and else in common languages.

Have a look at the documentation of When for further information.

Examples

Early-return:

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

ShaderBuilder::new_vertex_shader(|mut s, vertex| {
  let f = s.fun(|s: &mut Scope<Expr<i32>>| {
    s.when(lit!(1).lt(3), |s: &mut EscapeScope<Expr<i32>>| {
      // do something in here

      // early-return with 0; only possible if the function returns Expr<i32>
      s.leave(0);
    });

    lit!(1)
  });

  s.main_fun(|s: &mut Scope<()>| {
    let x = s.var(f());
  })
});

Aborting a function:

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

ShaderBuilder::new_vertex_shader(|mut s, vertex| {
  s.main_fun(|s: &mut Scope<()>| {
    s.when(lit!(1).lt(3), |s: &mut EscapeScope<()>| {
      // do something in here

      // break the parent function by aborting; this is possible because the return type is ()
      s.abort();
    });
  })
});
Loading content...

Provided methods

fn unless<'a>(
    &'a mut self,
    condition: impl Into<Expr<bool>>,
    body: impl FnOnce(&mut Self::InnerScope)
) -> When<'a, R>
[src]

Complement form of Scope::when.

This method does the same thing as Scope::when but applies the Not::not operator on the condition first.

Loading content...

Implementors

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

type InnerScope = LoopScope<R>

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

type InnerScope = EscapeScope<R>

Loading content...