pub struct I8 {
    /// The underlying unsigned `u8` type that makes up the signed `I8` type.
    underlying: u8,
}
Expand description

The 8-bit signed integer type.

Additional Information

Represented as an underlying u8 value.
Actual value is underlying value minus 2 ^ 7
Max value is 2 ^ 7 - 1, min value is - 2 ^ 7

Fields

underlying: u8

The underlying unsigned u8 type that makes up the signed I8 type.

Implementations

fn indent() -> u8

The underlying value that corresponds to zero value.

Returns

  • [u8] - The unsigned integer value representing a zero value.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let zero = I8::indent();
    assert(zero == 128u8);
}

fn bits() -> u64

The size of this type in bits.

Returns

  • [u64] - The number of bits.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let bits = I8::bits();
    assert(bits == 8);
}

fn from_uint(underlying: u8) -> Self

Helper function to get a signed I8 from an underlying u8.

Arguments

  • underlying: [u8] - The u8 that will represent the I8.

Returns

  • [I8] - The newly created I8 struct.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let underlying = 1u8;
    let i8 = I8::from_uint(underlying);
    assert(i8.underlying() == underlying);
}

fn max() -> Self

The largest value that can be represented by this integer type.

Returns

  • [I8] - The newly created I8 struct.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let i8 = I8::max();
    assert(i8.underlying() == u8::max());
}

fn min() -> Self

The smallest value that can be represented by this integer type.

Returns

  • [I8] - The newly created I8 struct.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let i8 = I8::new();
    assert(i8.underlying() == u8::min());
}

fn neg_try_from(value: u8) -> Option<Self>

Helper function to get a negative value of an unsigned number.

Arguments

  • value: [u8] - The unsigned number to negate.

Returns

  • [Option] - The newly created I8 struct.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let underlying = 1u8;
    let i8 = I8::neg_try_from(underlying);
    assert(i8.underlying() == 127u8);
}

fn new() -> Self

Initializes a new, zeroed I8.

Additional Information

The zero value for I8 is 128u8.

Returns

  • [I8] - The newly created I8 struct.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let i8 = I8::new();
    assert(i8.underlying() == 128u8);
}

fn zero() -> Self

The zero value I8.

Returns

  • [I8] - The newly created I8 type.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let i8 = I8::zero();
    assert(i8.underlying() == 128u8);
}

fn is_zero(self) -> bool

Returns whether a I8 is set to zero.

Returns

  • [bool] -> True if the I8 is zero, otherwise false.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let i8 = I8::zero();
    assert(i8.is_zero());
}

fn underlying(self) -> u8

Returns the underlying u8 representing the I8.

Returns

  • [u8] - The u8 representing the I8.

Examples

use sway_libs::signed_integers::i8::I8;

fn foo() {
    let i8 = I8::zero();
    assert(i8.underlying() == 128u8);
}

Trait Implementations

fn eq(self, other: Self) -> bool

fn neq(self, other: Self) -> bool

Evaluates if two values of the same type are not equal.

Additional Information

This function is inherited when eq() is implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if the two values are not equal, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
         self.val == other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 != struct2;
    assert(result);
}

fn gt(self, other: Self) -> bool

fn lt(self, other: Self) -> bool

fn ge(self, other: Self) -> bool

Evaluates if one value of the same type is greater or equal to than another.

Additional Information

This trait requires that the Ord and Eq traits are implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is greater than or equal to other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
        self.val == other.val
    }
}

impl Ord for MyStruct {
    fn gt(self, other: Self) -> bool {
        self.val > other.val
    }
}

impl OrdEq for MyStruct {}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 10 };
    let result = struct1 >= struct2;
    assert(result);
}

fn le(self, other: Self) -> bool

Evaluates if one value of the same type is less or equal to than another.

Additional Information

This trait requires that the Ord and Eq traits are implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is less than or equal to other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
        self.val == other.val
    }
}

impl Ord for MyStruct {
    fn lt(self, other: Self) -> bool {
        self.val < other.val
    }
}

impl OrdEq for MyStruct {}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 10 };
    let result = struct1 <= struct2;
    assert(result);
}

fn add(self, other: Self) -> Self

Add a I8 to a I8. Panics on overflow.

fn divide(self, divisor: Self) -> Self

Divide a I8 by a I8. Panics if divisor is zero.

fn multiply(self, other: Self) -> Self

Multiply a I8 with a I8. Panics of overflow.

fn subtract(self, other: Self) -> Self

Subtract a I8 from a I8. Panics of overflow.

fn wrapping_neg(self) -> Self

fn try_from(value: u8) -> Option<Self>