Skip to content

Commit

Permalink
Use the Self annotation in implementation blocks (FuelLabs#4871)
Browse files Browse the repository at this point in the history
## Description

In Rust, when implementing traits the type `Self` is used instead of the
concrete type. This provides a more flexible and generic API design and
it promotes code reusability and simplifies the creation of fluent
interfaces. The same has now been done for Sway.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: bitzoic <[email protected]>
  • Loading branch information
bitzoic and bitzoic authored Jul 26, 2023
1 parent cab7e33 commit 660abd9
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 114 deletions.
16 changes: 8 additions & 8 deletions sway-lib-core/src/primitive_conversions.sw
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl u64 {
}
}

pub fn from_le_bytes(bytes: [u8; 8]) -> u64 {
pub fn from_le_bytes(bytes: [u8; 8]) -> Self {
let a = bytes[0];
let b = bytes[1];
let c = bytes[2];
Expand Down Expand Up @@ -109,7 +109,7 @@ impl u64 {
}
}

pub fn from_be_bytes(bytes: [u8; 8]) -> u64 {
pub fn from_be_bytes(bytes: [u8; 8]) -> Self {
let a = bytes[0];
let b = bytes[1];
let c = bytes[2];
Expand Down Expand Up @@ -170,7 +170,7 @@ impl u32 {
}
}

pub fn from_le_bytes(bytes: [u8; 4]) -> u32 {
pub fn from_le_bytes(bytes: [u8; 4]) -> Self {
asm(a: bytes[0], b: bytes[1], c: bytes[2], d: bytes[3], i: 0x8, j: 0x10, k: 0x18, r1, r2, r3) {
sll r1 c j;
sll r2 d k;
Expand Down Expand Up @@ -205,7 +205,7 @@ impl u32 {
}
}

pub fn from_be_bytes(bytes: [u8; 4]) -> u32 {
pub fn from_be_bytes(bytes: [u8; 4]) -> Self {
asm(a: bytes[0], b: bytes[1], c: bytes[2], d: bytes[3], i: 0x8, j: 0x10, k: 0x18, r1, r2, r3) {
sll r1 a k;
sll r2 b j;
Expand Down Expand Up @@ -246,7 +246,7 @@ impl u16 {
}
}

pub fn from_le_bytes(bytes: [u8; 2]) -> u16 {
pub fn from_le_bytes(bytes: [u8; 2]) -> Self {
asm(a: bytes[0], b: bytes[1], i: 0x8, r1) {
sll r1 b i;
or r1 a r1;
Expand All @@ -268,7 +268,7 @@ impl u16 {
}
}

pub fn from_be_bytes(bytes: [u8; 2]) -> u16 {
pub fn from_be_bytes(bytes: [u8; 2]) -> Self {
asm(a: bytes[0], b: bytes[1], i: 0x8, r1) {
sll r1 a i;
or r1 r1 b;
Expand Down Expand Up @@ -315,7 +315,7 @@ impl b256 {
output
}

pub fn from_le_bytes(bytes: [u8; 32]) -> b256 {
pub fn from_le_bytes(bytes: [u8; 32]) -> Self {
let a = u64::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]]);
let b = u64::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]]);
let c = u64::from_le_bytes([bytes[16], bytes[17], bytes[18], bytes[19], bytes[20], bytes[21], bytes[22], bytes[23]]);
Expand Down Expand Up @@ -343,7 +343,7 @@ impl b256 {
output
}

pub fn from_be_bytes(bytes: [u8; 32]) -> b256 {
pub fn from_be_bytes(bytes: [u8; 32]) -> Self {
let a = u64::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]]);
let b = u64::from_be_bytes([bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]]);
let c = u64::from_be_bytes([bytes[16], bytes[17], bytes[18], bytes[19], bytes[20], bytes[21], bytes[22], bytes[23]]);
Expand Down
20 changes: 10 additions & 10 deletions sway-lib-core/src/primitives.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ library;

impl u64 {
/// The smallest value that can be represented by this integer type.
pub fn min() -> u64 {
pub fn min() -> Self {
0
}

/// The largest value that can be represented by this integer type,
/// 2<sup>64</sup> - 1.
pub fn max() -> u64 {
pub fn max() -> Self {
18446744073709551615
}

Expand All @@ -20,13 +20,13 @@ impl u64 {

impl u32 {
/// The smallest value that can be represented by this integer type.
pub fn min() -> u32 {
pub fn min() -> Self {
0
}

/// The largest value that can be represented by this integer type,
/// 2<sup>32</sup> - 1.
pub fn max() -> u32 {
pub fn max() -> Self {
4294967295
}

Expand All @@ -38,13 +38,13 @@ impl u32 {

impl u16 {
/// The smallest value that can be represented by this integer type.
pub fn min() -> u16 {
pub fn min() -> Self {
0
}

/// The largest value that can be represented by this integer type,
/// 2<sup>16</sup> - 1.
pub fn max() -> u16 {
pub fn max() -> Self {
65535
}

Expand All @@ -56,13 +56,13 @@ impl u16 {

impl u8 {
/// The smallest value that can be represented by this integer type.
pub fn min() -> u8 {
pub fn min() -> Self {
0
}

/// The largest value that can be represented by this integer type,
/// 2<sup>8</sup> - 1.
pub fn max() -> u8 {
pub fn max() -> Self {
255
}

Expand All @@ -74,13 +74,13 @@ impl u8 {

impl b256 {
/// The smallest value that can be represented by this type.
pub fn min() -> b256 {
pub fn min() -> Self {
0x0000000000000000000000000000000000000000000000000000000000000000
}

/// The largest value that can be represented by this type,
/// 2<sup>256</sup> - 1.
pub fn max() -> b256 {
pub fn max() -> Self {
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
}

Expand Down
12 changes: 6 additions & 6 deletions sway-lib-core/src/raw_ptr.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ impl raw_ptr {
}

/// Calculates the offset from the pointer.
pub fn add<T>(self, count: u64) -> raw_ptr {
pub fn add<T>(self, count: u64) -> Self {
__ptr_add::<T>(self, count)
}

/// Calculates the offset from the pointer.
pub fn sub<T>(self, count: u64) -> raw_ptr {
pub fn sub<T>(self, count: u64) -> Self {
__ptr_sub::<T>(self, count)
}

Expand All @@ -30,7 +30,7 @@ impl raw_ptr {
}

/// Copies `count * size_of<T>` bytes from `self` to `dst`.
pub fn copy_to<T>(self, dst: raw_ptr, count: u64) {
pub fn copy_to<T>(self, dst: Self, count: u64) {
let len = __mul(count, __size_of::<T>());
asm(dst: dst, src: self, len: len) {
mcp dst src len;
Expand Down Expand Up @@ -67,22 +67,22 @@ impl raw_ptr {
}

/// Copies `count` bytes from `self` to `dst`
pub fn copy_bytes_to(self, dst: raw_ptr, count: u64) {
pub fn copy_bytes_to(self, dst: Self, count: u64) {
asm(dst: dst, src: self, len: count) {
mcp dst src len;
};
}

/// Add a u64 offset to a raw_ptr
pub fn add_uint_offset(self, offset: u64) -> raw_ptr {
pub fn add_uint_offset(self, offset: u64) -> Self {
asm(ptr: self, offset: offset, new) {
add new ptr offset;
new: raw_ptr
}
}

/// Subtract a u64 offset from a raw_ptr
pub fn sub_uint_offset(self, offset: u64) -> raw_ptr {
pub fn sub_uint_offset(self, offset: u64) -> Self {
asm(ptr: self, offset: offset, new) {
sub new ptr offset;
new: raw_ptr
Expand Down
4 changes: 2 additions & 2 deletions sway-lib-std/src/address.sw
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ impl core::ops::Eq for Address {

/// Functions for casting between the `b256` and `Address` types.
impl From<b256> for Address {
fn from(bits: b256) -> Address {
Address { value: bits }
fn from(bits: b256) -> Self {
Self { value: bits }
}

fn into(self) -> b256 {
Expand Down
8 changes: 4 additions & 4 deletions sway-lib-std/src/b512.sw
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ impl core::ops::Eq for B512 {

/// Functions for casting between `B512` and an array of two `b256`s.
impl From<(b256, b256)> for B512 {
fn from(components: (b256, b256)) -> B512 {
B512 {
fn from(components: (b256, b256)) -> Self {
Self {
bytes: [components.0, components.1],
}
}
Expand All @@ -33,8 +33,8 @@ impl From<(b256, b256)> for B512 {
/// Methods on the `B512` type.
impl B512 {
/// Initializes a new, zeroed `B512`.
pub fn new() -> B512 {
B512 {
pub fn new() -> Self {
Self {
bytes: [ZERO_B256, ZERO_B256],
}
}
Expand Down
12 changes: 6 additions & 6 deletions sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Bytes {
/// assert(bytes.capacity() == 0);
/// ```
pub fn new() -> Self {
Bytes {
Self {
buf: RawBytes::new(),
len: 0,
}
Expand All @@ -98,7 +98,7 @@ impl Bytes {
/// bytes.push(10);
/// ```
pub fn with_capacity(capacity: u64) -> Self {
Bytes {
Self {
buf: RawBytes::with_capacity(capacity),
len: 0,
}
Expand Down Expand Up @@ -513,7 +513,7 @@ impl Bytes {
/// assert(left.len() == 1);
/// assert(right.len() == 2);
/// ```
pub fn split_at(self, mid: u64) -> (Bytes, Bytes) {
pub fn split_at(self, mid: u64) -> (Self, Self) {
assert(self.len >= mid);

let left_len = mid;
Expand Down Expand Up @@ -624,9 +624,9 @@ impl AsRawSlice for Bytes {

/// Methods for converting between the `Bytes` and the `b256` types.
impl From<b256> for Bytes {
fn from(b: b256) -> Bytes {
fn from(b: b256) -> Self {
// Artificially create bytes with capacity and len
let mut bytes = Bytes::with_capacity(32);
let mut bytes = Self::with_capacity(32);
bytes.len = 32;
// Copy bytes from contract_id into the buffer of the target bytes
__addr_of(b).copy_bytes_to(bytes.buf.ptr, 32);
Expand Down Expand Up @@ -734,7 +734,7 @@ impl From<Vec<u8>> for Bytes {
/// assert(bytes.get(2).unwrap() == c);
/// ```
fn from(vec: Vec<u8>) -> Self {
let mut bytes = Bytes::with_capacity(vec.len());
let mut bytes = Self::with_capacity(vec.len());
let mut i = 0;
while i < vec.len() {
bytes.push(vec.get(i).unwrap());
Expand Down
6 changes: 3 additions & 3 deletions sway-lib-std/src/contract_id.sw
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ impl core::ops::Eq for ContractId {

/// Functions for casting between the `b256` and `ContractId` types.
impl From<b256> for ContractId {
fn from(bits: b256) -> ContractId {
ContractId { value: bits }
fn from(bits: b256) -> Self {
Self { value: bits }
}

fn into(self) -> b256 {
Expand Down Expand Up @@ -89,7 +89,7 @@ impl ContractId {
asm(r1: amount) {
mint r1;
};
self.transfer(amount, ContractId::from(asm() { fp: b256 })); // Transfer the self contract token
self.transfer(amount, Self::from(asm() { fp: b256 })); // Transfer the self contract token
}
}

Expand Down
20 changes: 10 additions & 10 deletions sway-lib-std/src/identity.sw
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ impl core::ops::Eq for Identity {
impl Identity {
pub fn as_address(self) -> Option<Address> {
match self {
Identity::Address(addr) => Option::Some(addr),
Identity::ContractId(_) => Option::None,
Self::Address(addr) => Option::Some(addr),
Self::ContractId(_) => Option::None,
}
}

pub fn as_contract_id(self) -> Option<ContractId> {
match self {
Identity::Address(_) => Option::None,
Identity::ContractId(id) => Option::Some(id),
Self::Address(_) => Option::None,
Self::ContractId(id) => Option::Some(id),
}
}

pub fn is_address(self) -> bool {
match self {
Identity::Address(_) => true,
Identity::ContractId(_) => false,
Self::Address(_) => true,
Self::ContractId(_) => false,
}
}

pub fn is_contract_id(self) -> bool {
match self {
Identity::Address(_) => false,
Identity::ContractId(_) => true,
Self::Address(_) => false,
Self::ContractId(_) => true,
}
}

Expand Down Expand Up @@ -88,8 +88,8 @@ impl Identity {
/// ```
pub fn transfer(self, amount: u64, asset_id: AssetId) {
match self {
Identity::Address(addr) => addr.transfer(amount, asset_id),
Identity::ContractId(id) => id.transfer(amount, asset_id),
Self::Address(addr) => addr.transfer(amount, asset_id),
Self::ContractId(id) => id.transfer(amount, asset_id),
};
}
}
Expand Down
Loading

0 comments on commit 660abd9

Please sign in to comment.