-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): Add example returning references (#256)
- Loading branch information
Showing
9 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |