Skip to content

Commit

Permalink
Merge branch 'master' into esdrubal/6825_nested_generics2
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaBatty authored Jan 24, 2025
2 parents bf6b9e8 + e025355 commit 63037b9
Show file tree
Hide file tree
Showing 11 changed files with 1,108 additions and 10 deletions.
414 changes: 414 additions & 0 deletions docs/slides/encoding.md

Large diffs are not rendered by default.

62 changes: 60 additions & 2 deletions sway-lib-std/src/address.sw
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! The `Address` type used for interacting with addresses on the fuel network.
library;

use ::convert::From;
use ::convert::{From, Into, TryFrom};
use ::hash::{Hash, Hasher};
use ::bytes::Bytes;
use ::option::Option::{self, *};

/// The `Address` type, a struct wrapper around the inner `b256` value.
pub struct Address {
Expand Down Expand Up @@ -110,7 +112,7 @@ impl From<Address> for b256 {
/// ```sway
/// fn foo() {
/// let address = Address::zero();
/// let b256_data: b256 = address.into();
/// let b256_data: b256 = b256::from(address);
/// assert(b256_data == b256::zero());
/// }
/// ```
Expand All @@ -119,6 +121,62 @@ impl From<Address> for b256 {
}
}

impl TryFrom<Bytes> for Address {
/// Casts raw `Bytes` data to an `Address`.
///
/// # Arguments
///
/// * `bytes`: [Bytes] - The raw `Bytes` data to be casted.
///
/// # Returns
///
/// * [Address] - The newly created `Address` from the raw `Bytes`.
///
/// # Examples
///
/// ```sway
/// use std::bytes::Bytes;
///
/// fn foo(bytes: Bytes) {
/// let result = Address::try_from(bytes);
/// assert(result.is_some());
/// let address = result.unwrap();
/// }
/// ```
fn try_from(bytes: Bytes) -> Option<Self> {
if bytes.len() != 32 {
return None;
}

Some(Self {
bits: asm(ptr: bytes.ptr()) {
ptr: b256
},
})
}
}

impl Into<Bytes> for Address {
/// Casts an `Address` to raw `Bytes` data.
///
/// # Returns
///
/// * [Bytes] - The underlying raw `Bytes` data of the `Address`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let address = Address::zero();
/// let bytes_data: Bytes = address.into()
/// assert(bytes_data.len() == 32);
/// }
/// ```
fn into(self) -> Bytes {
Bytes::from(self.bits())
}
}

impl Hash for Address {
fn hash(self, ref mut state: Hasher) {
let Address { bits } = self;
Expand Down
68 changes: 63 additions & 5 deletions sway-lib-std/src/asset_id.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ library;

use ::alias::SubId;
use ::contract_id::ContractId;
use ::convert::From;
use ::convert::{From, Into, TryFrom};
use ::hash::{Hash, Hasher};
use ::bytes::Bytes;
use ::option::Option::{self, *};

/// An AssetId is used for interacting with an asset on the network.
///
Expand Down Expand Up @@ -71,7 +73,7 @@ impl AssetId {
/// # Examples
///
/// ```sway
/// use std::callframes::contract_id;
/// use std::call_frames::contract_id;
///
/// fn foo() {
/// let contract_id = contract_id();
Expand Down Expand Up @@ -109,7 +111,7 @@ impl AssetId {
/// # Examples
///
/// ```sway
/// use std::{callframes::contract_id, constants::DEFAULT_SUB_ID};
/// use std::{call_frames::contract_id, constants::DEFAULT_SUB_ID};
///
/// fn foo() {
/// let asset_id = AssetId::default();
Expand Down Expand Up @@ -236,12 +238,68 @@ impl From<AssetId> for b256 {
///
/// ```sway
/// fn foo() {
/// let asset_id = AssetId::b256::zero();
/// let b256_data: b256 = asset_id.into();
/// let asset_id = AssetId::zero();
/// let b256_data: b256 = b256::from(asset_id);
/// assert(b256_data == b256::zero());
/// }
/// ```
fn from(id: AssetId) -> Self {
id.bits()
}
}

impl TryFrom<Bytes> for AssetId {
/// Casts raw `Bytes` data to an `AssetId`.
///
/// # Arguments
///
/// * `bytes`: [Bytes] - The raw `Bytes` data to be casted.
///
/// # Returns
///
/// * [AssetId] - The newly created `AssetId` from the raw `Bytes`.
///
/// # Examples
///
/// ```sway
/// use std::bytes::Bytes;
///
/// fn foo(bytes: Bytes) {
/// let result = AssetId::try_from(bytes);
/// assert(result.is_some());
/// let asset_id = result.unwrap();
/// }
/// ```
fn try_from(bytes: Bytes) -> Option<Self> {
if bytes.len() != 32 {
return None;
}

Some(Self {
bits: asm(ptr: bytes.ptr()) {
ptr: b256
},
})
}
}

impl Into<Bytes> for AssetId {
/// Casts an `AssetId` to raw `Bytes` data.
///
/// # Returns
///
/// * [Bytes] - The underlying raw `Bytes` data of the `AssetId`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let asset_id = AssetId::zero();
/// let bytes_data: Bytes = asset_id.into();
/// assert(bytes_data.len() == 32);
/// }
/// ```
fn into(self) -> Bytes {
Bytes::from(self.bits())
}
}
60 changes: 59 additions & 1 deletion sway-lib-std/src/b512.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! The `B512` type supports the usage of 64-byte values in Sway which are needed when working with public keys and signatures.
library;

use ::convert::From;
use ::convert::{From, Into, TryFrom};
use ::bytes::Bytes;
use ::option::Option::{self, *};

/// Stores two `b256`s in contiguous memory.
/// Guaranteed to be contiguous for use with ec-recover: `std::ecr::ec_recover`.
Expand Down Expand Up @@ -159,3 +161,59 @@ impl B512 {
(self.bits)[0] == b256::zero() && (self.bits)[1] == b256::zero()
}
}

impl TryFrom<Bytes> for B512 {
/// Casts raw `Bytes` data to an `B512`.
///
/// # Arguments
///
/// * `bytes`: [Bytes] - The raw `Bytes` data to be casted.
///
/// # Returns
///
/// * [B512] - The newly created `B512` from the raw `Bytes`.
///
/// # Examples
///
/// ```sway
/// use std::bytes::Bytes;
///
/// fn foo(bytes: Bytes) {
/// let result = B512::try_from(bytes);
/// assert(result.is_some());
/// let b512 = result.unwrap();
/// }
/// ```
fn try_from(bytes: Bytes) -> Option<Self> {
if bytes.len() != 64 {
return None;
}

Some(Self {
bits: asm(ptr: bytes.ptr()) {
ptr: [b256; 2]
},
})
}
}

impl Into<Bytes> for B512 {
/// Casts an `B512` to raw `Bytes` data.
///
/// # Returns
///
/// * [Bytes] - The underlying raw `Bytes` data of the `B512`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let b512 = B512::from((b256::zero(), b256::zero()));
/// let bytes_data: Bytes = b512.into()
/// assert(bytes_data.len() == 64);
/// }
/// ```
fn into(self) -> Bytes {
Bytes::from(raw_slice::from_parts::<u8>(__addr_of(self.bits), 64))
}
}
60 changes: 59 additions & 1 deletion sway-lib-std/src/contract_id.sw
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! The `ContractId` type used for interacting with contracts on the fuel network.
library;

use ::convert::From;
use ::convert::{From, Into, TryFrom};
use ::hash::{Hash, Hasher};
use ::bytes::Bytes;
use ::option::Option::{self, *};

/// The `ContractId` type, a struct wrapper around the inner `b256` value.
pub struct ContractId {
Expand Down Expand Up @@ -80,6 +82,62 @@ impl From<ContractId> for b256 {
}
}

impl TryFrom<Bytes> for ContractId {
/// Casts raw `Bytes` data to an `ContractId`.
///
/// # Arguments
///
/// * `bytes`: [Bytes] - The raw `Bytes` data to be casted.
///
/// # Returns
///
/// * [ContractId] - The newly created `ContractId` from the raw `Bytes`.
///
/// # Examples
///
/// ```sway
/// use std::bytes::Bytes;
///
/// fn foo(bytes: Bytes) {
/// let result = ContractId::try_from(bytes);
/// assert(result.is_some());
/// let contract_id = result.unwrap();
/// }
/// ```
fn try_from(bytes: Bytes) -> Option<Self> {
if bytes.len() != 32 {
return None;
}

Some(Self {
bits: asm(ptr: bytes.ptr()) {
ptr: b256
},
})
}
}

impl Into<Bytes> for ContractId {
/// Casts an `ContractId` to raw `Bytes` data.
///
/// # Returns
///
/// * [Bytes] - The underlying raw `Bytes` data of the `ContractId`.
///
/// # Examples
///
/// ```sway
/// fn foo() {
/// let contract_id = ContractId::zero();
/// let bytes_data: Bytes = contract_id.into()
/// assert(bytes_data.len() == 32);
/// }
/// ```
fn into(self) -> Bytes {
Bytes::from(self.bits())
}
}

impl Hash for ContractId {
fn hash(self, ref mut state: Hasher) {
let Self { bits } = self;
Expand Down
Loading

0 comments on commit 63037b9

Please sign in to comment.