Skip to content

Commit

Permalink
chore(gcore): rename with_read_on_stack -> with_read_on_stack_or_heap (
Browse files Browse the repository at this point in the history
  • Loading branch information
grishasobol authored Jan 19, 2025
1 parent f07c634 commit 4602883
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/piggy-bank/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use gstd::{debug, exec, msg};

#[unsafe(no_mangle)]
extern "C" fn handle() {
msg::with_read_on_stack(|msg| {
msg::with_read_on_stack_or_heap(|msg| {
let available_value = exec::value_available();
let value = msg::value();
debug!("inserted: {value}, total: {available_value}");
Expand Down
2 changes: 1 addition & 1 deletion examples/stack-allocations/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static mut STATE: State = State {

fn do_actions(mut actions: Vec<Action>) -> u32 {
let check_sum = match actions.pop() {
Some(Action::Read) => msg::with_read_on_stack(|payload| {
Some(Action::Read) => msg::with_read_on_stack_or_heap(|payload| {
payload
.map(|payload| payload.iter().fold(0u32, |acc, x| acc + *x as u32))
.expect("Failed to read payload")
Expand Down
6 changes: 4 additions & 2 deletions gcore/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ pub fn read(buffer: &mut [u8]) -> Result<()> {
}

/// Executes function `f` with provided message payload allocated on stack.
/// If payload size is bigger than [stack_buffer::MAX_BUFFER_SIZE], then
/// allocation will be on heap.
///
/// Returns function `f` call result `T`.
///
Expand All @@ -184,13 +186,13 @@ pub fn read(buffer: &mut [u8]) -> Result<()> {
///
/// #[unsafe(no_mangle)]
/// extern "C" fn handle() {
/// msg::with_read_on_stack(|read_res| {
/// msg::with_read_on_stack_or_heap(|read_res| {
/// let payload: &mut [u8] = read_res.expect("Unable to read");
/// // do something with `payload`
/// });
/// }
/// ```
pub fn with_read_on_stack<T>(f: impl FnOnce(Result<&mut [u8]>) -> T) -> T {
pub fn with_read_on_stack_or_heap<T>(f: impl FnOnce(Result<&mut [u8]>) -> T) -> T {
let size = size();
stack_buffer::with_byte_buffer(size, |buffer| {
let mut len = 0u32;
Expand Down
2 changes: 1 addition & 1 deletion gstd/src/msg/encoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use scale_info::scale::{Decode, Encode};
/// - [`load_bytes`](super::load_bytes) function returns a payload as a byte
/// vector.
pub fn load<D: Decode>() -> crate::errors::Result<D> {
super::with_read_on_stack(
super::with_read_on_stack_or_heap(
|read_result: Result<&mut [u8]>| -> crate::errors::Result<D> {
let mut buffer = read_result? as &[u8];
D::decode(&mut buffer).map_err(Error::Decode)
Expand Down
2 changes: 1 addition & 1 deletion gstd/src/msg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! Note that messages and a reply are not sent immediately but collected during
//! the program execution and enqueued after the execution successfully ends.
pub use gcore::msg::{id, reply_code, reply_to, size, source, value, with_read_on_stack};
pub use gcore::msg::{id, reply_code, reply_to, size, source, value, with_read_on_stack_or_heap};

#[cfg(not(feature = "ethexe"))]
pub use gcore::msg::{signal_code, signal_from};
Expand Down

0 comments on commit 4602883

Please sign in to comment.