From 6e9c9e1ed7557c9bf2d600dbf09e96643a6135e9 Mon Sep 17 00:00:00 2001 From: Jay Geng Date: Tue, 13 Dec 2022 14:54:50 -0500 Subject: [PATCH 1/4] Update env to include delete-im, remove-envval changes --- soroban-ledger-snapshot/src/lib.rs | 7 +- soroban-sdk/src/accounts.rs | 62 ++++++--- soroban-sdk/src/bytes.rs | 152 ++++++++++------------ soroban-sdk/src/crypto.rs | 4 +- soroban-sdk/src/deploy.rs | 4 +- soroban-sdk/src/env.rs | 41 ++++-- soroban-sdk/src/ledger.rs | 2 +- soroban-sdk/src/lib.rs | 1 - soroban-sdk/src/map.rs | 138 ++++++++++++-------- soroban-sdk/src/serde.rs | 2 +- soroban-sdk/src/set.rs | 7 +- soroban-sdk/src/vec.rs | 194 ++++++++++++++++++----------- 12 files changed, 364 insertions(+), 250 deletions(-) diff --git a/soroban-ledger-snapshot/src/lib.rs b/soroban-ledger-snapshot/src/lib.rs index 76df473cf..5174a4084 100644 --- a/soroban-ledger-snapshot/src/lib.rs +++ b/soroban-ledger-snapshot/src/lib.rs @@ -3,6 +3,7 @@ use std::{ fs::File, io::{Read, Write}, path::Path, + rc::Rc, }; use soroban_env_host::{ @@ -79,12 +80,12 @@ impl LedgerSnapshot { // entry. pub fn update_entries<'a>( &mut self, - entries: impl IntoIterator, &'a Option>)>, + entries: impl IntoIterator, Option>)>, ) { for (k, e) in entries { - let i = self.ledger_entries.iter().position(|(ik, _)| ik == k); + let i = self.ledger_entries.iter().position(|(ik, _)| **ik == **k); if let Some(e) = e { - let new = (k.clone(), e.clone()); + let new = (Box::new((**k).clone()), Box::new((**e).clone())); if let Some(i) = i { self.ledger_entries[i] = new; } else { diff --git a/soroban-sdk/src/accounts.rs b/soroban-sdk/src/accounts.rs index ca0c1c308..2aaa9d7b6 100644 --- a/soroban-sdk/src/accounts.rs +++ b/soroban-sdk/src/accounts.rs @@ -5,7 +5,7 @@ use core::{cmp::Ordering, fmt::Debug}; use crate::{ env::internal::xdr, - env::internal::{Env as _, RawVal, RawValConvertible}, + env::internal::{Env as _, EnvBase as _, RawVal, RawValConvertible}, env::EnvObj, BytesN, ConversionError, Env, IntoVal, Object, TryFromVal, TryIntoVal, }; @@ -111,16 +111,30 @@ impl PartialOrd for AccountId { impl Ord for AccountId { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.0.cmp(&other.0) + self.0.env.check_same_env(&other.0.env); + let v = self + .0 + .env + .obj_cmp(self.0.obj.to_raw(), other.0.obj.to_raw()); + if v == 0 { + Ordering::Equal + } else if v < 0 { + Ordering::Less + } else { + Ordering::Greater + } } } impl TryFromVal for AccountId { type Error = ConversionError; - fn try_from_val(env: &Env, val: Object) -> Result { - if val.is_obj_type(xdr::ScObjectType::AccountId) { - Ok(AccountId(val.in_env(env))) + fn try_from_val(env: &Env, obj: Object) -> Result { + if obj.is_obj_type(xdr::ScObjectType::AccountId) { + Ok(AccountId(EnvObj { + env: env.clone(), + obj, + })) } else { Err(ConversionError {}) } @@ -182,7 +196,7 @@ use super::xdr::ScVal; impl TryFrom<&AccountId> for ScVal { type Error = ConversionError; fn try_from(v: &AccountId) -> Result { - ScVal::try_from_val(&v.0.env, v.0.val.to_raw()) + ScVal::try_from_val(&v.0.env, v.0.obj.to_raw()) } } @@ -248,28 +262,28 @@ impl TryIntoVal for super::xdr::AccountId { } impl AccountId { - pub(crate) unsafe fn unchecked_new(obj: EnvObj) -> Self { - Self(obj) + pub(crate) unsafe fn unchecked_new(env: Env, obj: Object) -> Self { + Self(EnvObj { env, obj }) } pub fn env(&self) -> &Env { - self.0.env() + &self.0.env } pub fn as_raw(&self) -> &RawVal { - self.0.as_raw() + self.0.obj.as_raw() } pub fn as_object(&self) -> &Object { - self.0.as_object() + &self.0.obj } pub fn to_raw(&self) -> RawVal { - self.0.to_raw() + self.0.obj.to_raw() } pub fn to_object(&self) -> Object { - self.0.to_object() + self.0.obj } } @@ -421,7 +435,11 @@ impl testutils::Accounts for Accounts { last_modified_ledger_seq: 0, ext: xdr::LedgerEntryExt::V0, }; - storage.put(&k, &v) + storage.put( + &k, + &v, + soroban_env_host::budget::AsBudget::as_budget(env.host()), + ) }) .unwrap(); } @@ -473,7 +491,10 @@ impl testutils::Accounts for Accounts { env.host() .with_mut_storage(|storage| { let k = xdr::LedgerKey::Account(xdr::LedgerKeyAccount { account_id: id }); - storage.del(&k) + storage.del( + &k, + soroban_env_host::budget::AsBudget::as_budget(env.host()), + ) }) .unwrap(); } @@ -507,7 +528,10 @@ impl Accounts { account_id: id.clone(), }); let mut v = storage - .get(&k) + .get( + &k, + soroban_env_host::budget::AsBudget::as_budget(env.host()), + ) .ok() .and_then(|v| { if let xdr::LedgerEntryData::Account(_) = v.data { @@ -526,7 +550,11 @@ impl Accounts { } else { panic!("ledger entry is not an account"); } - storage.put(&k, &v) + storage.put( + &k, + &v, + soroban_env_host::budget::AsBudget::as_budget(env.host()), + ) }) .unwrap(); } diff --git a/soroban-sdk/src/bytes.rs b/soroban-sdk/src/bytes.rs index a5ed8cba4..82b418d4a 100644 --- a/soroban-sdk/src/bytes.rs +++ b/soroban-sdk/src/bytes.rs @@ -10,7 +10,7 @@ use super::{ env::internal::{Env as _, EnvBase as _, RawValConvertible}, env::{EnvObj, IntoVal}, xdr::ScObjectType, - ConversionError, Env, EnvVal, Object, RawVal, TryFromVal, TryIntoVal, + ConversionError, Env, Object, RawVal, TryFromVal, TryIntoVal, }; use crate::unwrap::UnwrapOptimized; @@ -155,7 +155,18 @@ impl PartialOrd for Bytes { impl Ord for Bytes { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.0.cmp(&other.0) + self.0.env.check_same_env(&other.0.env); + let v = self + .0 + .env + .obj_cmp(self.0.obj.to_raw(), other.0.obj.to_raw()); + if v == 0 { + Ordering::Equal + } else if v < 0 { + Ordering::Less + } else { + Ordering::Greater + } } } @@ -176,7 +187,7 @@ impl TryFromVal for Bytes { fn try_from_val(env: &Env, val: Object) -> Result { if val.is_obj_type(ScObjectType::Bytes) { - Ok(unsafe { Bytes::unchecked_new(val.in_env(env)) }) + Ok(unsafe { Bytes::unchecked_new(env.clone(), val) }) } else { Err(ConversionError {}) } @@ -222,35 +233,28 @@ impl IntoVal for &Bytes { impl From for RawVal { #[inline(always)] fn from(v: Bytes) -> Self { - v.0.into() + v.0.obj.into() } } -impl From for EnvVal { - #[inline(always)] - fn from(v: Bytes) -> Self { - v.0.into() - } -} - -impl From for EnvObj { - #[inline(always)] - fn from(v: Bytes) -> Self { - v.0 - } -} +// impl From for EnvVal { +// #[inline(always)] +// fn from(v: Bytes) -> Self { +// v.0.into() +// } +// } impl From for Object { #[inline(always)] fn from(v: Bytes) -> Self { - v.0.val + v.0.obj } } impl From<&Bytes> for Object { #[inline(always)] fn from(v: &Bytes) -> Self { - v.0.val + v.0.obj } } @@ -265,7 +269,7 @@ impl From<&Bytes> for Bytes { impl TryFrom<&Bytes> for ScVal { type Error = ConversionError; fn try_from(v: &Bytes) -> Result { - ScVal::try_from_val(&v.0.env, v.0.val.to_raw()) + ScVal::try_from_val(&v.0.env, v.0.obj.to_raw()) } } @@ -322,36 +326,36 @@ impl IntoVal for &[u8; N] { impl Bytes { #[inline(always)] - pub(crate) unsafe fn unchecked_new(obj: EnvObj) -> Self { - Self(obj) + pub(crate) unsafe fn unchecked_new(env: Env, obj: Object) -> Self { + Self(EnvObj { env, obj }) } #[inline(always)] pub fn env(&self) -> &Env { - self.0.env() + &self.0.env } pub fn as_raw(&self) -> &RawVal { - self.0.as_raw() + self.0.obj.as_raw() } pub fn to_raw(&self) -> RawVal { - self.0.to_raw() + self.0.obj.to_raw() } pub fn as_object(&self) -> &Object { - self.0.as_object() + &self.0.obj } pub fn to_object(&self) -> Object { - self.0.to_object() + self.0.obj } /// Create an empty Bytes. #[inline(always)] pub fn new(env: &Env) -> Bytes { - let obj = env.bytes_new().in_env(env); - unsafe { Self::unchecked_new(obj) } + let obj = env.bytes_new(); + unsafe { Self::unchecked_new(env.clone(), obj) } } /// Create a Bytes from the given `[u8]`. @@ -362,20 +366,16 @@ impl Bytes { #[inline(always)] pub fn from_slice(env: &Env, items: &[u8]) -> Bytes { - Bytes( - env.bytes_new_from_slice(items) - .unwrap_optimized() - .in_env(env), - ) + Bytes(EnvObj { + env: env.clone(), + obj: env.bytes_new_from_slice(items).unwrap_optimized(), + }) } #[inline(always)] pub fn set(&mut self, i: u32, v: u8) { let v32: u32 = v.into(); - self.0 = self - .env() - .bytes_put(self.0.to_object(), i.into(), v32.into()) - .in_env(self.env()); + self.0.obj = self.env().bytes_put(self.0.obj, i.into(), v32.into()) } #[inline(always)] @@ -391,7 +391,7 @@ impl Bytes { pub fn get_unchecked(&self, i: u32) -> u8 { let res = self .env() - .bytes_get(self.0.to_object(), i.into()) + .bytes_get(self.0.obj, i.into()) .try_into() .unwrap_optimized(); let res32: u32 = unsafe { <_ as RawValConvertible>::unchecked_from_val(res) }; @@ -400,12 +400,12 @@ impl Bytes { #[inline(always)] pub fn is_empty(&self) -> bool { - self.env().bytes_len(self.0.to_object()).is_u32_zero() + self.env().bytes_len(self.0.obj).is_u32_zero() } #[inline(always)] pub fn len(&self) -> u32 { - let len = self.env().bytes_len(self.0.to_object()); + let len = self.env().bytes_len(self.0.obj); unsafe { <_ as RawValConvertible>::unchecked_from_val(len) } } @@ -420,7 +420,7 @@ impl Bytes { #[inline(always)] pub fn first_unchecked(&self) -> u8 { - let res = self.env().bytes_front(self.0.to_object()); + let res = self.env().bytes_front(self.0.obj); let res32: u32 = unsafe { <_ as RawValConvertible>::unchecked_from_val(res) }; res32 as u8 } @@ -436,7 +436,7 @@ impl Bytes { #[inline(always)] pub fn last_unchecked(&self) -> u8 { - let res = self.env().bytes_back(self.0.to_object()); + let res = self.env().bytes_back(self.0.obj); let res32: u32 = unsafe { <_ as RawValConvertible>::unchecked_from_val(res) }; res32 as u8 } @@ -453,33 +453,26 @@ impl Bytes { #[inline(always)] pub fn remove_unchecked(&mut self, i: u32) { - let env = self.env(); - let bin = env.bytes_del(self.0.to_object(), i.into()); - self.0 = bin.in_env(env); + self.0.obj = self.env().bytes_del(self.0.obj, i.into()) } #[inline(always)] pub fn push(&mut self, x: u8) { let x32: u32 = x.into(); - let env = self.env(); - let bin = env.bytes_push(self.0.to_object(), x32.into()); - self.0 = bin.in_env(env); + self.0.obj = self.env().bytes_push(self.0.obj, x32.into()) } #[inline(always)] pub fn pop(&mut self) -> Option { let last = self.last()?; - let env = self.env(); - let bin = env.bytes_pop(self.0.to_object()); - self.0 = bin.in_env(env); + self.0.obj = self.env().bytes_pop(self.0.obj); Some(last) } #[inline(always)] pub fn pop_unchecked(&mut self) -> u8 { let last = self.last_unchecked(); - let env = self.env(); - self.0 = env.bytes_pop(self.0.to_object()).in_env(env); + self.0.obj = self.env().bytes_pop(self.0.obj); last } @@ -491,10 +484,8 @@ impl Bytes { /// When `i` is greater than the length of [Bytes]. #[inline(always)] pub fn insert(&mut self, i: u32, b: u8) { - let env = self.env(); let b32: u32 = b.into(); - let bin = env.bytes_insert(self.0.to_object(), i.into(), b32.into()); - self.0 = bin.in_env(env); + self.0.obj = self.env().bytes_insert(self.0.obj, i.into(), b32.into()) } /// Insert the bytes in `bytes` into this [Bytes] starting at position @@ -538,9 +529,7 @@ impl Bytes { #[inline(always)] pub fn append(&mut self, other: &Bytes) { - let env = self.env(); - let bin = env.bytes_append(self.0.to_object(), other.0.to_object()); - self.0 = bin.in_env(env); + self.0.obj = self.env().bytes_append(self.0.obj, other.0.obj) } #[inline(always)] @@ -550,11 +539,10 @@ impl Bytes { #[inline(always)] pub fn extend_from_slice(&mut self, slice: &[u8]) { - let env = self.env(); - self.0 = env + self.0.obj = self + .env() .bytes_copy_from_slice(self.to_object(), self.len().into(), slice) .unwrap_optimized() - .in_env(env); } /// Copy the bytes from slice into [Bytes]. @@ -563,11 +551,10 @@ impl Bytes { /// if necessary. #[inline(always)] pub fn copy_from_slice(&mut self, i: u32, slice: &[u8]) { - let env = self.env(); - self.0 = env + self.0.obj = self + .env() .bytes_copy_from_slice(self.to_object(), i.into(), slice) .unwrap_optimized() - .in_env(env); } /// Copy the bytes in [Bytes] into the given slice. @@ -594,8 +581,8 @@ impl Bytes { Bound::Unbounded => self.len(), }; let env = self.env(); - let bin = env.bytes_slice(self.0.to_object(), start_bound.into(), end_bound.into()); - unsafe { Self::unchecked_new(bin.in_env(env)) } + let bin = env.bytes_slice(self.0.obj, start_bound.into(), end_bound.into()); + unsafe { Self::unchecked_new(env.clone(), bin) } } pub fn iter(&self) -> BinIter { @@ -628,7 +615,7 @@ impl Iterator for BinIter { if self.0.is_empty() { None } else { - let val = self.0.env().bytes_front(self.0 .0.to_object()); + let val = self.0.env().bytes_front(self.0 .0.obj); self.0 = self.0.slice(1..); let val = unsafe { ::unchecked_from_val(val) } as u8; Some(val) @@ -647,7 +634,7 @@ impl DoubleEndedIterator for BinIter { if len == 0 { None } else { - let val = self.0.env().bytes_back(self.0 .0.to_object()); + let val = self.0.env().bytes_back(self.0 .0.obj); self.0 = self.0.slice(..len - 1); let val = unsafe { ::unchecked_from_val(val) } as u8; Some(val) @@ -903,19 +890,12 @@ impl From> for RawVal { } } -impl From> for EnvVal { - #[inline(always)] - fn from(v: BytesN) -> Self { - v.0.into() - } -} - -impl From> for EnvObj { - #[inline(always)] - fn from(v: BytesN) -> Self { - v.0.into() - } -} +// impl From> for EnvVal { +// #[inline(always)] +// fn from(v: BytesN) -> Self { +// v.0.into() +// } +// } impl From> for Bytes { #[inline(always)] @@ -935,7 +915,7 @@ impl From<&BytesN> for Bytes { impl TryFrom<&BytesN> for ScVal { type Error = ConversionError; fn try_from(v: &BytesN) -> Result { - ScVal::try_from_val(&v.0 .0.env, v.0 .0.val.to_raw()) + ScVal::try_from_val(&v.0 .0.env, v.0 .0.obj.to_raw()) } } @@ -968,8 +948,8 @@ impl TryIntoVal> for ScVal { impl BytesN { #[inline(always)] - pub(crate) unsafe fn unchecked_new(obj: EnvObj) -> Self { - Self(Bytes::unchecked_new(obj)) + pub(crate) unsafe fn unchecked_new(env: Env, obj: Object) -> Self { + Self(Bytes::unchecked_new(env, obj)) } pub fn env(&self) -> &Env { diff --git a/soroban-sdk/src/crypto.rs b/soroban-sdk/src/crypto.rs index e0adb455c..2b61d8e6d 100644 --- a/soroban-sdk/src/crypto.rs +++ b/soroban-sdk/src/crypto.rs @@ -18,8 +18,8 @@ impl Crypto { /// Returns the SHA-256 hash of the data. pub fn sha256(&self, data: &Bytes) -> BytesN<32> { let env = self.env(); - let bin_obj = internal::Env::compute_hash_sha256(env, data.into()); - unsafe { BytesN::unchecked_new(bin_obj.in_env(env)) } + let bin = internal::Env::compute_hash_sha256(env, data.into()); + unsafe { BytesN::unchecked_new(env.clone(), bin) } } /// Verifies an ed25519 signature. diff --git a/soroban-sdk/src/deploy.rs b/soroban-sdk/src/deploy.rs index e5b14fa2d..bb532fb0f 100644 --- a/soroban-sdk/src/deploy.rs +++ b/soroban-sdk/src/deploy.rs @@ -109,7 +109,7 @@ impl DeployerWithCurrentContract { wasm_hash.into_val(env).to_object(), self.salt.to_object(), ); - unsafe { BytesN::<32>::unchecked_new(id.in_env(env)) } + unsafe { BytesN::<32>::unchecked_new(env.clone(), id) } } /// Deploy a built-in token contract. @@ -121,7 +121,7 @@ impl DeployerWithCurrentContract { pub fn deploy_token(&self) -> BytesN<32> { let env = &self.env; let id = env.create_token_from_contract(self.salt.to_object()); - unsafe { BytesN::<32>::unchecked_new(id.in_env(env)) } + unsafe { BytesN::<32>::unchecked_new(env.clone(), id) } } } diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 621be59c8..e47c0b56a 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -12,18 +12,18 @@ pub mod internal { pub type EnvImpl = Host; #[doc(hidden)] - impl TryConvert for super::Env + impl Convert for super::Env where - EnvImpl: TryConvert, + EnvImpl: Convert, { - type Error = >::Error; + type Error = >::Error; fn convert(&self, f: F) -> Result { self.env_impl.convert(f) } } } -// Testutils from the environmen are pub here, and then pub re-exported out of +// Testutils from the environment are pub here, and then pub re-exported out of // the SDK in the crate::testutils mod. #[cfg(any(test, feature = "testutils"))] pub mod testutils { @@ -35,6 +35,7 @@ pub mod testutils { pub use internal::meta; pub use internal::xdr; pub use internal::BitSet; +pub use internal::Compare; pub use internal::ConversionError; pub use internal::EnvBase; pub use internal::FromVal; @@ -49,8 +50,17 @@ pub use internal::TryFromVal; pub use internal::TryIntoVal; pub use internal::Val; -pub type EnvVal = internal::EnvVal; -pub type EnvObj = internal::EnvVal; +#[derive(Clone)] +pub struct EnvVal { + pub env: Env, + pub val: RawVal, +} + +#[derive(Clone)] +pub struct EnvObj { + pub env: Env, + pub obj: Object, +} use crate::{ accounts::Accounts, address::Address, crypto::Crypto, deploy::Deployer, events::Events, @@ -103,10 +113,10 @@ impl Env { .expect("unrecognized invoker type"); match invoker_type { InvokerType::Account => Address::Account(unsafe { - AccountId::unchecked_new(internal::Env::get_invoking_account(self).in_env(self)) + AccountId::unchecked_new(self.clone(), internal::Env::get_invoking_account(self)) }), InvokerType::Contract => Address::Contract(unsafe { - BytesN::unchecked_new(internal::Env::get_invoking_contract(self).in_env(self)) + BytesN::unchecked_new(self.clone(), internal::Env::get_invoking_contract(self)) }), } } @@ -160,7 +170,7 @@ impl Env { /// Get the 32-byte hash identifier of the current executing contract. pub fn current_contract(&self) -> BytesN<32> { let id = internal::Env::get_current_contract(self); - unsafe { BytesN::<32>::unchecked_new(id.in_env(self)) } + unsafe { BytesN::<32>::unchecked_new(self.clone(), id) } } /// Get the 32-byte hash identifier of the current executing contract. @@ -205,7 +215,12 @@ impl Env { /// ``` pub fn call_stack(&self) -> Vec<(BytesN<32>, Symbol)> { let stack = internal::Env::get_current_call_stack(self); - unsafe { Vec::unchecked_new(stack.in_env(self)) } + unsafe { + Vec::unchecked_new(EnvObj { + env: self.clone(), + obj: stack, + }) + } } #[doc(hidden)] @@ -536,6 +551,7 @@ impl Env { val: xdr::ScVal::Object(Some(xdr::ScObject::ContractCode(source))), }), }, + &self.env_impl.budget_cloned(), ) }) .unwrap(); @@ -651,11 +667,12 @@ impl Env { let snapshot = self.snapshot.clone().unwrap_or_default(); let mut snapshot = (*snapshot).clone(); snapshot.set_ledger_info(self.ledger().get()); + let budget = soroban_env_host::budget::AsBudget::as_budget(&self.env_impl); let storage = self .env_impl .with_mut_storage(|s| Ok(s.map.clone())) .unwrap(); - snapshot.update_entries(storage.iter()); + snapshot.update_entries(storage.iter(budget).unwrap()); snapshot } @@ -670,7 +687,7 @@ impl Env { /// Get the budget that tracks the resources consumed for the environment. pub fn budget(&self) -> Budget { - self.env_impl.with_budget(|b| Budget::new(b.clone())) + self.env_impl.with_budget(|b| Budget::new(b)) } } diff --git a/soroban-sdk/src/ledger.rs b/soroban-sdk/src/ledger.rs index e174366d4..9fbcc8943 100644 --- a/soroban-sdk/src/ledger.rs +++ b/soroban-sdk/src/ledger.rs @@ -89,7 +89,7 @@ impl Ledger { pub fn network_passphrase(&self) -> Bytes { let env = self.env(); let bin_obj = internal::Env::get_ledger_network_passphrase(env); - unsafe { Bytes::unchecked_new(bin_obj.in_env(env)) } + unsafe { Bytes::unchecked_new(env.clone(), bin_obj) } } } diff --git a/soroban-sdk/src/lib.rs b/soroban-sdk/src/lib.rs index 52238d607..f63eb24b5 100644 --- a/soroban-sdk/src/lib.rs +++ b/soroban-sdk/src/lib.rs @@ -602,7 +602,6 @@ pub use env::TryIntoVal; pub use env::Symbol; mod envhidden { - pub use super::env::EnvVal; pub use super::env::Object; pub use super::env::Status; } diff --git a/soroban-sdk/src/map.rs b/soroban-sdk/src/map.rs index d3f72f219..2b1f46ef5 100644 --- a/soroban-sdk/src/map.rs +++ b/soroban-sdk/src/map.rs @@ -3,10 +3,10 @@ use core::{cmp::Ordering, fmt::Debug, iter::FusedIterator, marker::PhantomData}; use crate::iter::{UncheckedEnumerable, UncheckedIter}; use super::{ - env::internal::{Env as _, RawValConvertible}, + env::internal::{Env as _, EnvBase as _, RawValConvertible}, env::EnvObj, xdr::ScObjectType, - ConversionError, Env, EnvVal, IntoVal, Object, RawVal, Status, TryFromVal, TryIntoVal, Vec, + ConversionError, Env, IntoVal, Object, RawVal, Status, TryFromVal, TryIntoVal, Vec, }; #[cfg(not(target_family = "wasm"))] @@ -121,7 +121,18 @@ where V: IntoVal + TryFromVal, { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.0.cmp(&other.0) + self.0.env.check_same_env(&other.0.env); + let v = self + .0 + .env + .obj_cmp(self.0.obj.to_raw(), other.0.obj.to_raw()); + if v == 0 { + Ordering::Equal + } else if v < 0 { + Ordering::Less + } else { + Ordering::Greater + } } } @@ -156,7 +167,14 @@ where #[inline(always)] fn try_from_val(env: &Env, obj: Object) -> Result { if obj.is_obj_type(ScObjectType::Map) { - Ok(Map(obj.in_env(env), PhantomData, PhantomData)) + Ok(Map( + EnvObj { + env: env.clone(), + obj, + }, + PhantomData, + PhantomData, + )) } else { Err(ConversionError {}) } @@ -226,37 +244,37 @@ where { #[inline(always)] fn from(m: Map) -> Self { - m.0.into() + m.0.obj.into() } } -impl From> for EnvVal -where - K: IntoVal + TryFromVal, - V: IntoVal + TryFromVal, -{ - #[inline(always)] - fn from(m: Map) -> Self { - m.0.into() - } -} - -impl From> for EnvObj -where - K: IntoVal + TryFromVal, - V: IntoVal + TryFromVal, -{ - #[inline(always)] - fn from(m: Map) -> Self { - m.0 - } -} +// impl From> for EnvVal +// where +// K: IntoVal + TryFromVal, +// V: IntoVal + TryFromVal, +// { +// #[inline(always)] +// fn from(m: Map) -> Self { +// m.0.into() +// } +// } + +// impl From> for EnvObj +// where +// K: IntoVal + TryFromVal, +// V: IntoVal + TryFromVal, +// { +// #[inline(always)] +// fn from(m: Map) -> Self { +// m.0 +// } +// } #[cfg(not(target_family = "wasm"))] impl TryFrom<&Map> for ScVal { type Error = ConversionError; fn try_from(v: &Map) -> Result { - ScVal::try_from_val(&v.0.env, v.0.val.to_raw()) + ScVal::try_from_val(&v.0.env, v.0.obj.to_raw()) } } @@ -307,32 +325,35 @@ where #[inline(always)] pub fn env(&self) -> &Env { - self.0.env() + &self.0.env } #[inline(always)] pub fn as_raw(&self) -> &RawVal { - self.0.as_raw() + self.0.obj.as_raw() } #[inline(always)] pub fn to_raw(&self) -> RawVal { - self.0.to_raw() + self.0.obj.to_raw() } #[inline(always)] pub(crate) fn as_object(&self) -> &Object { - self.0.as_object() + &self.0.obj } #[inline(always)] pub(crate) fn to_object(&self) -> Object { - self.0.to_object() + self.0.obj } #[inline(always)] pub fn new(env: &Env) -> Map { - let obj = env.map_new().in_env(env); + let obj = EnvObj { + env: env.clone(), + obj: env.map_new(), + }; unsafe { Self::unchecked_new(obj) } } @@ -348,7 +369,7 @@ where #[inline(always)] pub fn contains_key(&self, k: K) -> bool { let env = self.env(); - let has = env.map_has(self.0.to_object(), k.into_val(env)); + let has = env.map_has(self.0.obj, k.into_val(env)); has.is_true() } @@ -356,9 +377,9 @@ where pub fn get(&self, k: K) -> Option> { let env = self.env(); let k = k.into_val(env); - let has = env.map_has(self.0.to_object(), k); + let has = env.map_has(self.0.obj, k); if has.is_true() { - let v = env.map_get(self.0.to_object(), k); + let v = env.map_get(self.0.obj, k); Some(V::try_from_val(env, v)) } else { None @@ -368,25 +389,31 @@ where #[inline(always)] pub fn get_unchecked(&self, k: K) -> Result { let env = self.env(); - let v = env.map_get(self.0.to_object(), k.into_val(env)); + let v = env.map_get(self.0.obj, k.into_val(env)); V::try_from_val(env, v) } #[inline(always)] pub fn set(&mut self, k: K, v: V) { let env = self.env(); - let map = env.map_put(self.0.to_object(), k.into_val(env), v.into_val(env)); - self.0 = map.in_env(env); + let map = env.map_put(self.0.obj, k.into_val(env), v.into_val(env)); + self.0 = EnvObj { + env: env.clone(), + obj: map, + }; } #[inline(always)] pub fn remove(&mut self, k: K) -> Option<()> { let env = self.env(); let k = k.into_val(env); - let has = env.map_has(self.0.to_object(), k); + let has = env.map_has(self.0.obj, k); if has.is_true() { - let map = env.map_del(self.0.to_object(), k); - self.0 = map.in_env(env); + let map = env.map_del(self.0.obj, k); + self.0 = EnvObj { + env: env.clone(), + obj: map, + }; Some(()) } else { None @@ -396,35 +423,38 @@ where #[inline(always)] pub fn remove_unchecked(&mut self, k: K) { let env = self.env(); - let map = env.map_del(self.0.to_object(), k.into_val(env)); - self.0 = map.in_env(env); + let map = env.map_del(self.0.obj, k.into_val(env)); + self.0 = EnvObj { + env: env.clone(), + obj: map, + }; } #[inline(always)] pub fn is_empty(&self) -> bool { let env = self.env(); - let len = env.map_len(self.0.to_object()); + let len = env.map_len(self.0.obj); len.is_u32_zero() } #[inline(always)] pub fn len(&self) -> u32 { let env = self.env(); - let len = env.map_len(self.0.to_object()); + let len = env.map_len(self.0.obj); unsafe { ::unchecked_from_val(len) } } #[inline(always)] pub fn keys(&self) -> Vec { let env = self.env(); - let vec = env.map_keys(self.0.to_object()); + let vec = env.map_keys(self.0.obj); Vec::::try_from_val(env, vec).unwrap() } #[inline(always)] pub fn values(&self) -> Vec { let env = self.env(); - let vec = env.map_values(self.0.to_object()); + let vec = env.map_values(self.0.obj); Vec::::try_from_val(env, vec).unwrap() } @@ -490,12 +520,12 @@ where fn next(&mut self) -> Option { let env = &self.0 .0.env; - let key = env.map_min_key(self.0 .0.to_object()); + let key = env.map_min_key(self.0 .0.obj); if Status::try_from(key).is_ok() { return None; } - let value = env.map_get(self.0 .0.to_object(), key); - self.0 .0.val = env.map_del(self.0 .0.to_object(), key); + let value = env.map_get(self.0 .0.obj, key); + self.0 .0.obj = env.map_del(self.0 .0.obj, key); Some(Ok(( match K::try_from_val(env, key) { Ok(k) => k, @@ -523,12 +553,12 @@ where { fn next_back(&mut self) -> Option { let env = &self.0 .0.env; - let key = env.map_max_key(self.0 .0.to_object()); + let key = env.map_max_key(self.0 .0.obj); if Status::try_from(key).is_ok() { return None; } - let value = env.map_get(self.0 .0.to_object(), key); - self.0 .0.val = env.map_del(self.0 .0.to_object(), key); + let value = env.map_get(self.0 .0.obj, key); + self.0 .0.obj = env.map_del(self.0 .0.obj, key); Some(Ok(( match K::try_from_val(env, key) { Ok(k) => k, diff --git a/soroban-sdk/src/serde.rs b/soroban-sdk/src/serde.rs index 78833ca23..afda58242 100644 --- a/soroban-sdk/src/serde.rs +++ b/soroban-sdk/src/serde.rs @@ -46,7 +46,7 @@ where fn serialize(self, env: &Env) -> Bytes { let val: RawVal = self.into_val(env); let bin = env.serialize_to_bytes(val); - unsafe { Bytes::unchecked_new(bin.in_env(env)) } + unsafe { Bytes::unchecked_new(env.clone(), bin) } } } diff --git a/soroban-sdk/src/set.rs b/soroban-sdk/src/set.rs index 1cdd62eb6..61e086258 100644 --- a/soroban-sdk/src/set.rs +++ b/soroban-sdk/src/set.rs @@ -283,7 +283,12 @@ where fn try_from_val(env: &Env, obj: Object) -> Result { if obj.is_obj_type(ScObjectType::Map) { - Ok(unsafe { Set::::unchecked_new(obj.in_env(env)) }) + Ok(unsafe { + Set::::unchecked_new(EnvObj { + env: env.clone(), + obj, + }) + }) } else { Err(ConversionError {}) } diff --git a/soroban-sdk/src/vec.rs b/soroban-sdk/src/vec.rs index ecb7e9ae0..36af2a1f1 100644 --- a/soroban-sdk/src/vec.rs +++ b/soroban-sdk/src/vec.rs @@ -10,9 +10,9 @@ use core::{ use crate::iter::{UncheckedEnumerable, UncheckedIter}; use super::{ - env::{internal::Env as _, EnvObj, RawValConvertible}, + env::{internal::Env as _, internal::EnvBase as _, EnvObj, RawValConvertible}, xdr::ScObjectType, - ConversionError, Env, EnvVal, IntoVal, Object, RawVal, Set, TryFromVal, TryIntoVal, + ConversionError, Env, IntoVal, Object, RawVal, Set, TryFromVal, TryIntoVal, }; #[cfg(doc)] @@ -142,7 +142,18 @@ where T: IntoVal + TryFromVal, { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.0.cmp(&other.0) + self.0.env.check_same_env(&other.0.env); + let v = self + .0 + .env + .obj_cmp(self.0.obj.to_raw(), other.0.obj.to_raw()); + if v == 0 { + Ordering::Equal + } else if v < 0 { + Ordering::Less + } else { + Ordering::Greater + } } } @@ -186,7 +197,12 @@ where #[inline(always)] fn try_from_val(env: &Env, obj: Object) -> Result { if obj.is_obj_type(ScObjectType::Vec) { - Ok(unsafe { Vec::::unchecked_new(obj.in_env(env)) }) + Ok(unsafe { + Vec::::unchecked_new(EnvObj { + env: env.clone(), + obj, + }) + }) } else { Err(ConversionError {}) } @@ -251,7 +267,7 @@ where { #[inline(always)] fn from(v: Vec) -> Self { - v.0.into() + v.0.obj.into() } } @@ -270,28 +286,28 @@ where { #[inline(always)] fn from(v: Vec) -> Self { - v.0.into() - } -} - -impl From> for EnvVal -where - T: IntoVal + TryFromVal, -{ - fn from(v: Vec) -> Self { - v.0.into() + v.0.obj } } -impl From> for EnvObj -where - T: IntoVal + TryFromVal, -{ - #[inline(always)] - fn from(v: Vec) -> Self { - v.0 - } -} +// impl From> for EnvVal +// where +// T: IntoVal + TryFromVal, +// { +// fn from(v: Vec) -> Self { +// v.0.into() +// } +// } + +// impl From> for EnvObj +// where +// T: IntoVal + TryFromVal, +// { +// #[inline(always)] +// fn from(v: Vec) -> Self { +// v.0 +// } +// } impl From> for Set where @@ -313,7 +329,7 @@ use super::xdr::{ScObject, ScVal, ScVec}; impl TryFrom<&Vec> for ScVal { type Error = ConversionError; fn try_from(v: &Vec) -> Result { - ScVal::try_from_val(&v.0.env, v.0.val.to_raw()) + ScVal::try_from_val(&v.0.env, v.0.obj.to_raw()) } } @@ -443,23 +459,23 @@ impl Vec { } pub fn env(&self) -> &Env { - self.0.env() + &self.0.env } pub fn as_raw(&self) -> &RawVal { - self.0.as_raw() + self.0.obj.as_raw() } pub fn to_raw(&self) -> RawVal { - self.0.to_raw() + self.0.obj.to_raw() } pub fn as_object(&self) -> &Object { - self.0.as_object() + &self.0.obj } pub fn to_object(&self) -> Object { - self.0.to_object() + self.0.obj } } @@ -469,8 +485,13 @@ where { #[inline(always)] pub fn new(env: &Env) -> Vec { - let obj = env.vec_new(().into()).in_env(env); - unsafe { Self::unchecked_new(obj) } + let obj = env.vec_new(().into()); + unsafe { + Self::unchecked_new(EnvObj { + env: env.clone(), + obj, + }) + } } #[inline(always)] @@ -494,7 +515,7 @@ where pub fn get(&self, i: u32) -> Option> { if i < self.len() { let env = self.env(); - let val = env.vec_get(self.0.to_object(), i.into()); + let val = env.vec_get(self.0.obj, i.into()); Some(T::try_from_val(env, val)) } else { None @@ -507,15 +528,18 @@ where T::Error: Debug, { let env = self.env(); - let val = env.vec_get(self.0.to_object(), i.into()); + let val = env.vec_get(self.0.obj, i.into()); T::try_from_val(env, val) } #[inline(always)] pub fn set(&mut self, i: u32, v: T) { let env = self.env(); - let vec = env.vec_put(self.0.to_object(), i.into(), v.into_val(env)); - self.0 = vec.in_env(env); + let obj = env.vec_put(self.0.obj, i.into(), v.into_val(env)); + self.0 = EnvObj { + env: env.clone(), + obj, + }; } #[inline(always)] @@ -531,37 +555,46 @@ where #[inline(always)] pub fn remove_unchecked(&mut self, i: u32) { let env = self.env(); - let vec = env.vec_del(self.0.to_object(), i.into()); - self.0 = vec.in_env(env); + let obj = env.vec_del(self.0.obj, i.into()); + self.0 = EnvObj { + env: env.clone(), + obj, + }; } #[inline(always)] pub fn is_empty(&self) -> bool { let env = self.env(); - let val = env.vec_len(self.0.to_object()); + let val = env.vec_len(self.0.obj); val.is_u32_zero() } #[inline(always)] pub fn len(&self) -> u32 { let env = self.env(); - let val = env.vec_len(self.0.to_object()); + let val = env.vec_len(self.0.obj); unsafe { <_ as RawValConvertible>::unchecked_from_val(val) } } #[inline(always)] pub fn push_front(&mut self, x: T) { let env = self.env(); - let vec = env.vec_push_front(self.0.to_object(), x.into_val(env)); - self.0 = vec.in_env(env); + let obj = env.vec_push_front(self.0.obj, x.into_val(env)); + self.0 = EnvObj { + env: env.clone(), + obj, + }; } #[inline(always)] pub fn pop_front(&mut self) -> Option> { let last = self.last()?; let env = self.env(); - let vec = env.vec_pop_front(self.0.to_object()); - self.0 = vec.in_env(env); + let obj = env.vec_pop_front(self.0.obj); + self.0 = EnvObj { + env: env.clone(), + obj, + }; Some(last) } @@ -569,24 +602,33 @@ where pub fn pop_front_unchecked(&mut self) -> Result { let last = self.first_unchecked(); let env = self.env(); - let vec = env.vec_pop_front(self.0.to_object()); - self.0 = vec.in_env(env); + let obj = env.vec_pop_front(self.0.obj); + self.0 = EnvObj { + env: env.clone(), + obj, + }; last } #[inline(always)] pub fn push_back(&mut self, x: T) { let env = self.env(); - let vec = env.vec_push_back(self.0.to_object(), x.into_val(env)); - self.0 = vec.in_env(env); + let obj = env.vec_push_back(self.0.obj, x.into_val(env)); + self.0 = EnvObj { + env: env.clone(), + obj, + }; } #[inline(always)] pub fn pop_back(&mut self) -> Option> { let last = self.last()?; let env = self.env(); - let vec = env.vec_pop_back(self.0.to_object()); - self.0 = vec.in_env(env); + let obj = env.vec_pop_back(self.0.obj); + self.0 = EnvObj { + env: env.clone(), + obj, + }; Some(last) } @@ -594,8 +636,11 @@ where pub fn pop_back_unchecked(&mut self) -> Result { let last = self.last_unchecked(); let env = self.env(); - let vec = env.vec_pop_back(self.0.to_object()); - self.0 = vec.in_env(env); + let obj = env.vec_pop_back(self.0.obj); + self.0 = EnvObj { + env: env.clone(), + obj, + }; last } @@ -622,16 +667,16 @@ where if self.is_empty() { None } else { - let env = self.0.env(); - let val = env.vec_front(self.0.to_object()); + let env = &self.0.env; + let val = env.vec_front(self.0.obj); Some(T::try_from_val(env, val)) } } #[inline(always)] pub fn first_unchecked(&self) -> Result { - let env = self.0.env(); - let val = env.vec_front(self.0.to_object()); + let env = &self.0.env; + let val = env.vec_front(self.0.obj); T::try_from_val(env, val) } @@ -641,7 +686,7 @@ where None } else { let env = self.env(); - let val = env.vec_back(self.0.to_object()); + let val = env.vec_back(self.0.obj); Some(T::try_from_val(env, val)) } } @@ -649,22 +694,28 @@ where #[inline(always)] pub fn last_unchecked(&self) -> Result { let env = self.env(); - let val = env.vec_back(self.0.to_object()); + let val = env.vec_back(self.0.obj); T::try_from_val(env, val) } #[inline(always)] pub fn insert(&mut self, i: u32, x: T) { let env = self.env(); - let vec = env.vec_put(self.0.to_object(), i.into(), x.into_val(env)); - self.0 = vec.in_env(env); + let obj = env.vec_put(self.0.obj, i.into(), x.into_val(env)); + self.0 = EnvObj { + env: env.clone(), + obj, + }; } #[inline(always)] pub fn append(&mut self, other: &Vec) { let env = self.env(); - let vec = env.vec_append(self.0.to_object(), other.0.to_object()); - self.0 = vec.in_env(env); + let obj = env.vec_append(self.0.obj, other.0.obj); + self.0 = EnvObj { + env: env.clone(), + obj, + }; } #[inline(always)] @@ -697,8 +748,11 @@ where Bound::Unbounded => self.len(), }; let env = self.env(); - let vec = env.vec_slice(self.0.to_object(), start_bound.into(), end_bound.into()); - let vec = vec.in_env(env); + let obj = env.vec_slice(self.0.obj, start_bound.into(), end_bound.into()); + let vec = EnvObj { + env: env.clone(), + obj, + }; unsafe { Self::unchecked_new(vec) } } @@ -737,7 +791,7 @@ where pub fn contains(&self, item: impl Borrow) -> bool { let env = self.env(); let val = item.borrow().into_val(env); - !env.vec_first_index_of(self.to_object(), val).is_void() + !env.vec_first_index_of(self.0.obj, val).is_void() } /// Returns the index of the first occurrence of the item. @@ -747,7 +801,7 @@ where pub fn first_index_of(&self, item: impl Borrow) -> Option { let env = self.env(); let val = item.borrow().into_val(env); - env.vec_first_index_of(self.to_object(), val) + env.vec_first_index_of(self.0.obj, val) .try_into_val(env) .unwrap() } @@ -759,7 +813,7 @@ where pub fn last_index_of(&self, item: impl Borrow) -> Option { let env = self.env(); let val = item.borrow().into_val(env); - env.vec_last_index_of(self.to_object(), val) + env.vec_last_index_of(self.0.obj, val) .try_into_val(env) .unwrap() } @@ -777,7 +831,7 @@ where pub fn binary_search(&self, item: impl Borrow) -> Result { let env = self.env(); let val = item.borrow().into_val(env); - let high_low = env.vec_binary_search(self.to_object(), val); + let high_low = env.vec_binary_search(self.0.obj, val); let high: u32 = (high_low >> u32::BITS) as u32; let low: u32 = high_low as u32; if high == 1 { @@ -835,7 +889,7 @@ where if len == 0 { None } else { - let val = self.0.env().vec_front(self.0 .0.to_object()); + let val = self.0.env().vec_front(self.0 .0.obj); self.0 = self.0.slice(1..); Some(T::try_from_val(self.0.env(), val)) } @@ -859,7 +913,7 @@ where if len == 0 { None } else { - let val = self.0.env().vec_back(self.0 .0.to_object()); + let val = self.0.env().vec_back(self.0 .0.obj); self.0 = self.0.slice(..len - 1); Some(T::try_from_val(self.0.env(), val)) } From 73bf749bfe2ce5a4642f7faec3efedec01e5a21d Mon Sep 17 00:00:00 2001 From: Jay Geng Date: Tue, 13 Dec 2022 17:19:12 -0500 Subject: [PATCH 2/4] Clean out EnvVal --- soroban-sdk/src/bytes.rs | 14 -------------- soroban-sdk/src/env.rs | 6 ------ soroban-sdk/src/map.rs | 22 ---------------------- soroban-sdk/src/vec.rs | 19 ------------------- 4 files changed, 61 deletions(-) diff --git a/soroban-sdk/src/bytes.rs b/soroban-sdk/src/bytes.rs index 82b418d4a..6167613b0 100644 --- a/soroban-sdk/src/bytes.rs +++ b/soroban-sdk/src/bytes.rs @@ -237,13 +237,6 @@ impl From for RawVal { } } -// impl From for EnvVal { -// #[inline(always)] -// fn from(v: Bytes) -> Self { -// v.0.into() -// } -// } - impl From for Object { #[inline(always)] fn from(v: Bytes) -> Self { @@ -890,13 +883,6 @@ impl From> for RawVal { } } -// impl From> for EnvVal { -// #[inline(always)] -// fn from(v: BytesN) -> Self { -// v.0.into() -// } -// } - impl From> for Bytes { #[inline(always)] fn from(v: BytesN) -> Self { diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index e47c0b56a..c1ecce086 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -50,12 +50,6 @@ pub use internal::TryFromVal; pub use internal::TryIntoVal; pub use internal::Val; -#[derive(Clone)] -pub struct EnvVal { - pub env: Env, - pub val: RawVal, -} - #[derive(Clone)] pub struct EnvObj { pub env: Env, diff --git a/soroban-sdk/src/map.rs b/soroban-sdk/src/map.rs index 2b1f46ef5..eaf76da9b 100644 --- a/soroban-sdk/src/map.rs +++ b/soroban-sdk/src/map.rs @@ -248,28 +248,6 @@ where } } -// impl From> for EnvVal -// where -// K: IntoVal + TryFromVal, -// V: IntoVal + TryFromVal, -// { -// #[inline(always)] -// fn from(m: Map) -> Self { -// m.0.into() -// } -// } - -// impl From> for EnvObj -// where -// K: IntoVal + TryFromVal, -// V: IntoVal + TryFromVal, -// { -// #[inline(always)] -// fn from(m: Map) -> Self { -// m.0 -// } -// } - #[cfg(not(target_family = "wasm"))] impl TryFrom<&Map> for ScVal { type Error = ConversionError; diff --git a/soroban-sdk/src/vec.rs b/soroban-sdk/src/vec.rs index 36af2a1f1..74b85d40b 100644 --- a/soroban-sdk/src/vec.rs +++ b/soroban-sdk/src/vec.rs @@ -290,25 +290,6 @@ where } } -// impl From> for EnvVal -// where -// T: IntoVal + TryFromVal, -// { -// fn from(v: Vec) -> Self { -// v.0.into() -// } -// } - -// impl From> for EnvObj -// where -// T: IntoVal + TryFromVal, -// { -// #[inline(always)] -// fn from(v: Vec) -> Self { -// v.0 -// } -// } - impl From> for Set where T: IntoVal + TryFromVal, From 6900279190938ae1df401968e0a1c82c971edcb3 Mon Sep 17 00:00:00 2001 From: Jay Geng Date: Tue, 13 Dec 2022 21:50:56 -0500 Subject: [PATCH 3/4] Remove EnvVal, update env --- Cargo.lock | 53 ++---------- Cargo.toml | 6 +- soroban-sdk/src/accounts.rs | 39 ++++----- soroban-sdk/src/bytes.rs | 75 +++++++++-------- soroban-sdk/src/env.rs | 13 +-- soroban-sdk/src/map.rs | 111 +++++++++++-------------- soroban-sdk/src/set.rs | 15 ++-- soroban-sdk/src/vec.rs | 159 ++++++++++++------------------------ 8 files changed, 170 insertions(+), 301 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0633fb2ca..52a43ab1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,15 +59,6 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" -[[package]] -name = "bitmaps" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "031043d04099746d8db04daf1fa424b2bc8bd69d92b25962dcde24da39ab64a2" -dependencies = [ - "typenum", -] - [[package]] name = "block-buffer" version = "0.9.0" @@ -438,20 +429,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" -[[package]] -name = "im-rc" -version = "15.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af1955a75fa080c677d3972822ec4bad316169ab1cfc6c257a942c2265dbe5fe" -dependencies = [ - "bitmaps", - "rand_core 0.6.4", - "rand_xoshiro", - "sized-chunks", - "typenum", - "version_check", -] - [[package]] name = "indexmap" version = "1.9.2" @@ -744,15 +721,6 @@ dependencies = [ "rand_core 0.5.1", ] -[[package]] -name = "rand_xoshiro" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa" -dependencies = [ - "rand_core 0.6.4", -] - [[package]] name = "rustc-demangle" version = "0.1.21" @@ -860,16 +828,6 @@ version = "1.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" -[[package]] -name = "sized-chunks" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e" -dependencies = [ - "bitmaps", - "typenum", -] - [[package]] name = "soroban-auth" version = "0.3.2" @@ -883,7 +841,7 @@ dependencies = [ [[package]] name = "soroban-env-common" version = "0.0.11" -source = "git+https://github.com/stellar/rs-soroban-env?rev=c551daf#c551daf2b2f7fb4eb2d6e12ede320670133b54fe" +source = "git+https://github.com/stellar/rs-soroban-env?rev=995386a#995386a39bce05133c26361aa18fb98afbcb2fdd" dependencies = [ "crate-git-revision", "serde", @@ -896,7 +854,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.11" -source = "git+https://github.com/stellar/rs-soroban-env?rev=c551daf#c551daf2b2f7fb4eb2d6e12ede320670133b54fe" +source = "git+https://github.com/stellar/rs-soroban-env?rev=995386a#995386a39bce05133c26361aa18fb98afbcb2fdd" dependencies = [ "soroban-env-common", "static_assertions", @@ -905,14 +863,13 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.11" -source = "git+https://github.com/stellar/rs-soroban-env?rev=c551daf#c551daf2b2f7fb4eb2d6e12ede320670133b54fe" +source = "git+https://github.com/stellar/rs-soroban-env?rev=995386a#995386a39bce05133c26361aa18fb98afbcb2fdd" dependencies = [ "backtrace", "curve25519-dalek", "dyn-fmt", "ed25519-dalek", "hex", - "im-rc", "log", "num-derive", "num-integer", @@ -928,7 +885,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.11" -source = "git+https://github.com/stellar/rs-soroban-env?rev=c551daf#c551daf2b2f7fb4eb2d6e12ede320670133b54fe" +source = "git+https://github.com/stellar/rs-soroban-env?rev=995386a#995386a39bce05133c26361aa18fb98afbcb2fdd" dependencies = [ "itertools", "proc-macro2", @@ -950,7 +907,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.11" -source = "git+https://github.com/stellar/rs-soroban-env?rev=c551daf#c551daf2b2f7fb4eb2d6e12ede320670133b54fe" +source = "git+https://github.com/stellar/rs-soroban-env?rev=995386a#995386a39bce05133c26361aa18fb98afbcb2fdd" dependencies = [ "itertools", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index b41dbbd04..00cf3afd5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,17 +35,17 @@ soroban-token-spec = { version = "0.3.2", path = "soroban-token-spec" } [workspace.dependencies.soroban-env-common] version = "0.0.11" git = "https://github.com/stellar/rs-soroban-env" -rev = "c551daf" +rev = "995386a" [workspace.dependencies.soroban-env-guest] version = "0.0.11" git = "https://github.com/stellar/rs-soroban-env" -rev = "c551daf" +rev = "995386a" [workspace.dependencies.soroban-env-host] version = "0.0.11" git = "https://github.com/stellar/rs-soroban-env" -rev = "c551daf" +rev = "995386a" [workspace.dependencies.stellar-strkey] version = "0.0.6" diff --git a/soroban-sdk/src/accounts.rs b/soroban-sdk/src/accounts.rs index 2aaa9d7b6..4d3d611de 100644 --- a/soroban-sdk/src/accounts.rs +++ b/soroban-sdk/src/accounts.rs @@ -6,7 +6,6 @@ use core::{cmp::Ordering, fmt::Debug}; use crate::{ env::internal::xdr, env::internal::{Env as _, EnvBase as _, RawVal, RawValConvertible}, - env::EnvObj, BytesN, ConversionError, Env, IntoVal, Object, TryFromVal, TryIntoVal, }; @@ -76,7 +75,10 @@ impl Accounts { /// /// In tests account identifiers can be generated using [`Accounts`]. #[derive(Clone)] -pub struct AccountId(EnvObj); +pub struct AccountId { + env: Env, + obj: Object, +} impl Debug for AccountId { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { @@ -111,18 +113,9 @@ impl PartialOrd for AccountId { impl Ord for AccountId { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.0.env.check_same_env(&other.0.env); - let v = self - .0 - .env - .obj_cmp(self.0.obj.to_raw(), other.0.obj.to_raw()); - if v == 0 { - Ordering::Equal - } else if v < 0 { - Ordering::Less - } else { - Ordering::Greater - } + self.env.check_same_env(&other.env); + let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw()); + v.cmp(&0) } } @@ -131,10 +124,10 @@ impl TryFromVal for AccountId { fn try_from_val(env: &Env, obj: Object) -> Result { if obj.is_obj_type(xdr::ScObjectType::AccountId) { - Ok(AccountId(EnvObj { + Ok(AccountId { env: env.clone(), obj, - })) + }) } else { Err(ConversionError {}) } @@ -196,7 +189,7 @@ use super::xdr::ScVal; impl TryFrom<&AccountId> for ScVal { type Error = ConversionError; fn try_from(v: &AccountId) -> Result { - ScVal::try_from_val(&v.0.env, v.0.obj.to_raw()) + ScVal::try_from_val(&v.env, v.obj.to_raw()) } } @@ -263,27 +256,27 @@ impl TryIntoVal for super::xdr::AccountId { impl AccountId { pub(crate) unsafe fn unchecked_new(env: Env, obj: Object) -> Self { - Self(EnvObj { env, obj }) + Self { env, obj } } pub fn env(&self) -> &Env { - &self.0.env + &self.env } pub fn as_raw(&self) -> &RawVal { - self.0.obj.as_raw() + self.obj.as_raw() } pub fn as_object(&self) -> &Object { - &self.0.obj + &self.obj } pub fn to_raw(&self) -> RawVal { - self.0.obj.to_raw() + self.obj.to_raw() } pub fn to_object(&self) -> Object { - self.0.obj + self.obj } } diff --git a/soroban-sdk/src/bytes.rs b/soroban-sdk/src/bytes.rs index 6167613b0..c397cf5ae 100644 --- a/soroban-sdk/src/bytes.rs +++ b/soroban-sdk/src/bytes.rs @@ -8,7 +8,7 @@ use core::{ use super::{ env::internal::{Env as _, EnvBase as _, RawValConvertible}, - env::{EnvObj, IntoVal}, + env::IntoVal, xdr::ScObjectType, ConversionError, Env, Object, RawVal, TryFromVal, TryIntoVal, }; @@ -121,8 +121,10 @@ macro_rules! bytesn { /// assert_eq!(slice, [1u8; 32]); /// ``` #[derive(Clone)] -#[repr(transparent)] -pub struct Bytes(EnvObj); +pub struct Bytes { + env: Env, + obj: Object, +} impl Debug for Bytes { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { @@ -155,11 +157,8 @@ impl PartialOrd for Bytes { impl Ord for Bytes { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.0.env.check_same_env(&other.0.env); - let v = self - .0 - .env - .obj_cmp(self.0.obj.to_raw(), other.0.obj.to_raw()); + self.env.check_same_env(&other.env); + let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw()); if v == 0 { Ordering::Equal } else if v < 0 { @@ -233,21 +232,21 @@ impl IntoVal for &Bytes { impl From for RawVal { #[inline(always)] fn from(v: Bytes) -> Self { - v.0.obj.into() + v.obj.into() } } impl From for Object { #[inline(always)] fn from(v: Bytes) -> Self { - v.0.obj + v.obj } } impl From<&Bytes> for Object { #[inline(always)] fn from(v: &Bytes) -> Self { - v.0.obj + v.obj } } @@ -262,7 +261,7 @@ impl From<&Bytes> for Bytes { impl TryFrom<&Bytes> for ScVal { type Error = ConversionError; fn try_from(v: &Bytes) -> Result { - ScVal::try_from_val(&v.0.env, v.0.obj.to_raw()) + ScVal::try_from_val(&v.env, v.obj.to_raw()) } } @@ -320,28 +319,28 @@ impl IntoVal for &[u8; N] { impl Bytes { #[inline(always)] pub(crate) unsafe fn unchecked_new(env: Env, obj: Object) -> Self { - Self(EnvObj { env, obj }) + Self { env, obj } } #[inline(always)] pub fn env(&self) -> &Env { - &self.0.env + &self.env } pub fn as_raw(&self) -> &RawVal { - self.0.obj.as_raw() + self.obj.as_raw() } pub fn to_raw(&self) -> RawVal { - self.0.obj.to_raw() + self.obj.to_raw() } pub fn as_object(&self) -> &Object { - &self.0.obj + &self.obj } pub fn to_object(&self) -> Object { - self.0.obj + self.obj } /// Create an empty Bytes. @@ -359,16 +358,16 @@ impl Bytes { #[inline(always)] pub fn from_slice(env: &Env, items: &[u8]) -> Bytes { - Bytes(EnvObj { + Bytes { env: env.clone(), obj: env.bytes_new_from_slice(items).unwrap_optimized(), - }) + } } #[inline(always)] pub fn set(&mut self, i: u32, v: u8) { let v32: u32 = v.into(); - self.0.obj = self.env().bytes_put(self.0.obj, i.into(), v32.into()) + self.obj = self.env().bytes_put(self.obj, i.into(), v32.into()) } #[inline(always)] @@ -384,7 +383,7 @@ impl Bytes { pub fn get_unchecked(&self, i: u32) -> u8 { let res = self .env() - .bytes_get(self.0.obj, i.into()) + .bytes_get(self.obj, i.into()) .try_into() .unwrap_optimized(); let res32: u32 = unsafe { <_ as RawValConvertible>::unchecked_from_val(res) }; @@ -393,12 +392,12 @@ impl Bytes { #[inline(always)] pub fn is_empty(&self) -> bool { - self.env().bytes_len(self.0.obj).is_u32_zero() + self.env().bytes_len(self.obj).is_u32_zero() } #[inline(always)] pub fn len(&self) -> u32 { - let len = self.env().bytes_len(self.0.obj); + let len = self.env().bytes_len(self.obj); unsafe { <_ as RawValConvertible>::unchecked_from_val(len) } } @@ -413,7 +412,7 @@ impl Bytes { #[inline(always)] pub fn first_unchecked(&self) -> u8 { - let res = self.env().bytes_front(self.0.obj); + let res = self.env().bytes_front(self.obj); let res32: u32 = unsafe { <_ as RawValConvertible>::unchecked_from_val(res) }; res32 as u8 } @@ -429,7 +428,7 @@ impl Bytes { #[inline(always)] pub fn last_unchecked(&self) -> u8 { - let res = self.env().bytes_back(self.0.obj); + let res = self.env().bytes_back(self.obj); let res32: u32 = unsafe { <_ as RawValConvertible>::unchecked_from_val(res) }; res32 as u8 } @@ -446,26 +445,26 @@ impl Bytes { #[inline(always)] pub fn remove_unchecked(&mut self, i: u32) { - self.0.obj = self.env().bytes_del(self.0.obj, i.into()) + self.obj = self.env().bytes_del(self.obj, i.into()) } #[inline(always)] pub fn push(&mut self, x: u8) { let x32: u32 = x.into(); - self.0.obj = self.env().bytes_push(self.0.obj, x32.into()) + self.obj = self.env().bytes_push(self.obj, x32.into()) } #[inline(always)] pub fn pop(&mut self) -> Option { let last = self.last()?; - self.0.obj = self.env().bytes_pop(self.0.obj); + self.obj = self.env().bytes_pop(self.obj); Some(last) } #[inline(always)] pub fn pop_unchecked(&mut self) -> u8 { let last = self.last_unchecked(); - self.0.obj = self.env().bytes_pop(self.0.obj); + self.obj = self.env().bytes_pop(self.obj); last } @@ -478,7 +477,7 @@ impl Bytes { #[inline(always)] pub fn insert(&mut self, i: u32, b: u8) { let b32: u32 = b.into(); - self.0.obj = self.env().bytes_insert(self.0.obj, i.into(), b32.into()) + self.obj = self.env().bytes_insert(self.obj, i.into(), b32.into()) } /// Insert the bytes in `bytes` into this [Bytes] starting at position @@ -522,7 +521,7 @@ impl Bytes { #[inline(always)] pub fn append(&mut self, other: &Bytes) { - self.0.obj = self.env().bytes_append(self.0.obj, other.0.obj) + self.obj = self.env().bytes_append(self.obj, other.obj) } #[inline(always)] @@ -532,7 +531,7 @@ impl Bytes { #[inline(always)] pub fn extend_from_slice(&mut self, slice: &[u8]) { - self.0.obj = self + self.obj = self .env() .bytes_copy_from_slice(self.to_object(), self.len().into(), slice) .unwrap_optimized() @@ -544,7 +543,7 @@ impl Bytes { /// if necessary. #[inline(always)] pub fn copy_from_slice(&mut self, i: u32, slice: &[u8]) { - self.0.obj = self + self.obj = self .env() .bytes_copy_from_slice(self.to_object(), i.into(), slice) .unwrap_optimized() @@ -574,7 +573,7 @@ impl Bytes { Bound::Unbounded => self.len(), }; let env = self.env(); - let bin = env.bytes_slice(self.0.obj, start_bound.into(), end_bound.into()); + let bin = env.bytes_slice(self.obj, start_bound.into(), end_bound.into()); unsafe { Self::unchecked_new(env.clone(), bin) } } @@ -608,7 +607,7 @@ impl Iterator for BinIter { if self.0.is_empty() { None } else { - let val = self.0.env().bytes_front(self.0 .0.obj); + let val = self.0.env().bytes_front(self.0.obj); self.0 = self.0.slice(1..); let val = unsafe { ::unchecked_from_val(val) } as u8; Some(val) @@ -627,7 +626,7 @@ impl DoubleEndedIterator for BinIter { if len == 0 { None } else { - let val = self.0.env().bytes_back(self.0 .0.obj); + let val = self.0.env().bytes_back(self.0.obj); self.0 = self.0.slice(..len - 1); let val = unsafe { ::unchecked_from_val(val) } as u8; Some(val) @@ -901,7 +900,7 @@ impl From<&BytesN> for Bytes { impl TryFrom<&BytesN> for ScVal { type Error = ConversionError; fn try_from(v: &BytesN) -> Result { - ScVal::try_from_val(&v.0 .0.env, v.0 .0.obj.to_raw()) + ScVal::try_from_val(&v.0.env, v.0.obj.to_raw()) } } diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index c1ecce086..1baf29d4c 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -50,12 +50,6 @@ pub use internal::TryFromVal; pub use internal::TryIntoVal; pub use internal::Val; -#[derive(Clone)] -pub struct EnvObj { - pub env: Env, - pub obj: Object, -} - use crate::{ accounts::Accounts, address::Address, crypto::Crypto, deploy::Deployer, events::Events, ledger::Ledger, logging::Logger, storage::Storage, AccountId, Bytes, BytesN, Vec, @@ -209,12 +203,7 @@ impl Env { /// ``` pub fn call_stack(&self) -> Vec<(BytesN<32>, Symbol)> { let stack = internal::Env::get_current_call_stack(self); - unsafe { - Vec::unchecked_new(EnvObj { - env: self.clone(), - obj: stack, - }) - } + unsafe { Vec::unchecked_new(self.clone(), stack) } } #[doc(hidden)] diff --git a/soroban-sdk/src/map.rs b/soroban-sdk/src/map.rs index eaf76da9b..c08e26d12 100644 --- a/soroban-sdk/src/map.rs +++ b/soroban-sdk/src/map.rs @@ -4,7 +4,6 @@ use crate::iter::{UncheckedEnumerable, UncheckedIter}; use super::{ env::internal::{Env as _, EnvBase as _, RawValConvertible}, - env::EnvObj, xdr::ScObjectType, ConversionError, Env, IntoVal, Object, RawVal, Status, TryFromVal, TryIntoVal, Vec, }; @@ -84,9 +83,13 @@ macro_rules! map { /// map![&env, (2, 20), (1, 10)], /// ) /// ``` -#[repr(transparent)] #[derive(Clone)] -pub struct Map(EnvObj, PhantomData, PhantomData); +pub struct Map { + env: Env, + obj: Object, + _k: PhantomData, + _v: PhantomData, +} impl Eq for Map where @@ -121,11 +124,8 @@ where V: IntoVal + TryFromVal, { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.0.env.check_same_env(&other.0.env); - let v = self - .0 - .env - .obj_cmp(self.0.obj.to_raw(), other.0.obj.to_raw()); + self.env.check_same_env(&other.env); + let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw()); if v == 0 { Ordering::Equal } else if v < 0 { @@ -167,14 +167,12 @@ where #[inline(always)] fn try_from_val(env: &Env, obj: Object) -> Result { if obj.is_obj_type(ScObjectType::Map) { - Ok(Map( - EnvObj { - env: env.clone(), - obj, - }, - PhantomData, - PhantomData, - )) + Ok(Map { + env: env.clone(), + obj, + _k: PhantomData, + _v: PhantomData, + }) } else { Err(ConversionError {}) } @@ -244,7 +242,7 @@ where { #[inline(always)] fn from(m: Map) -> Self { - m.0.obj.into() + m.obj.into() } } @@ -252,7 +250,7 @@ where impl TryFrom<&Map> for ScVal { type Error = ConversionError; fn try_from(v: &Map) -> Result { - ScVal::try_from_val(&v.0.env, v.0.obj.to_raw()) + ScVal::try_from_val(&v.env, v.obj.to_raw()) } } @@ -297,42 +295,43 @@ where V: IntoVal + TryFromVal, { #[inline(always)] - pub(crate) unsafe fn unchecked_new(obj: EnvObj) -> Self { - Self(obj, PhantomData, PhantomData) + pub(crate) unsafe fn unchecked_new(env: Env, obj: Object) -> Self { + Self { + env, + obj, + _k: PhantomData, + _v: PhantomData, + } } #[inline(always)] pub fn env(&self) -> &Env { - &self.0.env + &self.env } #[inline(always)] pub fn as_raw(&self) -> &RawVal { - self.0.obj.as_raw() + self.obj.as_raw() } #[inline(always)] pub fn to_raw(&self) -> RawVal { - self.0.obj.to_raw() + self.obj.to_raw() } #[inline(always)] pub(crate) fn as_object(&self) -> &Object { - &self.0.obj + &self.obj } #[inline(always)] pub(crate) fn to_object(&self) -> Object { - self.0.obj + self.obj } #[inline(always)] pub fn new(env: &Env) -> Map { - let obj = EnvObj { - env: env.clone(), - obj: env.map_new(), - }; - unsafe { Self::unchecked_new(obj) } + unsafe { Self::unchecked_new(env.clone(), env.map_new()) } } #[inline(always)] @@ -347,7 +346,7 @@ where #[inline(always)] pub fn contains_key(&self, k: K) -> bool { let env = self.env(); - let has = env.map_has(self.0.obj, k.into_val(env)); + let has = env.map_has(self.obj, k.into_val(env)); has.is_true() } @@ -355,9 +354,9 @@ where pub fn get(&self, k: K) -> Option> { let env = self.env(); let k = k.into_val(env); - let has = env.map_has(self.0.obj, k); + let has = env.map_has(self.obj, k); if has.is_true() { - let v = env.map_get(self.0.obj, k); + let v = env.map_get(self.obj, k); Some(V::try_from_val(env, v)) } else { None @@ -367,31 +366,23 @@ where #[inline(always)] pub fn get_unchecked(&self, k: K) -> Result { let env = self.env(); - let v = env.map_get(self.0.obj, k.into_val(env)); + let v = env.map_get(self.obj, k.into_val(env)); V::try_from_val(env, v) } #[inline(always)] pub fn set(&mut self, k: K, v: V) { let env = self.env(); - let map = env.map_put(self.0.obj, k.into_val(env), v.into_val(env)); - self.0 = EnvObj { - env: env.clone(), - obj: map, - }; + self.obj = env.map_put(self.obj, k.into_val(env), v.into_val(env)); } #[inline(always)] pub fn remove(&mut self, k: K) -> Option<()> { let env = self.env(); let k = k.into_val(env); - let has = env.map_has(self.0.obj, k); + let has = env.map_has(self.obj, k); if has.is_true() { - let map = env.map_del(self.0.obj, k); - self.0 = EnvObj { - env: env.clone(), - obj: map, - }; + self.obj = env.map_del(self.obj, k); Some(()) } else { None @@ -401,38 +392,34 @@ where #[inline(always)] pub fn remove_unchecked(&mut self, k: K) { let env = self.env(); - let map = env.map_del(self.0.obj, k.into_val(env)); - self.0 = EnvObj { - env: env.clone(), - obj: map, - }; + self.obj = env.map_del(self.obj, k.into_val(env)); } #[inline(always)] pub fn is_empty(&self) -> bool { let env = self.env(); - let len = env.map_len(self.0.obj); + let len = env.map_len(self.obj); len.is_u32_zero() } #[inline(always)] pub fn len(&self) -> u32 { let env = self.env(); - let len = env.map_len(self.0.obj); + let len = env.map_len(self.obj); unsafe { ::unchecked_from_val(len) } } #[inline(always)] pub fn keys(&self) -> Vec { let env = self.env(); - let vec = env.map_keys(self.0.obj); + let vec = env.map_keys(self.obj); Vec::::try_from_val(env, vec).unwrap() } #[inline(always)] pub fn values(&self) -> Vec { let env = self.env(); - let vec = env.map_values(self.0.obj); + let vec = env.map_values(self.obj); Vec::::try_from_val(env, vec).unwrap() } @@ -497,13 +484,13 @@ where type Item = Result<(K, V), ConversionError>; fn next(&mut self) -> Option { - let env = &self.0 .0.env; - let key = env.map_min_key(self.0 .0.obj); + let env = &self.0.env; + let key = env.map_min_key(self.0.obj); if Status::try_from(key).is_ok() { return None; } - let value = env.map_get(self.0 .0.obj, key); - self.0 .0.obj = env.map_del(self.0 .0.obj, key); + let value = env.map_get(self.0.obj, key); + self.0.obj = env.map_del(self.0.obj, key); Some(Ok(( match K::try_from_val(env, key) { Ok(k) => k, @@ -530,13 +517,13 @@ where V: IntoVal + TryFromVal, { fn next_back(&mut self) -> Option { - let env = &self.0 .0.env; - let key = env.map_max_key(self.0 .0.obj); + let env = &self.0.env; + let key = env.map_max_key(self.0.obj); if Status::try_from(key).is_ok() { return None; } - let value = env.map_get(self.0 .0.obj, key); - self.0 .0.obj = env.map_del(self.0 .0.obj, key); + let value = env.map_get(self.0.obj, key); + self.0.obj = env.map_del(self.0.obj, key); Some(Ok(( match K::try_from_val(env, key) { Ok(k) => k, diff --git a/soroban-sdk/src/set.rs b/soroban-sdk/src/set.rs index 61e086258..69270fb23 100644 --- a/soroban-sdk/src/set.rs +++ b/soroban-sdk/src/set.rs @@ -1,8 +1,8 @@ use core::{cmp::Ordering, fmt::Debug, iter::FusedIterator}; use super::{ - env::internal::Env as _, env::EnvObj, xdr::ScObjectType, ConversionError, Env, IntoVal, Map, - Object, RawVal, TryFromVal, TryIntoVal, Vec, + env::internal::Env as _, xdr::ScObjectType, ConversionError, Env, IntoVal, Map, Object, RawVal, + TryFromVal, TryIntoVal, Vec, }; /// Create a [Set] with the given items. @@ -73,8 +73,8 @@ where Self(map) } - unsafe fn unchecked_new(obj: EnvObj) -> Self { - let map = Map::unchecked_new(obj); + unsafe fn unchecked_new(env: Env, obj: Object) -> Self { + let map = Map::unchecked_new(env, obj); Self(map) } @@ -283,12 +283,7 @@ where fn try_from_val(env: &Env, obj: Object) -> Result { if obj.is_obj_type(ScObjectType::Map) { - Ok(unsafe { - Set::::unchecked_new(EnvObj { - env: env.clone(), - obj, - }) - }) + Ok(unsafe { Set::::unchecked_new(env.clone(), obj) }) } else { Err(ConversionError {}) } diff --git a/soroban-sdk/src/vec.rs b/soroban-sdk/src/vec.rs index 74b85d40b..6cdde5f71 100644 --- a/soroban-sdk/src/vec.rs +++ b/soroban-sdk/src/vec.rs @@ -10,7 +10,7 @@ use core::{ use crate::iter::{UncheckedEnumerable, UncheckedIter}; use super::{ - env::{internal::Env as _, internal::EnvBase as _, EnvObj, RawValConvertible}, + env::{internal::Env as _, internal::EnvBase as _, RawValConvertible}, xdr::ScObjectType, ConversionError, Env, IntoVal, Object, RawVal, Set, TryFromVal, TryIntoVal, }; @@ -114,8 +114,11 @@ impl_into_vec_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 /// [im_rc::Vector]: https://docs.rs/im-rc/latest/im_rc/struct.Vector.html /// [rrb]: https://infoscience.epfl.ch/record/213452/files/rrbvector.pdf #[derive(Clone)] -#[repr(transparent)] -pub struct Vec(EnvObj, PhantomData); +pub struct Vec { + env: Env, + obj: Object, + _t: PhantomData, +} impl Eq for Vec where T: IntoVal + TryFromVal {} @@ -142,11 +145,8 @@ where T: IntoVal + TryFromVal, { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.0.env.check_same_env(&other.0.env); - let v = self - .0 - .env - .obj_cmp(self.0.obj.to_raw(), other.0.obj.to_raw()); + self.env.check_same_env(&other.env); + let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw()); if v == 0 { Ordering::Equal } else if v < 0 { @@ -178,13 +178,13 @@ where impl IntoVal> for Vec { fn into_val(self, _env: &Env) -> Vec { - unsafe { Vec::unchecked_new(self.0) } + unsafe { Vec::unchecked_new(self.env, self.obj) } } } impl IntoVal> for &Vec { fn into_val(self, _env: &Env) -> Vec { - unsafe { Vec::unchecked_new(self.0.clone()) } + unsafe { Vec::unchecked_new(self.env.clone(), self.obj.clone()) } } } @@ -197,12 +197,7 @@ where #[inline(always)] fn try_from_val(env: &Env, obj: Object) -> Result { if obj.is_obj_type(ScObjectType::Vec) { - Ok(unsafe { - Vec::::unchecked_new(EnvObj { - env: env.clone(), - obj, - }) - }) + Ok(unsafe { Vec::::unchecked_new(env.clone(), obj) }) } else { Err(ConversionError {}) } @@ -267,7 +262,7 @@ where { #[inline(always)] fn from(v: Vec) -> Self { - v.0.obj.into() + v.obj.into() } } @@ -286,7 +281,7 @@ where { #[inline(always)] fn from(v: Vec) -> Self { - v.0.obj + v.obj } } @@ -310,7 +305,7 @@ use super::xdr::{ScObject, ScVal, ScVec}; impl TryFrom<&Vec> for ScVal { type Error = ConversionError; fn try_from(v: &Vec) -> Result { - ScVal::try_from_val(&v.0.env, v.0.obj.to_raw()) + ScVal::try_from_val(&v.env, v.obj.to_raw()) } } @@ -435,28 +430,32 @@ where impl Vec { #[inline(always)] - pub(crate) unsafe fn unchecked_new(obj: EnvObj) -> Self { - Self(obj, PhantomData) + pub(crate) unsafe fn unchecked_new(env: Env, obj: Object) -> Self { + Self { + env, + obj, + _t: PhantomData, + } } pub fn env(&self) -> &Env { - &self.0.env + &self.env } pub fn as_raw(&self) -> &RawVal { - self.0.obj.as_raw() + self.obj.as_raw() } pub fn to_raw(&self) -> RawVal { - self.0.obj.to_raw() + self.obj.to_raw() } pub fn as_object(&self) -> &Object { - &self.0.obj + &self.obj } pub fn to_object(&self) -> Object { - self.0.obj + self.obj } } @@ -466,13 +465,7 @@ where { #[inline(always)] pub fn new(env: &Env) -> Vec { - let obj = env.vec_new(().into()); - unsafe { - Self::unchecked_new(EnvObj { - env: env.clone(), - obj, - }) - } + unsafe { Self::unchecked_new(env.clone(), env.vec_new(().into())) } } #[inline(always)] @@ -496,7 +489,7 @@ where pub fn get(&self, i: u32) -> Option> { if i < self.len() { let env = self.env(); - let val = env.vec_get(self.0.obj, i.into()); + let val = env.vec_get(self.obj, i.into()); Some(T::try_from_val(env, val)) } else { None @@ -509,18 +502,14 @@ where T::Error: Debug, { let env = self.env(); - let val = env.vec_get(self.0.obj, i.into()); + let val = env.vec_get(self.obj, i.into()); T::try_from_val(env, val) } #[inline(always)] pub fn set(&mut self, i: u32, v: T) { let env = self.env(); - let obj = env.vec_put(self.0.obj, i.into(), v.into_val(env)); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_put(self.obj, i.into(), v.into_val(env)); } #[inline(always)] @@ -536,46 +525,34 @@ where #[inline(always)] pub fn remove_unchecked(&mut self, i: u32) { let env = self.env(); - let obj = env.vec_del(self.0.obj, i.into()); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_del(self.obj, i.into()); } #[inline(always)] pub fn is_empty(&self) -> bool { let env = self.env(); - let val = env.vec_len(self.0.obj); + let val = env.vec_len(self.obj); val.is_u32_zero() } #[inline(always)] pub fn len(&self) -> u32 { let env = self.env(); - let val = env.vec_len(self.0.obj); + let val = env.vec_len(self.obj); unsafe { <_ as RawValConvertible>::unchecked_from_val(val) } } #[inline(always)] pub fn push_front(&mut self, x: T) { let env = self.env(); - let obj = env.vec_push_front(self.0.obj, x.into_val(env)); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_push_front(self.obj, x.into_val(env)); } #[inline(always)] pub fn pop_front(&mut self) -> Option> { let last = self.last()?; let env = self.env(); - let obj = env.vec_pop_front(self.0.obj); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_pop_front(self.obj); Some(last) } @@ -583,33 +560,21 @@ where pub fn pop_front_unchecked(&mut self) -> Result { let last = self.first_unchecked(); let env = self.env(); - let obj = env.vec_pop_front(self.0.obj); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_pop_front(self.obj); last } #[inline(always)] pub fn push_back(&mut self, x: T) { let env = self.env(); - let obj = env.vec_push_back(self.0.obj, x.into_val(env)); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_push_back(self.obj, x.into_val(env)); } #[inline(always)] pub fn pop_back(&mut self) -> Option> { let last = self.last()?; let env = self.env(); - let obj = env.vec_pop_back(self.0.obj); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_pop_back(self.obj); Some(last) } @@ -617,11 +582,7 @@ where pub fn pop_back_unchecked(&mut self) -> Result { let last = self.last_unchecked(); let env = self.env(); - let obj = env.vec_pop_back(self.0.obj); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_pop_back(self.obj); last } @@ -648,16 +609,16 @@ where if self.is_empty() { None } else { - let env = &self.0.env; - let val = env.vec_front(self.0.obj); + let env = &self.env; + let val = env.vec_front(self.obj); Some(T::try_from_val(env, val)) } } #[inline(always)] pub fn first_unchecked(&self) -> Result { - let env = &self.0.env; - let val = env.vec_front(self.0.obj); + let env = &self.env; + let val = env.vec_front(self.obj); T::try_from_val(env, val) } @@ -667,7 +628,7 @@ where None } else { let env = self.env(); - let val = env.vec_back(self.0.obj); + let val = env.vec_back(self.obj); Some(T::try_from_val(env, val)) } } @@ -675,28 +636,20 @@ where #[inline(always)] pub fn last_unchecked(&self) -> Result { let env = self.env(); - let val = env.vec_back(self.0.obj); + let val = env.vec_back(self.obj); T::try_from_val(env, val) } #[inline(always)] pub fn insert(&mut self, i: u32, x: T) { let env = self.env(); - let obj = env.vec_put(self.0.obj, i.into(), x.into_val(env)); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_put(self.obj, i.into(), x.into_val(env)); } #[inline(always)] pub fn append(&mut self, other: &Vec) { let env = self.env(); - let obj = env.vec_append(self.0.obj, other.0.obj); - self.0 = EnvObj { - env: env.clone(), - obj, - }; + self.obj = env.vec_append(self.obj, other.obj); } #[inline(always)] @@ -729,12 +682,8 @@ where Bound::Unbounded => self.len(), }; let env = self.env(); - let obj = env.vec_slice(self.0.obj, start_bound.into(), end_bound.into()); - let vec = EnvObj { - env: env.clone(), - obj, - }; - unsafe { Self::unchecked_new(vec) } + let obj = env.vec_slice(self.obj, start_bound.into(), end_bound.into()); + unsafe { Self::unchecked_new(env.clone(), obj) } } pub fn iter(&self) -> VecIter @@ -772,7 +721,7 @@ where pub fn contains(&self, item: impl Borrow) -> bool { let env = self.env(); let val = item.borrow().into_val(env); - !env.vec_first_index_of(self.0.obj, val).is_void() + !env.vec_first_index_of(self.obj, val).is_void() } /// Returns the index of the first occurrence of the item. @@ -782,7 +731,7 @@ where pub fn first_index_of(&self, item: impl Borrow) -> Option { let env = self.env(); let val = item.borrow().into_val(env); - env.vec_first_index_of(self.0.obj, val) + env.vec_first_index_of(self.obj, val) .try_into_val(env) .unwrap() } @@ -794,7 +743,7 @@ where pub fn last_index_of(&self, item: impl Borrow) -> Option { let env = self.env(); let val = item.borrow().into_val(env); - env.vec_last_index_of(self.0.obj, val) + env.vec_last_index_of(self.obj, val) .try_into_val(env) .unwrap() } @@ -812,7 +761,7 @@ where pub fn binary_search(&self, item: impl Borrow) -> Result { let env = self.env(); let val = item.borrow().into_val(env); - let high_low = env.vec_binary_search(self.0.obj, val); + let high_low = env.vec_binary_search(self.obj, val); let high: u32 = (high_low >> u32::BITS) as u32; let low: u32 = high_low as u32; if high == 1 { @@ -870,7 +819,7 @@ where if len == 0 { None } else { - let val = self.0.env().vec_front(self.0 .0.obj); + let val = self.0.env().vec_front(self.0.obj); self.0 = self.0.slice(1..); Some(T::try_from_val(self.0.env(), val)) } @@ -894,7 +843,7 @@ where if len == 0 { None } else { - let val = self.0.env().vec_back(self.0 .0.obj); + let val = self.0.env().vec_back(self.0.obj); self.0 = self.0.slice(..len - 1); Some(T::try_from_val(self.0.env(), val)) } From 3b84f63118459d7cfd3598050590a3c5fae6daae Mon Sep 17 00:00:00 2001 From: Jay Geng Date: Tue, 13 Dec 2022 21:56:58 -0500 Subject: [PATCH 4/4] Clean up cmp --- soroban-sdk/src/bytes.rs | 8 +------- soroban-sdk/src/map.rs | 8 +------- soroban-sdk/src/vec.rs | 8 +------- 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/soroban-sdk/src/bytes.rs b/soroban-sdk/src/bytes.rs index c397cf5ae..446dd8bd2 100644 --- a/soroban-sdk/src/bytes.rs +++ b/soroban-sdk/src/bytes.rs @@ -159,13 +159,7 @@ impl Ord for Bytes { fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.env.check_same_env(&other.env); let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw()); - if v == 0 { - Ordering::Equal - } else if v < 0 { - Ordering::Less - } else { - Ordering::Greater - } + v.cmp(&0) } } diff --git a/soroban-sdk/src/map.rs b/soroban-sdk/src/map.rs index c08e26d12..4d3488ea0 100644 --- a/soroban-sdk/src/map.rs +++ b/soroban-sdk/src/map.rs @@ -126,13 +126,7 @@ where fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.env.check_same_env(&other.env); let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw()); - if v == 0 { - Ordering::Equal - } else if v < 0 { - Ordering::Less - } else { - Ordering::Greater - } + v.cmp(&0) } } diff --git a/soroban-sdk/src/vec.rs b/soroban-sdk/src/vec.rs index 6cdde5f71..460e5c154 100644 --- a/soroban-sdk/src/vec.rs +++ b/soroban-sdk/src/vec.rs @@ -147,13 +147,7 @@ where fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.env.check_same_env(&other.env); let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw()); - if v == 0 { - Ordering::Equal - } else if v < 0 { - Ordering::Less - } else { - Ordering::Greater - } + v.cmp(&0) } }