Skip to content

Commit 8ce917a

Browse files
mootz12Copilot
andauthored
Rename assert_in_contract to debug_assert_in_contract (#1806)
### What Rename's `assert_in_contract` to `debug_assert_in_contract`, and adds a `#[doc(hidden)]` tag. `assert_in_contract` was not deprecated as it's primarily a macro used internally. ### Why This function is primarily for internal use to provide better errors to users invoking certain env methods during tests outside of the context of a contract. Closes #1805 ### Known limitations ~This is public interface change, and needs to wait for the next major version~ macro was deprecated instead, just need a minor release --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 6b892a4 commit 8ce917a

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

soroban-sdk/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,28 @@ pub mod reexports_for_macros {
134134
pub use ctor;
135135
}
136136

137+
/// `debug_assert_in_contract!` asserts that the contract is currently executing within a
138+
/// contract. The macro expands to an assertion when testutils are enabled or in tests,
139+
/// otherwise it expands to nothing.
140+
macro_rules! debug_assert_in_contract {
141+
($env:expr $(,)?) => {{
142+
{
143+
#[cfg(any(test, feature = "testutils"))]
144+
assert!(
145+
($env).in_contract(),
146+
"this function is not accessible outside of a contract, wrap \
147+
the call with `env.as_contract()` to access it from a \
148+
particular contract"
149+
);
150+
}
151+
}};
152+
}
153+
154+
// For internal use, use `debug_assert_in_contract!` instead.
137155
/// Assert in contract asserts that the contract is currently executing within a
138156
/// contract. The macro maps to code when testutils are enabled or in tests,
139157
/// otherwise maps to nothing.
158+
#[deprecated(note = "this macro is deprecated and will be removed in a future release")]
140159
#[macro_export]
141160
macro_rules! assert_in_contract {
142161
($env:expr $(,)?) => {{

soroban-sdk/src/prng.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl Prng {
9696
/// low risk tolerance, see the module-level comment.**
9797
pub fn seed(&self, seed: Bytes) {
9898
let env = self.env();
99-
assert_in_contract!(env);
99+
debug_assert_in_contract!(env);
100100
internal::Env::prng_reseed(env, seed.into()).unwrap_infallible();
101101
}
102102

@@ -380,7 +380,7 @@ impl Prng {
380380
impl<T> Shuffle for Vec<T> {
381381
fn shuffle(&mut self, prng: &Prng) {
382382
let env = prng.env();
383-
assert_in_contract!(env);
383+
debug_assert_in_contract!(env);
384384
let obj = internal::Env::prng_vec_shuffle(env, self.to_object()).unwrap_infallible();
385385
*self = unsafe { Self::unchecked_new(env.clone(), obj) };
386386
}
@@ -456,7 +456,7 @@ impl Fill for u64 {
456456
impl Gen for u64 {
457457
fn gen(prng: &Prng) -> Self {
458458
let env = prng.env();
459-
assert_in_contract!(env);
459+
debug_assert_in_contract!(env);
460460
internal::Env::prng_u64_in_inclusive_range(env, u64::MIN, u64::MAX).unwrap_infallible()
461461
}
462462
}
@@ -466,7 +466,7 @@ impl GenRange for u64 {
466466

467467
fn gen_range(prng: &Prng, r: impl RangeBounds<Self::RangeBound>) -> Self {
468468
let env = prng.env();
469-
assert_in_contract!(env);
469+
debug_assert_in_contract!(env);
470470
let start_bound = match r.start_bound() {
471471
Bound::Included(b) => *b,
472472
Bound::Excluded(b) => b
@@ -493,7 +493,7 @@ impl Fill for Bytes {
493493
/// If the length of Bytes is greater than u32::MAX in length.
494494
fn fill(&mut self, prng: &Prng) {
495495
let env = prng.env();
496-
assert_in_contract!(env);
496+
debug_assert_in_contract!(env);
497497
let len: u32 = self.len();
498498
let obj = internal::Env::prng_bytes_new(env, len.into()).unwrap_infallible();
499499
*self = unsafe { Bytes::unchecked_new(env.clone(), obj) };
@@ -505,7 +505,7 @@ impl GenLen for Bytes {
505505
/// Generates the Bytes with the Prng of the given length.
506506
fn gen_len(prng: &Prng, len: u32) -> Self {
507507
let env = prng.env();
508-
assert_in_contract!(env);
508+
debug_assert_in_contract!(env);
509509
let obj = internal::Env::prng_bytes_new(env, len.into()).unwrap_infallible();
510510
unsafe { Bytes::unchecked_new(env.clone(), obj) }
511511
}
@@ -531,7 +531,7 @@ impl<const N: usize> Gen for BytesN<N> {
531531
/// If the length of BytesN is greater than u32::MAX in length.
532532
fn gen(prng: &Prng) -> Self {
533533
let env = prng.env();
534-
assert_in_contract!(env);
534+
debug_assert_in_contract!(env);
535535
let len: u32 = N.try_into().unwrap_optimized();
536536
let obj = internal::Env::prng_bytes_new(env, len.into()).unwrap_infallible();
537537
unsafe { BytesN::unchecked_new(env.clone(), obj) }
@@ -546,7 +546,7 @@ impl Fill for [u8] {
546546
/// If the slice is greater than u32::MAX in length.
547547
fn fill(&mut self, prng: &Prng) {
548548
let env = prng.env();
549-
assert_in_contract!(env);
549+
debug_assert_in_contract!(env);
550550
let len: u32 = self.len().try_into().unwrap_optimized();
551551
let bytes: Bytes = internal::Env::prng_bytes_new(env, len.into())
552552
.unwrap_infallible()
@@ -563,7 +563,7 @@ impl<const N: usize> Fill for [u8; N] {
563563
/// If the array is greater than u32::MAX in length.
564564
fn fill(&mut self, prng: &Prng) {
565565
let env = prng.env();
566-
assert_in_contract!(env);
566+
debug_assert_in_contract!(env);
567567
let len: u32 = N.try_into().unwrap_optimized();
568568
let bytes: Bytes = internal::Env::prng_bytes_new(env, len.into())
569569
.unwrap_infallible()

soroban-sdk/src/storage.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Storage {
8686
/// This should be used for data that requires persistency, such as token
8787
/// balances, user properties etc.
8888
pub fn persistent(&self) -> Persistent {
89-
assert_in_contract!(self.env);
89+
debug_assert_in_contract!(self.env);
9090

9191
Persistent {
9292
storage: self.clone(),
@@ -105,7 +105,7 @@ impl Storage {
105105
/// This should be used for data that needs to only exist for a limited
106106
/// period of time, such as oracle data, claimable balances, offer, etc.
107107
pub fn temporary(&self) -> Temporary {
108-
assert_in_contract!(self.env);
108+
debug_assert_in_contract!(self.env);
109109

110110
Temporary {
111111
storage: self.clone(),
@@ -138,7 +138,7 @@ impl Storage {
138138
/// operates on etc. Do not use this with any data that can scale in
139139
/// unbounded fashion (such as user balances).
140140
pub fn instance(&self) -> Instance {
141-
assert_in_contract!(self.env);
141+
debug_assert_in_contract!(self.env);
142142

143143
Instance {
144144
storage: self.clone(),

0 commit comments

Comments
 (0)