2 releases

0.1.1 Jun 6, 2020
0.1.0 Jun 4, 2020

#1204 in Procedural macros

MIT license

24KB
438 lines

Build Creates.io Docs

forward_goto

This crate provides a sound and safe way to jump from a goto forward in the control-flow to a label.

use forward_goto::rewrite_forward_goto;

#[rewrite_forward_goto]
fn decode_msg(luck: impl Fn() -> bool, is_alan_turing: bool) {
    if is_alan_turing {
        forward_goto!('turing_complete);
    }

    println!("You'll need a lot of luck for this");
    
    if luck() {
        println!("Seems you were lucky");

        forward_label!('turing_complete);

        println!("Message decoded!");
    } else {
        println!("No luck today...");
    }
}

Should you use it?

Probably not!


lib.rs:

This crate provides a sound and safe way to jump from a goto forward in the control-flow to a label.

Rust has no goto mechanism with which it is possible to arbitrarily jump through the control-flow.

This crates provides a procedural macro that allows to write gotos that can jump forward in the control-flow to a specified label.

The jump is safe in terms of the borrow-checker, but several restrictions apply. See rewrite_forward_goto for more information.

use forward_goto::rewrite_forward_goto;

#[rewrite_forward_goto]
fn decode_msg(luck: impl Fn() -> bool, is_alan_turing: bool) {
    if is_alan_turing {
        forward_goto!('turing_complete);
    }

    println!("You'll need a lot of luck for this");
    
    if luck() {
        println!("Seems you were lucky");

        forward_label!('turing_complete);

        println!("Message decoded!");
    } else {
        println!("No luck today...");
    }
}

Dependencies

~1.5MB
~37K SLoC