Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(examples): make demos be more consistent #3138

Merged
merged 9 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions examples/async-custom-entry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
name = "demo-async-custom-entry"
version = "0.1.0"
authors.workspace = true
edition = "2021"
license = "GPL-3.0"
workspace = "../../"
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
parity-scale-codec = { workspace = true, features = ["derive"] }
gstd.workspace = true
parity-scale-codec.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true

[dev-dependencies]
gtest.workspace = true

[lib]

[features]
debug = ["gstd/debug"]
std = ["parity-scale-codec/std"]
Expand Down
31 changes: 1 addition & 30 deletions examples/async-custom-entry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,4 @@ mod code {
pub use code::WASM_BINARY_OPT as WASM_BINARY;

#[cfg(not(feature = "std"))]
mod wasm {
use gstd::{msg, ActorId};

static mut USER: ActorId = ActorId::zero();

#[gstd::async_init(handle_reply = my_handle_reply, handle_signal = my_handle_signal)]
async fn init() {
gstd::Config::set_system_reserve(10_000_000_000).expect("Failed to set system reserve");

unsafe { USER = msg::source() }
}

#[gstd::async_main]
async fn main() {
#[allow(clippy::empty_loop)]
loop {}
}

fn my_handle_reply() {
unsafe {
msg::send_bytes(USER, b"my_handle_reply", 0).unwrap();
}
}

fn my_handle_signal() {
unsafe {
msg::send_bytes(USER, b"my_handle_signal", 0).unwrap();
}
}
}
mod wasm;
46 changes: 46 additions & 0 deletions examples/async-custom-entry/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is part of Gear.

// Copyright (C) 2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use gstd::{msg, ActorId};

static mut USER: ActorId = ActorId::zero();

#[gstd::async_init(handle_reply = my_handle_reply, handle_signal = my_handle_signal)]
async fn init() {
gstd::Config::set_system_reserve(10_000_000_000).expect("Failed to set system reserve");

unsafe { USER = msg::source() }
}

#[gstd::async_main]
async fn main() {
#[allow(clippy::empty_loop)]
loop {}
}

fn my_handle_reply() {
unsafe {
msg::send_bytes(USER, b"my_handle_reply", 0).unwrap();
}
}

fn my_handle_signal() {
unsafe {
msg::send_bytes(USER, b"my_handle_signal", 0).unwrap();
}
}
7 changes: 4 additions & 3 deletions examples/async-init/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
workspace = "../../"
homepage.workspace = true
repository.workspace = true

shamilsan marked this conversation as resolved.
Show resolved Hide resolved
[dependencies]
gstd.workspace = true
parity-scale-codec = { workspace = true, features = ["derive"] }
futures = { workspace = true, features = ["alloc"] }
parity-scale-codec.workspace = true
futures.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true
Expand Down
72 changes: 1 addition & 71 deletions examples/async-init/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,74 +57,4 @@ impl InputArgs {
}

#[cfg(not(feature = "std"))]
mod wasm {
/* The program demonstrates asynchronous execution and
* how to use macros `gstd::async_init`/`gstd::async_main`.
*
* `Init` method gets three addresses, sends "PING" messages
* to them and waits for at least two replies with any payload ("approvals").
*
* `Handle` processes only "PING" messages. When `handle` gets such message
* it sends empty requests to the three addresses and waits for just one approval.
* If an approval is obtained the method replies with "PONG".
*/

use crate::InputArgs;
use futures::future;
use gstd::{msg, prelude::*, ActorId};

// One of the addresses supposed to be non-program.
static mut ARGUMENTS: InputArgs = InputArgs {
approver_first: ActorId::zero(),
approver_second: ActorId::zero(),
approver_third: ActorId::zero(),
};

static mut RESPONSES: u8 = 0;

#[gstd::async_init]
async fn init() {
let arguments: InputArgs = msg::load().expect("Failed to load arguments");

let mut requests = arguments
.iter()
.map(|&addr| {
msg::send_bytes_for_reply(addr, "PING", 0, 0).expect("Failed to send message")
})
.collect::<Vec<_>>();

unsafe {
ARGUMENTS = arguments;
}

while !requests.is_empty() {
let (.., remaining) = future::select_all(requests).await;
unsafe {
RESPONSES += 1;
}

if unsafe { RESPONSES } >= 2 {
break;
}

requests = remaining;
}
}

#[gstd::async_main]
async fn main() {
let message = msg::load_bytes().expect("Failed to load bytes");

assert_eq!(message, b"PING");

let requests = unsafe { ARGUMENTS.iter() }
.map(|&addr| {
msg::send_bytes_for_reply(addr, "PING", 0, 0).expect("Failed to send message")
})
.collect::<Vec<_>>();

let _ = future::select_all(requests).await;

msg::reply(unsafe { RESPONSES }, 0).expect("Failed to send reply");
}
}
mod wasm;
82 changes: 82 additions & 0 deletions examples/async-init/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// This file is part of Gear.

// Copyright (C) 2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! The program demonstrates asynchronous execution and
//! how to use macros `gstd::async_init`/`gstd::async_main`.
//!
//! `Init` method gets three addresses, sends "PING" messages
//! to them and waits for at least two replies with any payload ("approvals").
//!
//! `Handle` processes only "PING" messages. When `handle` gets such message
//! it sends empty requests to the three addresses and waits for just one approval.
//! If an approval is obtained the method replies with "PONG".

use crate::InputArgs;
use futures::future;
use gstd::{msg, prelude::*, ActorId};

// One of the addresses supposed to be non-program.
static mut ARGUMENTS: InputArgs = InputArgs {
approver_first: ActorId::zero(),
approver_second: ActorId::zero(),
approver_third: ActorId::zero(),
};

static mut RESPONSES: u8 = 0;

#[gstd::async_init]
async fn init() {
let arguments: InputArgs = msg::load().expect("Failed to load arguments");

let mut requests = arguments
.iter()
.map(|&addr| msg::send_bytes_for_reply(addr, "PING", 0, 0).expect("Failed to send message"))
.collect::<Vec<_>>();

unsafe {
ARGUMENTS = arguments;
}

while !requests.is_empty() {
let (.., remaining) = future::select_all(requests).await;
unsafe {
RESPONSES += 1;
}

if unsafe { RESPONSES } >= 2 {
break;
}

requests = remaining;
}
}

#[gstd::async_main]
async fn main() {
let message = msg::load_bytes().expect("Failed to load bytes");

assert_eq!(message, b"PING");

let requests = unsafe { ARGUMENTS.iter() }
.map(|&addr| msg::send_bytes_for_reply(addr, "PING", 0, 0).expect("Failed to send message"))
.collect::<Vec<_>>();

let _ = future::select_all(requests).await;

msg::reply(unsafe { RESPONSES }, 0).expect("Failed to send reply");
}
3 changes: 2 additions & 1 deletion examples/async-recursion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
workspace = "../../"
homepage.workspace = true
repository.workspace = true

[dependencies]
gstd.workspace = true
Expand Down
37 changes: 1 addition & 36 deletions examples/async-recursion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,4 @@ mod code {
pub use code::WASM_BINARY_OPT as WASM_BINARY;

#[cfg(not(feature = "std"))]
mod wasm {
use async_recursion::async_recursion;
use gstd::{msg, prelude::*, ActorId};

static mut DESTINATION: ActorId = ActorId::zero();

#[no_mangle]
extern "C" fn init() {
let destination = msg::load().expect("Failed to load destination");
unsafe { DESTINATION = destination };
}

/// Send message "PING" and wait for a reply, then recursively
/// repeat with `val` decreased by reply len while `val` > reply len.
#[async_recursion]
async fn rec_func(val: i32) {
let reply = msg::send_bytes_for_reply(unsafe { DESTINATION }, "PING", 0, 0)
.expect("Failed to send message")
.await
.expect("Received error reply");

msg::send(msg::source(), val, 0).expect("Failed to send message");

let reply_len = reply.len() as i32;

if val - reply_len > 0 {
rec_func(val - reply_len).await;
}
}

#[gstd::async_main]
async fn main() {
let arg = msg::load().expect("Failed to load argument");
rec_func(arg).await;
}
}
mod wasm;
52 changes: 52 additions & 0 deletions examples/async-recursion/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This file is part of Gear.

// Copyright (C) 2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use async_recursion::async_recursion;
use gstd::{msg, prelude::*, ActorId};

static mut DESTINATION: ActorId = ActorId::zero();

#[no_mangle]
extern "C" fn init() {
let destination = msg::load().expect("Failed to load destination");
unsafe { DESTINATION = destination };
}

/// Send message "PING" and wait for a reply, then recursively
/// repeat with `val` decreased by reply len while `val` > reply len.
#[async_recursion]
async fn rec_func(val: i32) {
let reply = msg::send_bytes_for_reply(unsafe { DESTINATION }, "PING", 0, 0)
.expect("Failed to send message")
.await
.expect("Received error reply");

msg::send(msg::source(), val, 0).expect("Failed to send message");

let reply_len = reply.len() as i32;

if val - reply_len > 0 {
rec_func(val - reply_len).await;
}
}

#[gstd::async_main]
async fn main() {
let arg = msg::load().expect("Failed to load argument");
rec_func(arg).await;
}
Loading