Skip to content

Commit

Permalink
Merge branch 'master' into esdrubal/fix_impl_self_duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
IGI-111 authored Sep 3, 2024
2 parents 8659f70 + 8cb7796 commit 4e693d3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
11 changes: 6 additions & 5 deletions sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,12 @@ impl Bytes {
(left_bytes, right_bytes)
}

/// Moves all elements of `other` into `self`, leaving `other` empty.
/// Copies all elements of `other` into `self`
///
/// # Additional Information
///
/// NOTE: Appending `self` to itself will duplicate the `Bytes`. i.e. [0, 1, 2] => [0, 1, 2, 0, 1, 2]
/// This function differs from the rust `append` function in that it does not clear the `other` `Bytes`
///
/// # Arguments
///
Expand Down Expand Up @@ -696,7 +701,6 @@ impl Bytes {
// optimization for when starting with empty bytes and appending to it
if self.len == 0 {
self = other;
other.clear();
return;
};

Expand All @@ -717,9 +721,6 @@ impl Bytes {

// set capacity and length
self.len = both_len;

// clear `other`
other.clear();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,82 +62,82 @@
{
"concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903",
"name": "BOOL",
"offset": 7432
"offset": 7208
},
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"name": "U8",
"offset": 7624
"offset": 7400
},
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"name": "ANOTHER_U8",
"offset": 7360
"offset": 7136
},
{
"concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef",
"name": "U16",
"offset": 7568
"offset": 7344
},
{
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"name": "U32",
"offset": 7608
"offset": 7384
},
{
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"name": "U64",
"offset": 7616
"offset": 7392
},
{
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
"name": "U256",
"offset": 7576
"offset": 7352
},
{
"concreteTypeId": "7c5ee1cecf5f8eacd1284feb5f0bf2bdea533a51e2f0c9aabe9236d335989f3b",
"name": "B256",
"offset": 7400
"offset": 7176
},
{
"concreteTypeId": "81fc10c4681a3271cf2d66b2ec6fbc8ed007a442652930844fcf11818c295bff",
"name": "CONFIGURABLE_STRUCT",
"offset": 7520
"offset": 7296
},
{
"concreteTypeId": "a2922861f03be8a650595dd76455b95383a61b46dd418f02607fa2e00dc39d5c",
"name": "CONFIGURABLE_ENUM_A",
"offset": 7440
"offset": 7216
},
{
"concreteTypeId": "a2922861f03be8a650595dd76455b95383a61b46dd418f02607fa2e00dc39d5c",
"name": "CONFIGURABLE_ENUM_B",
"offset": 7480
"offset": 7256
},
{
"concreteTypeId": "4926d35d1a5157936b0a29bc126b8aace6d911209a5c130e9b716b0c73643ea6",
"name": "ARRAY_BOOL",
"offset": 7368
"offset": 7144
},
{
"concreteTypeId": "776fb5a3824169d6736138565fdc20aad684d9111266a5ff6d5c675280b7e199",
"name": "ARRAY_U64",
"offset": 7376
"offset": 7152
},
{
"concreteTypeId": "c998ca9a5f221fe7b5c66ae70c8a9562b86d964408b00d17f883c906bc1fe4be",
"name": "TUPLE_BOOL_U64",
"offset": 7552
"offset": 7328
},
{
"concreteTypeId": "94f0fa95c830be5e4f711963e83259fe7e8bc723278ab6ec34449e791a99b53a",
"name": "STR_4",
"offset": 7544
"offset": 7320
},
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"name": "NOT_USED",
"offset": 7536
"offset": 7312
}
],
"encodingVersion": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use basic_storage_abi::{BasicStorage, Quad};
#[cfg(experimental_new_encoding = false)]
const CONTRACT_ID = 0x94db39f409a31b9f2ebcadeea44378e419208c20de90f5d8e1e33dc1523754cb;
#[cfg(experimental_new_encoding = true)]
const CONTRACT_ID = 0x882ef2f70013d9437c7e727c3e41bb2918441c7804fca647d38245d7309ee5df; // AUTO-CONTRACT-ID ../../test_contracts/basic_storage --release
const CONTRACT_ID = 0xa4ade9ba5b85a85758300bee7a0ea1b506d5f967e6e183ad6b09513b3925e59d; // AUTO-CONTRACT-ID ../../test_contracts/basic_storage --release

fn main() -> u64 {
let addr = abi(BasicStorage, CONTRACT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,14 @@ fn bytes_append() {
bytes.append(bytes2);
assert(bytes.len() == first_length + second_length);
assert(bytes.capacity() == first_length + first_length);
assert(bytes2.is_empty());

assert(bytes2.len() == second_length);
assert(!bytes2.is_empty());

assert(bytes2.get(0).unwrap() == d);
assert(bytes2.get(1).unwrap() == e);
assert(bytes2.get(2).unwrap() == f);

let values = [a, b, c, d, e, f];
let mut i = 0;
while i < 6 {
Expand Down Expand Up @@ -757,7 +764,12 @@ fn bytes_append_to_empty() {
empty_bytes.append(bytes);
assert(empty_bytes.len() == bytes_length);
assert(empty_bytes.capacity() == bytes_original_capacity);
assert(bytes.is_empty());

assert(bytes.len() == bytes_length);
assert(!bytes.is_empty());
assert(bytes.get(0).unwrap() == a);
assert(bytes.get(1).unwrap() == b);
assert(bytes.get(2).unwrap() == c);

let values = [a, b, c];
let mut i = 0;
Expand All @@ -767,6 +779,32 @@ fn bytes_append_to_empty() {
};
}

fn bytes_append_self() {
let (mut bytes, a, b, c) = setup();
assert(bytes.len() == 3);
assert(bytes.get(0).unwrap() == a);
assert(bytes.get(1).unwrap() == b);
assert(bytes.get(2).unwrap() == c);

bytes.append(bytes);

assert(bytes.len() == 6);
assert(bytes.get(0).unwrap() == a);
assert(bytes.get(1).unwrap() == b);
assert(bytes.get(2).unwrap() == c);
assert(bytes.get(3).unwrap() == a);
assert(bytes.get(4).unwrap() == b);
assert(bytes.get(5).unwrap() == c);
}

fn bytes_append_empty_self() {
let mut empty_bytes = Bytes::new();

empty_bytes.append(empty_bytes);

assert(empty_bytes.len() == 0);
}

#[test()]
fn bytes_eq() {
let (mut bytes, _a, _b, _c) = setup();
Expand Down

0 comments on commit 4e693d3

Please # to comment.