Skip to content

Commit

Permalink
Format sway-lib-core and sway-lib-std (FuelLabs#3902)
Browse files Browse the repository at this point in the history
This just applies formatting by `forc fmt`.
  • Loading branch information
nfurfaro authored Jan 26, 2023
1 parent 0ab1d22 commit 46f92f2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 66 deletions.
5 changes: 3 additions & 2 deletions sway-lib-core/src/never.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library never;

use ::ops::{Not, Eq, Ord};
use ::ops::{Eq, Not, Ord};

/// `Never` represents the type of computations which never resolve to any value at all.
///
Expand Down Expand Up @@ -44,7 +44,8 @@ pub enum Never {}

impl Not for Never {
fn not(self) -> Self {
match self {}
match self {
}
}
}

Expand Down
76 changes: 34 additions & 42 deletions sway-lib-core/src/ops.sw
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ impl Ord for u8 {

impl Ord for b256 {
fn gt(self, other: Self) -> bool {
let(self_word_1, self_word_2, self_word_3, self_word_4) = decompose(self);
let(other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);
let (self_word_1, self_word_2, self_word_3, self_word_4) = decompose(self);
let (other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);

if self.eq(other) {
false
Expand All @@ -303,8 +303,8 @@ impl Ord for b256 {
}

fn lt(self, other: Self) -> bool {
let(self_word_1, self_word_2, self_word_3, self_word_4) = decompose(self);
let(other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);
let (self_word_1, self_word_2, self_word_3, self_word_4) = decompose(self);
let (other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);

if self.eq(other) {
false
Expand Down Expand Up @@ -481,8 +481,8 @@ impl Not for u8 {

impl BitwiseAnd for b256 {
fn binary_and(val: self, other: Self) -> Self {
let(value_word_1, value_word_2, value_word_3, value_word_4) = decompose(val);
let(other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);
let (value_word_1, value_word_2, value_word_3, value_word_4) = decompose(val);
let (other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);
let word_1 = value_word_1.binary_and(other_word_1);
let word_2 = value_word_2.binary_and(other_word_2);
let word_3 = value_word_3.binary_and(other_word_3);
Expand All @@ -494,8 +494,8 @@ impl BitwiseAnd for b256 {

impl BitwiseOr for b256 {
fn binary_or(val: self, other: Self) -> Self {
let(value_word_1, value_word_2, value_word_3, value_word_4) = decompose(val);
let(other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);
let (value_word_1, value_word_2, value_word_3, value_word_4) = decompose(val);
let (other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);
let word_1 = value_word_1.binary_or(other_word_1);
let word_2 = value_word_2.binary_or(other_word_2);
let word_3 = value_word_3.binary_or(other_word_3);
Expand All @@ -507,8 +507,8 @@ impl BitwiseOr for b256 {

impl BitwiseXor for b256 {
fn binary_xor(val: self, other: Self) -> Self {
let(value_word_1, value_word_2, value_word_3, value_word_4) = decompose(val);
let(other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);
let (value_word_1, value_word_2, value_word_3, value_word_4) = decompose(val);
let (other_word_1, other_word_2, other_word_3, other_word_4) = decompose(other);
let word_1 = value_word_1.binary_xor(other_word_1);
let word_2 = value_word_2.binary_xor(other_word_2);
let word_3 = value_word_3.binary_xor(other_word_3);
Expand All @@ -522,8 +522,7 @@ trait OrdEq: Ord + Eq {
} {
fn ge(self, other: Self) -> bool {
self.gt(other) || self.eq(other)
}
fn le(self, other: Self) -> bool {
} fn le(self, other: Self) -> bool {
self.lt(other) || self.eq(other)
}
}
Expand Down Expand Up @@ -606,77 +605,67 @@ impl Shiftable for u8 {

impl Shiftable for b256 {
fn lsh(self, shift_amount: u64) -> Self {
let(word_1, word_2, word_3, word_4) = decompose(self);
let (word_1, word_2, word_3, word_4) = decompose(self);
let mut w1 = 0;
let mut w2 = 0;
let mut w3 = 0;
let mut w4 = 0;

let w = shift_amount.divide(64); // num of whole words to shift in addition to b
let b = shift_amount.modulo(64); // num of bits to shift within each word

// TODO: Use generalized looping version when vec lands !
if w.eq(0) {
let(shifted_2, carry_2) = lsh_with_carry(word_2, b);
let (shifted_2, carry_2) = lsh_with_carry(word_2, b);
w1 = word_1.lsh(b).add(carry_2);
let(shifted_3, carry_3) = lsh_with_carry(word_3, b);
let (shifted_3, carry_3) = lsh_with_carry(word_3, b);
w2 = shifted_2.add(carry_3);
let(shifted_4, carry_4) = lsh_with_carry(word_4, b);
let (shifted_4, carry_4) = lsh_with_carry(word_4, b);
w3 = shifted_3.add(carry_4);
w4 = shifted_4;
} else if w.eq(1) {
let(shifted_3, carry_3) = lsh_with_carry(word_3, b);
let (shifted_3, carry_3) = lsh_with_carry(word_3, b);
w1 = word_2.lsh(b).add(carry_3);
let(shifted_4, carry_4) = lsh_with_carry(word_4, b);
let (shifted_4, carry_4) = lsh_with_carry(word_4, b);
w2 = shifted_3.add(carry_4);
w3 = shifted_4;
} else if w.eq(2) {
let(shifted_4, carry_4) = lsh_with_carry(word_4, b);
let (shifted_4, carry_4) = lsh_with_carry(word_4, b);
w1 = word_3.lsh(b).add(carry_4);
w2 = shifted_4;
} else if w.eq(3) {
w1 = word_4.lsh(b);
} else {
();
};
} else if w.eq(3) { w1 = word_4.lsh(b); } else { (); };

compose((w1, w2, w3, w4))
}

fn rsh(self, shift_amount: u64) -> Self {
let(word_1, word_2, word_3, word_4) = decompose(self);
let (word_1, word_2, word_3, word_4) = decompose(self);
let mut w1 = 0;
let mut w2 = 0;
let mut w3 = 0;
let mut w4 = 0;

let w = shift_amount.divide(64); // num of whole words to shift in addition to b
let b = shift_amount.modulo(64); // num of bits to shift within each word

// TODO: Use generalized looping version when vec lands !
if w.eq(0) {
let(shifted_3, carry_3) = rsh_with_carry(word_3, b);
let (shifted_3, carry_3) = rsh_with_carry(word_3, b);
w4 = word_4.rsh(b).add(carry_3);
let(shifted_2, carry_2) = rsh_with_carry(word_2, b);
let (shifted_2, carry_2) = rsh_with_carry(word_2, b);
w3 = shifted_3.add(carry_2);
let(shifted_1, carry_1) = rsh_with_carry(word_1, b);
let (shifted_1, carry_1) = rsh_with_carry(word_1, b);
w2 = shifted_2.add(carry_1);
w1 = shifted_1;
} else if w.eq(1) {
let(shifted_2, carry_2) = rsh_with_carry(word_2, b);
let (shifted_2, carry_2) = rsh_with_carry(word_2, b);
w4 = word_3.rsh(b).add(carry_2);
let(shifted_1, carry_1) = rsh_with_carry(word_1, b);
let (shifted_1, carry_1) = rsh_with_carry(word_1, b);
w3 = shifted_2.add(carry_1);
w2 = shifted_1;
} else if w.eq(2) {
let(shifted_1, carry_1) = rsh_with_carry(word_1, b);
let (shifted_1, carry_1) = rsh_with_carry(word_1, b);
w4 = word_2.rsh(b).add(carry_1);
w3 = shifted_1;
} else if w.eq(3) {
w4 = word_1.rsh(b);
} else {
();
};
} else if w.eq(3) { w4 = word_1.rsh(b); } else { (); };

compose((w1, w2, w3, w4))
}
Expand All @@ -685,7 +674,6 @@ impl Shiftable for b256 {
/////////////////////////////////////////////////
// Internal Helpers
/////////////////////////////////////////////////

/// Left shift a u64 and preserve the overflow amount if any
fn lsh_with_carry(word: u64, shift_amount: u64) -> (u64, u64) {
let right_shift_amount = 64.subtract(shift_amount);
Expand Down Expand Up @@ -713,7 +701,7 @@ fn decompose(val: b256) -> (u64, u64, u64, u64) {
}

#[test]
fn test_compose() {
fn test_compose() {
let expected: b256 = 0x0000000000000001_0000000000000002_0000000000000003_0000000000000004;
let composed = compose((1, 2, 3, 4));
if composed.neq(expected) {
Expand All @@ -722,11 +710,15 @@ fn test_compose() {
}

#[test]
fn test_decompose() {
fn test_decompose() {
let initial: b256 = 0x0000000000000001_0000000000000002_0000000000000003_0000000000000004;
let expected = (1, 2, 3, 4);
let decomposed = decompose(initial);
if decomposed.0.neq(expected.0) && decomposed.1.neq(expected.1) && decomposed.2.neq(expected.2) && decomposed.3.neq(expected.3) {
if decomposed.0.neq(expected.0)
&& decomposed.1.neq(expected.1)
&& decomposed.2.neq(expected.2)
&& decomposed.3.neq(expected.3)
{
__revert(0)
}
}
4 changes: 1 addition & 3 deletions sway-lib-std/src/low_level_call.sw
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fn contract_id_to_bytes(contract_id: ContractId) -> Bytes {

/// Represent a raw pointer as a `Bytes`, so it can be concatenated with a payload.
fn ptr_as_bytes(ptr: raw_ptr) -> Bytes {

let mut bytes = Bytes::with_capacity(8);
bytes.len = 8;

Expand Down Expand Up @@ -91,15 +90,14 @@ pub fn call_with_function_selector(
call_with_raw_payload(payload, call_params);
}


// TO DO: Deprecate when SDK supports Bytes
/// Call a target contract with a function selector and calldata, provided as `Vec<u8>`.
pub fn call_with_function_selector_vec(
target: ContractId,
function_selector: Vec<u8>,
calldata: Vec<u8>,
single_value_type_arg: bool,
call_params: CallParams
call_params: CallParams,
) {
let mut function_selector = function_selector;
let mut calldata = calldata;
Expand Down
1 change: 0 additions & 1 deletion sway-lib-std/src/message.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ::outputs::{Output, output_count, output_type};
use ::revert::revert;
use ::error_signals::FAILED_SEND_MESSAGE_SIGNAL;


/// Sends a message `msg_data` to `recipient` with a `coins` amount of the base asset.
///
/// Use `send_typed_message` instead of `send_message` if the message needs to be indexed.
Expand Down
36 changes: 18 additions & 18 deletions sway-lib-std/src/result.sw
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ impl<T, E> Result<T, E> {
// Querying the contained values
//
/// Returns `true` if the result is `Ok`.
///
///
/// ### Examples
///
///
/// ```
/// enum Error {
/// NotFound,
/// Invalid,
/// }
///
///
/// let x: Result<u64, Error> = Result::Ok(42);
/// assert(x.is_ok());
///
///
/// let y: Result<u64, Error> = Result::Err(Error::NotFound));
/// assert(!x.is_ok());
/// ```
Expand All @@ -94,18 +94,18 @@ impl<T, E> Result<T, E> {
}

/// Returns `true` if the result is `Err`.
///
///
/// ### Examples
///
///
/// ```
/// enum Error {
/// NotFound,
/// Invalid,
/// }
///
///
/// let x: Result<u64, Error> = Result::Ok(42);
/// assert(!x.is_err());
///
///
/// let y: Result<u64, Error> = Result::Err(Error::NotFound));
/// assert(x.is_err());
/// ```
Expand All @@ -121,22 +121,22 @@ impl<T, E> Result<T, E> {
/// Because this function may revert, its use is generally discouraged.
/// Instead, prefer to use pattern matching and handle the `Err`
/// case explicitly.
///
///
/// ### Reverts
///
///
/// Reverts if the self value is `Err`.
///
///
/// ### Examples
///
///
/// ```
/// enum Error {
/// NotFound,
/// Invalid,
/// }
///
///
/// let x: Result<u64, Error> = Result::Ok(42);
/// assert(x.unwrap() == 42);
///
///
/// let y: Result<u64, Error> = Result::Err(Error::NotFound));
/// assert(x.unwrap() == 42); // reverts
/// ```
Expand All @@ -148,18 +148,18 @@ impl<T, E> Result<T, E> {
}

/// Returns the contained `Ok` value or a provided default.
///
///
/// ### Examples
///
///
/// ```
/// enum Error {
/// NotFound,
/// Invalid,
/// }
///
///
/// let x: Result<u64, Error> = Result::Ok(42);
/// assert(x.unwrap_or(69) == 42);
///
///
/// let y: Result<u64, Error> = Result::Err(Error::NotFound));
/// assert(x.unwrap_or(69) == 69);
/// ```
Expand Down

0 comments on commit 46f92f2

Please sign in to comment.