Skip to content

Commit 26d3585

Browse files
authored
feat: add to_string and log that doesn't require a passed env (#180)
* feat: add to_string and log that doesn't require a passed `env` * fix: add usages to test if it compiles * fix: fmt * fix: clippy
1 parent dd1c9f6 commit 26d3585

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

crates/loam-sdk/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ macro_rules! map {
1919
soroban_sdk::map![soroban_sdk::env(), $($tokens)*]
2020
};
2121
}
22+
23+
#[cfg(feature = "loam-soroban-sdk")]
24+
#[macro_export]
25+
macro_rules! log {
26+
($($tokens:tt)*) => {
27+
{
28+
use soroban_sdk::Env;
29+
soroban_sdk::log![soroban_sdk::env(), $($tokens)*];
30+
}
31+
};
32+
}

crates/loam-soroban-sdk/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![no_std]
22

3+
use soroban_sdk::unwrap::UnwrapOptimized;
34
pub use soroban_sdk::*;
5+
46
pub mod into_key;
57
pub mod loam_storage;
68

@@ -20,6 +22,12 @@ pub fn set_env(env: Env) {
2022
unsafe { ENV = Some(env) };
2123
}
2224

25+
/// Utility to cast a `&str` to a `String`.
26+
#[must_use]
27+
pub fn to_string(s: &str) -> String {
28+
soroban_sdk::String::from_str(env(), s)
29+
}
30+
2331
/// Returns a reference to the current environment.
2432
///
2533
/// # Panics
@@ -29,8 +37,9 @@ pub fn set_env(env: Env) {
2937
/// function is called in normal operation.
3038
#[must_use]
3139
#[allow(static_mut_refs)]
40+
#[inline]
3241
pub fn env() -> &'static Env {
33-
unsafe { ENV.as_ref().unwrap() }
42+
unsafe { ENV.as_ref().unwrap_optimized() }
3443
}
3544

3645
impl<T> Lazy for T

examples/soroban/status_message/src/status_message.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![allow(deprecated)]
2-
32
// Currently need to import `self` because `contracttype` expects it in the namespace
43
use loam_sdk::{
5-
soroban_sdk::{self, contracttype, env, Address, IntoKey, Lazy, Map, String},
4+
log,
5+
soroban_sdk::{self, contracttype, env, to_string, Address, IntoKey, Lazy, Map, String},
66
subcontract,
77
};
88

@@ -19,10 +19,8 @@ impl Default for StatusMessage {
1919
#[subcontract]
2020
pub trait IsPostable {
2121
/// Documentation ends up in the contract's metadata and thus the CLI, etc
22-
fn messages_get(
23-
&self,
24-
author: loam_sdk::soroban_sdk::Address,
25-
) -> Option<loam_sdk::soroban_sdk::String>;
22+
fn messages_get(&self, author: loam_sdk::soroban_sdk::Address)
23+
-> loam_sdk::soroban_sdk::String;
2624

2725
/// Only the author can set the message
2826
fn messages_set(
@@ -33,12 +31,15 @@ pub trait IsPostable {
3331
}
3432

3533
impl IsPostable for StatusMessage {
36-
fn messages_get(&self, author: Address) -> Option<String> {
37-
self.0.get(author)
34+
fn messages_get(&self, author: Address) -> String {
35+
self.0
36+
.get(author)
37+
.unwrap_or_else(|| to_string("No message"))
3838
}
3939

4040
fn messages_set(&mut self, author: Address, text: String) {
4141
author.require_auth();
42+
log!("Setting message {} for {}", text, author);
4243
self.0.set(author, text);
4344
}
4445
}

0 commit comments

Comments
 (0)