Skip to content

Commit

Permalink
feat(examples): Add example returning references (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
holykol authored May 20, 2024
1 parent ef0d636 commit 81d7eb3
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ license = "GPL-3.0"
resolver = "2"
members = [
"client-gen",
"examples/references/app",
"examples/references/wasm",
"examples/no-svcs-prog/app",
"examples/no-svcs-prog/wasm",
"examples/puppeteer/app",
Expand Down
3 changes: 3 additions & 0 deletions examples/references/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# references

This program demostrates how to return references instead of owned types
8 changes: 8 additions & 0 deletions examples/references/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "references-app"
version = "0.1.0"
edition = "2021"

[dependencies]
gstd = { workspace = true, features = ["debug"] }
sails-rtl.workspace = true
44 changes: 44 additions & 0 deletions examples/references/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![no_std]

use core::ptr::addr_of;

use gstd::prelude::*;
use sails_rtl::gstd::gservice;

static mut COUNTER: Counter = Counter { count: 0 };
static mut BYTES: Vec<u8> = Vec::new();

#[derive(Debug, Encode, Decode, TypeInfo)]
#[codec(crate = sails_rtl::scale_codec)]
#[scale_info(crate = sails_rtl::scale_info)]
pub struct Counter {
count: u32,
}

#[derive(Default)]
pub struct ReferenceService;

#[gservice]
impl ReferenceService {
pub const fn new() -> Self {
Self
}

pub fn baked(&self) -> &'static str {
"Static str!"
}

pub fn incr(&mut self) -> &'static Counter {
unsafe {
COUNTER.count += 1;
&*addr_of!(COUNTER)
}
}

pub fn add_byte(&mut self, byte: u8) -> &'static [u8] {
unsafe {
BYTES.push(byte);
&*addr_of!(BYTES)
}
}
}
14 changes: 14 additions & 0 deletions examples/references/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "references"
version = "0.1.0"
edition = "2021"

[dependencies]
gstd.workspace = true
sails-rtl.workspace = true
references-app = { path = "../app" }

[build-dependencies]
gwasm-builder.workspace = true
sails-idl-gen.workspace = true
references-app = { path = "../app" }
15 changes: 15 additions & 0 deletions examples/references/wasm/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use references_app::ReferenceService;
use sails_idl_gen::service;
use std::{env, fs::File, path::PathBuf};

fn main() {
gwasm_builder::build();

let manifest_dir_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());

let idl_file_path = manifest_dir_path.join("references.idl");

let idl_file = File::create(idl_file_path).unwrap();

service::generate_idl::<ReferenceService>(idl_file).unwrap();
}
10 changes: 10 additions & 0 deletions examples/references/wasm/references.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type Counter = struct {
count: u32,
};

service {
AddByte : (byte: u8) -> vec u8;
Incr : () -> Counter;
query Baked : () -> str;
};

14 changes: 14 additions & 0 deletions examples/references/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![no_std]

use references_app::ReferenceService;
use sails_rtl::gstd::{msg, services::Service};

#[gstd::async_main]
async fn main() {
let input_bytes = msg::load_bytes().expect("Failed to read input");
let output_bytes = ReferenceService::new()
.expose(msg::id().into(), &[1, 2, 3])
.handle(&input_bytes)
.await;
msg::reply_bytes(output_bytes, 0).expect("Failed to send output");
}

0 comments on commit 81d7eb3

Please sign in to comment.