Skip to content

Commit

Permalink
ManagedVec is_single_item, some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Dec 31, 2024
1 parent a56917f commit a21448e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
38 changes: 26 additions & 12 deletions framework/base/src/types/managed/wrapped/managed_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,20 @@ where
self.byte_len() == 0
}

pub fn try_get(&self, index: usize) -> Option<T::Ref<'_>> {
/// Internal function that loads the payload for an item.
///
/// Payload passed as mutable reference, to avoid copying of bytes around the stack.
fn load_item_payload(&self, index: usize, payload: &mut T::PAYLOAD) -> bool {
let byte_index = index * T::payload_size();

let mut payload = T::PAYLOAD::new_buffer();
if self
.buffer
self.buffer
.load_slice(byte_index, payload.payload_slice_mut())
.is_ok()
{
}

pub fn try_get(&self, index: usize) -> Option<T::Ref<'_>> {
let mut payload = T::PAYLOAD::new_buffer();
if self.load_item_payload(index, &mut payload) {
unsafe { Some(T::borrow_from_payload(&payload)) }
} else {
None
Expand Down Expand Up @@ -182,21 +187,30 @@ where
}
}

/// If it contains precisely one item, will return `Some` with a reference to that item.
///
/// Will return `None` for zero or more than one item.
pub fn is_single_item(&self) -> Option<T::Ref<'_>> {
let mut payload = T::PAYLOAD::new_buffer();
if self.len() == 1 {
let _ = self.load_item_payload(0, &mut payload);
unsafe { Some(T::borrow_from_payload(&payload)) }
} else {
None
}
}

pub fn get_mut(&mut self, index: usize) -> ManagedVecRefMut<M, T> {
ManagedVecRefMut::new(self.get_handle(), index)
}

pub(super) unsafe fn get_unsafe(&self, index: usize) -> T {
let byte_index = index * T::payload_size();
let mut payload = T::PAYLOAD::new_buffer();
if self
.buffer
.load_slice(byte_index, payload.payload_slice_mut())
.is_err()
{
if self.load_item_payload(index, &mut payload) {
T::read_from_payload(&payload)
} else {
M::error_api_impl().signal_error(INDEX_OUT_OF_RANGE_MSG);
}
T::read_from_payload(&payload)
}

pub fn set(&mut self, index: usize, item: T) -> Result<(), InvalidSliceError> {
Expand Down
17 changes: 16 additions & 1 deletion framework/scenario/tests/managed_vec_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Deref;

use multiversx_sc::types::{BigUint, ManagedVec};
use multiversx_sc::types::{BigUint, ManagedRef, ManagedVec};
use multiversx_sc_scenario::api::StaticApi;

#[test]
Expand Down Expand Up @@ -595,3 +595,18 @@ fn test_managed_vec_get_mut() {
assert_eq!(*managed_vec.get(0), 200u32);
assert_eq!(*managed_vec.get(1), 300u32);
}

#[test]
fn test_is_single_item() {
let mut managed_vec = ManagedVec::<StaticApi, BigUint<StaticApi>>::new();
assert!(managed_vec.is_single_item().is_none());

managed_vec.push(BigUint::<StaticApi>::from(1u32));
assert_eq!(
managed_vec.is_single_item(),
Some(ManagedRef::new(&BigUint::<StaticApi>::from(1u32)))
);

managed_vec.push(BigUint::<StaticApi>::from(2u32));
assert!(managed_vec.is_single_item().is_none());
}

0 comments on commit a21448e

Please sign in to comment.