marslang rs-0.19.1

rs-0.19.1 — async and await, programs, and broadcast channels#

Programs can now do several things at once: async and await run tasks that wait without blocking each other, HTTP requests, sleeps, and the new std.process wait inside a task while the rest of the program goes on, and std.broadcast sends messages within a program, to the other Marslang programs on a computer, or across the network.

async and await#

mars
takepkg std.async;

async func slow(string name, float seconds){
    await async.sleep(seconds);
    ret name;
}

func m{
    out(await async.all(arr(slow("a", 0.2), slow("b", 0.2))));   // ["a", "b"]
}

Both tasks sleep at the same time, so this takes 0.2 seconds, not 0.4.

Waiting without blocking#

Inside a task, every std.http request, time.sleep, and std.process run waits on its own while other tasks go on. Five slow requests take as long as the slowest, not their sum. Outside tasks, nothing changes.

std.process#

mars
takepkg std.process;

func m{
    result = process.run(arr("git", "--version"));
    out(result.ok);   // true
}

run, run_in (another folder), run_with (text for standard input), check (raises unless the program succeeded), which, and set_timeout. There is no shell: every array element is one argument, so text from a user can never become a command.

std.broadcast#

mars
takepkg std.async;
takepkg std.broadcast;

func m{
    news = broadcast.channel("scores", "local");
    func listen(){ out(await news.receive()); }
    t = async.spawn(listen);
    news.send(map().set("player", "Ada"));
    await t;   // {"player": "Ada"}
}

Channels carry any JSON value, and there are three scopes:

Receiving is a task: receive, receive_for, on, and close.

Smaller changes#

Validation#

cargo test passes 501 tests on Windows, among them tasks under the collector, 1,000 concurrent tasks, and broadcasts between two running programs. Every push runs them on Windows, macOS, and Linux on x86_64 and ARM64, and runs the static Linux binary inside Alpine, Debian 11, Ubuntu 20.04, Rocky Linux 8, and Amazon Linux 2.